1
0
Fork 0
mirror of https://github.com/pry/pry-rails.git synced 2022-11-09 12:36:03 -05:00

Merge remote-tracking branch 'TreyLawrence/show-models'

Conflicts:
	Rakefile
	lib/pry-rails/commands.rb
This commit is contained in:
Ryan Fitzgerald 2012-07-18 17:03:19 -07:00
commit d88ed9ae91
6 changed files with 56 additions and 3 deletions

View file

@ -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'

View file

@ -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

View file

@ -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

3
test/models/beer.rb Normal file
View file

@ -0,0 +1,3 @@
class Beer < ActiveRecord::Base
belongs_to :hacker
end

4
test/models/hacker.rb Normal file
View file

@ -0,0 +1,4 @@
class Hacker < ActiveRecord::Base
has_many :pokemons
has_many :beers
end

4
test/models/pokemon.rb Normal file
View file

@ -0,0 +1,4 @@
class Pokemon < ActiveRecord::Base
belongs_to :hacker
has_many :beers, through: :hacker
end