class SequelSchemaDotGenerator::SchemaSourceType::Column

Public Instance Methods

relations() click to toggle source
# File lib/sequel_schema_dot_generator/schema_source_types/column.rb, line 4
def relations
  relations = []

  @tables.each do |table_name|
    @db[table_name].columns.select{|cn|cn=~/_id$/}.each do |column_name|
      foreign_column = column_name.to_s
      foreign_table = existing_table_name foreign_column.gsub(/_id$/, '')
      relations << [foreign_table, 'id', table_name, foreign_column]
    end
  end

  relations
end

Private Instance Methods

existing_table_name(name) click to toggle source

@param [String] name

@return [String] form of name param in which it is name of existing table

# File lib/sequel_schema_dot_generator/schema_source_types/column.rb, line 23
def existing_table_name name
  tables = @db.tables.map(&:to_s)
  table_name = name if tables.include? name
  table_name ||= name.pluralize if tables.include? name.pluralize
  table_name ||= name.singularize if tables.include? name.singularize
  table_name ||= 'association_table_not_found'

  table_name
end