Module: SchemaPlus::ActiveRecord::ColumnOptionsHandler

Included in:
SchemaPlus::ActiveRecord::ConnectionAdapters::TableDefinition, ForeignKeys, Migration::CommandRecorder
Defined in:
lib/schema_plus/active_record/column_options_handler.rb

Instance Method Summary collapse

Instance Method Details

#schema_plus_handle_column_options(table_name, column_name, column_options, opts = {}) ⇒ Object

:nodoc:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/schema_plus/active_record/column_options_handler.rb', line 22

def schema_plus_handle_column_options(table_name, column_name, column_options, opts = {}) #:nodoc:
  config = opts[:config] || SchemaPlus.config
  fk_args = get_fk_args(table_name, column_name, column_options, config)

  # remove existing fk and auto-generated index in case of change to existing column
  if fk_args # includes :none for explicitly off
    remove_foreign_key_if_exists(table_name, column_name)
    remove_auto_index_if_exists(table_name, column_name)
  end

  fk_args = nil if fk_args == :none

  # create index if requested explicity or implicitly due to auto_index
  index = column_options[:index]
  index = column_options[:_index] if column_options.include? :_index
  if index.nil? and fk_args && config.foreign_keys.auto_index?
    index = { :name => auto_index_name(table_name, column_name) }
  end
  column_index(table_name, column_name, index) if index

  if fk_args
    references = fk_args.delete(:references)
    add_foreign_key(table_name, column_name, references.first, references.last, fk_args)
  end
end

#schema_plus_normalize_column_options(options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/schema_plus/active_record/column_options_handler.rb', line 3

def schema_plus_normalize_column_options(options)
  # replace some shortcuts with full versions
  [:index, :_index].each do |key|
    case options[key]
    when true then options[key] = {}
    when :unique then options[key] = { :unique => true }
    when Hash
      if options[key][:length].is_a? Hash
        normalize = if "#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}".to_r >= "4.2".to_r
                      :stringify_keys!
                    else
                      :symbolize_keys!
                    end
        options[key][:length].send normalize
      end
    end
  end
end