Class: PryRails::ModelFormatter
- Inherits:
-
Object
- Object
- PryRails::ModelFormatter
- Defined in:
- lib/pry-rails/model_formatter.rb
Instance Method Summary collapse
- #format_active_record(model) ⇒ Object
- #format_association(type, other, options = []) ⇒ Object
- #format_column(name, type) ⇒ Object
- #format_error(message) ⇒ Object
- #format_model_name(model) ⇒ Object
- #format_mongoid(model) ⇒ Object
- #kind_of_relation(relation) ⇒ Object
Instance Method Details
#format_active_record(model) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/pry-rails/model_formatter.rb', line 5 def format_active_record(model) out = [] out.push format_model_name model if model.table_exists? model.columns.each do |column| out.push format_column column.name, column.type end else out.push format_error "Table doesn't exist" end reflections = model.reflections.sort_by do |other_model, reflection| [reflection.macro.to_s, other_model.to_s] end reflections.each do |other_model, reflection| = [] if reflection.[:through].present? << "through #{text.blue ":#{reflection.[:through]}"}" end if reflection.[:class_name].present? << "class_name #{text.green ":#{reflection.[:class_name]}"}" end if reflection.[:foreign_key].present? << "foreign_key #{text.red ":#{reflection.[:foreign_key]}"}" end out.push format_association reflection.macro, other_model, end out.join("\n") end |
#format_association(type, other, options = []) ⇒ Object
75 76 77 78 |
# File 'lib/pry-rails/model_formatter.rb', line 75 def format_association(type, other, = []) = (.any?) ? " (#{.join(', ')})" : '' " #{type} #{text.blue ":#{other}"}#{}" end |
#format_column(name, type) ⇒ Object
71 72 73 |
# File 'lib/pry-rails/model_formatter.rb', line 71 def format_column(name, type) " #{name}: #{text.green type}" end |
#format_error(message) ⇒ Object
80 81 82 |
# File 'lib/pry-rails/model_formatter.rb', line 80 def format_error() " #{text.red }" end |
#format_model_name(model) ⇒ Object
67 68 69 |
# File 'lib/pry-rails/model_formatter.rb', line 67 def format_model_name(model) text.bright_blue model end |
#format_mongoid(model) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pry-rails/model_formatter.rb', line 42 def format_mongoid(model) out = [] out.push format_model_name model model.fields.values.sort_by(&:name).each do |column| out.push format_column column.name, column.[:type] end model.relations.each do |other_model, ref| = [] << 'autosave' if ref.[:autosave] || ref.autosave? << 'autobuild' if ref.[:autobuild] || ref.autobuilding? << 'validate' if ref.[:validate] || ref.validate? if ref.[:dependent] || ref.dependent << "dependent-#{ref.[:dependent] || ref.dependent}" end out.push format_association \ kind_of_relation(ref.relation), other_model, end out.join("\n") end |
#kind_of_relation(relation) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pry-rails/model_formatter.rb', line 84 def kind_of_relation(relation) case relation.to_s.sub(/^Mongoid::(Relations::|Association::)/, '') when 'Referenced::Many', 'Referenced::HasMany::Proxy' 'has_many' when 'Referenced::One', 'Referenced::HasOne::Proxy' 'has_one' when 'Referenced::In', 'Referenced::BelongsTo::Proxy' 'belongs_to' when 'Referenced::HasAndBelongsToMany::Proxy' 'has_and_belongs_to_many' when 'Embedded::Many', 'Embedded::EmbedsMany::Proxy' 'embeds_many' when 'Embedded::One', 'Embedded::EmbedsOne::Proxy' 'embeds_one' when 'Embedded::In', 'Embedded::EmbeddedIn::Proxy' 'embedded_in' else '(unknown relation)' end end |