diff --git a/Rakefile b/Rakefile index 7413a80..d301523 100644 --- a/Rakefile +++ b/Rakefile @@ -26,6 +26,9 @@ task :init_test_app => 'appraisal:install' do system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Hacker social_ability:integer' system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Beer name:string type:string rating:integer ibu:integer abv:integer' system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rake db:migrate' + + # Replace generated models + cp_r 'test/models/', 'test/app/app/models/' end desc 'Start the Rails server' diff --git a/Readme.md b/Readme.md index ba27dfe..fb851b3 100644 --- a/Readme.md +++ b/Readme.md @@ -42,6 +42,34 @@ edit_pokemon GET /pokemon/edit(.:format) pokemons#edit [3] pry(main)> show-routes --grep new new_pokemon GET /pokemon/new(.:format) pokemons#new new_beer GET /beer/new(.:format) beers#new +[4] pry(main)> show-models +Beer + id: integer + name: string + type: string + rating: integer + ibu: integer + abv: integer + created_at: datetime + updated_at: datetime + belongs_to hacker +Hacker + id: integer + social_ability: integer + created_at: datetime + updated_at: datetime + has_many pokemons + has_many beers +Pokemon + id: integer + name: string + caught: binary + species: string + abilities: string + created_at: datetime + updated_at: datetime + belongs_to hacker + has_many beers through hacker ``` # Alternative diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb index ab3889c..3db40ee 100644 --- a/lib/pry-rails/commands.rb +++ b/lib/pry-rails/commands.rb @@ -66,16 +66,27 @@ module PryRails show-models displays the current Rails app's models. USAGE - opt.on :G, "grep", "Filter output by regular expression", :argument => true + opt.on :G, "grep", "Color output red by regular expression", :argument => true end def process Rails.application.eager_load! + models = ActiveRecord::Base.descendants.map do |mod| - mod.to_s + "\n" + mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n") + model_string = mod.to_s + "\n" + if mod.table_exists? + model_string << mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n") + else + model_string << " Table doesn't exist" + end + mod.reflections.each do |model,ref| + model_string << "\n #{ref.macro.to_s} #{model}" + model_string << " through #{ref.options[:through]}" unless ref.options[:through].nil? + end + model_string end.join("\n") - models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } + models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } unless opts[:G].nil? output.puts models end diff --git a/test/models/beer.rb b/test/models/beer.rb new file mode 100644 index 0000000..be56ed0 --- /dev/null +++ b/test/models/beer.rb @@ -0,0 +1,3 @@ +class Beer < ActiveRecord::Base + belongs_to :hacker +end diff --git a/test/models/hacker.rb b/test/models/hacker.rb new file mode 100644 index 0000000..0ce1492 --- /dev/null +++ b/test/models/hacker.rb @@ -0,0 +1,4 @@ +class Hacker < ActiveRecord::Base + has_many :pokemons + has_many :beers +end diff --git a/test/models/pokemon.rb b/test/models/pokemon.rb new file mode 100644 index 0000000..1f9bcf3 --- /dev/null +++ b/test/models/pokemon.rb @@ -0,0 +1,4 @@ +class Pokemon < ActiveRecord::Base + belongs_to :hacker + has_many :beers, through: :hacker +end