class ModelFromTableGenerator::Resource

Attributes

table_name[R]

Public Class Methods

new(connection, table_name) click to toggle source
# File lib/generators/model_from_table_generator.rb, line 21
def initialize connection, table_name
  @connection = connection
  @table_name = table_name
end

Public Instance Methods

belongs_to_columns() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 58
def belongs_to_columns
  @connection.columns(table_name).select { |col| col.name =~ /^.*_id$/ }
end
belongs_to_syms() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 54
def belongs_to_syms
  belongs_to_columns.map { |col| col.name.sub('_id', '').to_sym }
end
class_name() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 26
def class_name
  @class_name ||= table_name.classify
end
file_path() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 30
def file_path
  "app/models/#{class_name.underscore}.rb"
end
primary_key() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 42
def primary_key
  @pk if @pk != nil

  if @connection.respond_to?(:pk_and_sequence_for)
    @pk, _ = @connection.pk_and_sequence_for(table_name)
  elsif @connection.respond_to?(:primary_key)
    @pk = @connection.primary_key(table_name)
  end

  @pk
end
primary_key_required?() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 38
def primary_key_required?
  primary_key != nil && primary_key.to_s != 'id'
end
table_name_required?() click to toggle source
# File lib/generators/model_from_table_generator.rb, line 34
def table_name_required?
  class_name.tableize != table_name
end