Module: LookupBy::Lookup::SchemaMethods
- Defined in:
- lib/lookup_by/lookup.rb
Instance Method Summary collapse
-
#create_lookup_table(name, options = {}) ⇒ Object
Create a lookup table.
- #create_lookup_tables(*names) ⇒ Object
Instance Method Details
#create_lookup_table(name, options = {}) ⇒ Object
Create a lookup table.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/lookup_by/lookup.rb', line 147 def create_lookup_table(name, = {}) .symbolize_keys! schema = [: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 = [:lookup_column] || table.singularize lookup_type = [:lookup_type] || :text = .slice(:primary_key, :id) [:primary_key] ||= table.singularize + '_id' [:id] = false if [:small] create_table name, do |t| t.column [:primary_key], 'smallserial primary key' if [:small] t.column lookup_column, lookup_type, 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
180 181 182 183 184 185 186 |
# File 'lib/lookup_by/lookup.rb', line 180 def create_lookup_tables(*names) = names.last.is_a?(Hash) ? names.pop : {} names.each do |name| create_lookup_table name, end end |