class Araa::Table

Attributes

belongs_to[RW]
has_many[RW]
has_many_through[RW]

Public Class Methods

new(name, build=false) click to toggle source
# File lib/araa/table.rb, line 9
def initialize(name, build=false)
  @name = name
  @belongs_to = []
  @has_one = []
  @has_one_through = []
  @has_many = []
  @has_many_through = []
  @has_and_belongs_to_many = []

  if build
    find_belongs_to
    find_has_many
    find_has_many_through
  end
end

Public Instance Methods

fields() click to toggle source
# File lib/araa/table.rb, line 37
def fields
  model.column_names.reject { |c| Araa::RAILS_META_COLUMNS.include?(c) }
end
find_belongs_to() click to toggle source
# File lib/araa/table.rb, line 51
def find_belongs_to
  fields.each { |field|
    next unless field.match(/_id$/)
    @belongs_to << field.gsub('_id', '')
  }
end
find_has_many(taf=Araa::ActiveRecord.tables_field_map) click to toggle source
# File lib/araa/table.rb, line 58
def find_has_many(taf=Araa::ActiveRecord.tables_field_map)
  taf.each do |table_field|
    if Regexp.new("#{table_name.singularize}_id").match(table_field)
      @has_many << table_field.split('::').first
    end
  end
end
find_has_many_through(taf=Araa::ActiveRecord.tables_field_map) click to toggle source
# File lib/araa/table.rb, line 66
def find_has_many_through(taf=Araa::ActiveRecord.tables_field_map)
  @has_many.each do |hm|
    associated_table = Araa::Table.new(hm)
    associated_table.find_belongs_to

    associated_table.belongs_to.each do |bt|
      next if bt == table_name.singularize
      @has_many_through << [bt.pluralize, hm]
    end
  end
end
has_relations?() click to toggle source
# File lib/araa/table.rb, line 41
def has_relations?
  @belongs_to.any? || @has_many.any? || @has_many_through.any?
end
is_habtm?() click to toggle source
# File lib/araa/table.rb, line 45
def is_habtm?
  # a table is considered a has_and_belongs_to_many table if it only has
  # foreign key columns.
  fields.all? { |field| field.match(/_id$/) }
end
model() click to toggle source
# File lib/araa/table.rb, line 33
def model
  model_name.constantize
end
model_name() click to toggle source
# File lib/araa/table.rb, line 29
def model_name
  table_name.singularize.camelize
end
table_name() click to toggle source
# File lib/araa/table.rb, line 25
def table_name
  @name
end