Module: ModelProbe

Includes:
Color
Defined in:
lib/model_probe.rb,
lib/model_probe/color.rb,
lib/model_probe/version.rb,
lib/model_probe/railtie.rb

Defined Under Namespace

Modules: Color Classes: Railtie

Constant Summary collapse

VERSION =
"1.0.7"

Instance Method Summary collapse

Instance Method Details

Prints a stub that can be used for a test fixture



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/model_probe.rb', line 30

def print_fixture
  values = columns.sort_by(&:name).each_with_object({}) do |column, memo|
    next if primary_key_column?(column)
    next if timestamp_column?(column)
    memo[column.name] = column.default || "value"
  end

  hash = { self.name.underscore => values }
  puts hash.to_yaml
  nil
end

Prints a new model definition based on the database schema



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/model_probe.rb', line 43

def print_model
  puts "class #{name} < #{superclass.name}"
  puts "  # extends ..................................................................."
  puts "  # includes .................................................................."
  puts if relation_columns.size > 0
  puts "  # relationships ............................................................."
  relation_columns.sort_by(&:name).each do |column|
    next if primary_key_column?(column)
    puts "  belongs_to :#{column.name.sub(/_id\z/, "")}" if column.name =~ /_id\z/
  end
  puts if relation_columns.size > 0 || validation_columns.size > 0
  puts "  # validations ..............................................................."
  validation_columns.sort_by(&:name).each do |column|
    next if primary_key_column?(column)
    puts "  validates :#{column.name}, presence: true" unless column.null
    if i(text string).include?(column.type) && column.limit.to_i > 0
      puts "  validates :#{column.name}, length: { maximum: #{column.limit} }"
    end
  end
  puts if validation_columns.size > 0
  puts "  # callbacks ................................................................."
  puts "  # scopes ...................................................................."
  puts "  # additional config (i.e. accepts_nested_attribute_for etc...) .............."
  puts
  puts "  # class methods ............................................................."
  puts "  class << self"
  puts "  end"
  puts
  puts "  # public instance methods ..................................................."
  puts
  puts "  # protected instance methods ................................................"
  puts "  protected"
  puts
  puts "  # private instance methods .................................................."
  puts "  private"
  puts "end"
  nil
end

#probeObject

Pretty prints column meta data for an ActiveModel



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/model_probe.rb', line 9

def probe
  name_pad = columns.map { |c| c.name.length }.max + 1
  type_pad = columns.map { |c| c.type.length }.max + 2
  sql_type_pad = columns.map { |c| c.sql_type.length }.max + 1

  columns.sort { |a, b| a.name <=> b.name }.map do |column|
    name = column.name
    name = "* #{name}" if primary_key_column?(column)
    print yellow(name.to_s.rjust(name_pad))
    print " "
    print blue(column.type.to_s.ljust(type_pad, "."))
    print magenta(column.sql_type.to_s.ljust(sql_type_pad))
    column.null ? print(red("NULL")) : print("    ")
    print " [#{column.default}]" if column.default
    print " #{gray column.comment}" if column.comment
    puts
  end
  nil
end