Module: SchemaPlus::ActiveRecord::ConnectionAdapters::AbstractAdapter::AddColumnOptions

Defined in:
lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:


194
195
196
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 194

def self.included(base) #:nodoc:
  base.alias_method_chain :add_column_options!, :schema_plus
end

Instance Method Details

#add_column_options_with_schema_plus!(sql, options) ⇒ Object


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 198

def add_column_options_with_schema_plus!(sql, options)
  if options_include_default?(options)
    default = options[:default]

    if default.is_a? Hash and [[:expr], [:value]].include?(default.keys)
      value = default[:value]
      expr = sql_for_function(default[:expr]) || default[:expr] if default[:expr]
    else
      value = default
      expr = sql_for_function(default)
    end

    if expr
      raise ArgumentError, "Invalid default expression" unless default_expr_valid?(expr)
      sql << " DEFAULT #{expr}"
      # must explicitly check for :null to allow change_column to work on migrations
      if options[:null] == false
        sql << " NOT NULL"
      end
    else
      add_column_options_without_schema_plus!(sql, options.merge(default: value))
    end
  else
    add_column_options_without_schema_plus!(sql, options)
  end
end

#default_expr_valid?(expr) ⇒ Boolean

(abstract) Return true if the passed expression can be used as a column default value. (For most databases the specific expression doesn't matter, and the adapter's function would return a constant true if default expressions are supported or false if they're not.)


235
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 235

def default_expr_valid?(expr) raise "Internal Error: Connection adapter didn't override abstract function"; end

#sql_for_function(function_name) ⇒ Object

(abstract) Return SQL definition for a given canonical function_name symbol. Currently, the only function to support is :now, which should return a DATETIME object for the current time.


240
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 240

def sql_for_function(function_name) raise "Internal Error: Connection adapter didn't override abstract function"; end