lib/model_probe.rb
# Prints a new model definition based on the database schema
Prints a new model definition based on the database schema
Prints a new model definition based on the database schema
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
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
Prints a new model definition based on the database schema
Prints a new model definition based on the database schema