Module: LookupBy::Lookup::SchemaMethods

Defined in:
lib/lookup_by/lookup.rb

Instance Method Summary collapse

Instance Method Details

#create_lookup_table(name, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/lookup_by/lookup.rb', line 116

def create_lookup_table(name, options = {})
  options.symbolize_keys!

  schema = options[:schema].to_s

  if schema.present?
    table = name.to_s
  else
    schema, table = name.to_s.split('.')
    schema, table = nil, schema unless table
  end

  name = schema.blank? ? table : "#{schema}.#{table}"

  lookup_column = options[:lookup_column] || table.singularize
  lookup_type   = options[:lookup_type]   || :text

  table_options = options.slice(:primary_key, :id)
  table_options[:primary_key] ||= table.singularize + '_id'

  create_table name, table_options do |t|
    t.send lookup_type, lookup_column, null: false

    yield t if block_given?
  end

  add_index name, lookup_column, unique: true, name: "#{table}__u_#{lookup_column}"
end

#create_lookup_tables(*names) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/lookup_by/lookup.rb', line 145

def create_lookup_tables(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}

  names.each do |name|
    create_lookup_table name, options
  end
end