Class: PryRails::ModelFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-rails/model_formatter.rb

Instance Method Summary collapse

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|
    options = []

    if reflection.options[:through].present?
      options << "through #{text.blue ":#{reflection.options[:through]}"}"
    end

    if reflection.options[:class_name].present?
      options << "class_name #{text.green ":#{reflection.options[:class_name]}"}"
    end

    if reflection.options[:foreign_key].present?
      options << "foreign_key #{text.red ":#{reflection.options[:foreign_key]}"}"
    end

    out.push format_association reflection.macro, other_model, options
  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, options = [])
  options_string = (options.any?) ? " (#{options.join(', ')})" : ''
  "  #{type} #{text.blue ":#{other}"}#{options_string}"
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(message)
  "  #{text.red message}"
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.options[:type]
  end

  model.relations.each do |other_model, ref|
    options = []
    options << 'autosave'  if ref.options[:autosave] || ref.autosave?
    options << 'autobuild' if ref.options[:autobuild] || ref.autobuilding?
    options << 'validate'  if ref.options[:validate] || ref.validate?

    if ref.options[:dependent] || ref.dependent
      options << "dependent-#{ref.options[:dependent] || ref.dependent}"
    end

    out.push format_association \
      kind_of_relation(ref.relation), other_model, options
  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