Module: SchemaPlus::ActiveRecord::ConnectionAdapters::Column

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

Overview

SchemaPlus adds several methods to Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_exprObject (readonly)

Returns the value of attribute default_expr


10
11
12
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 10

def default_expr
  @default_expr
end

#model=(value) ⇒ Object (writeonly)

model gets set by SchemaPlus::ActiveRecord::Base::columns_with_schema_plus


11
12
13
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 11

def model=(value)
  @model = value
end

Instance Method Details

#as_json(options = nil) ⇒ Object

The default as_jon includes all instance variables. but


57
58
59
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 57

def as_json(options=nil)
  instance_values.except "model", "adapter"
end

#case_sensitive?Boolean

Returns true if the column is in one or more indexes that are case sensitive


37
38
39
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 37

def case_sensitive?
  indexes.any?{|i| i.case_sensitive?}
end

#indexesObject

Returns the list of IndexDefinition instances for each index that refers to this column. Returns an empty list if there are no such indexes.


16
17
18
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 16

def indexes
  @indexes ||= @model.indexes.select{|index| index.columns.include? self.name}
end

#required_onObject

Returns the circumstance in which the column must have a value:

nil     if the column may be null
:save   if the column has no default value
:update otherwise

45
46
47
48
49
50
51
52
53
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 45

def required_on
  if null
    nil
  elsif default.nil?
    :save
  else
    :update
  end
end

#unique?Boolean

Returns true if the column is in a unique index. See also unique_scope


31
32
33
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 31

def unique?
  indexes.any?{|i| i.unique}
end

#unique_scopeObject

If the column is in a unique index, returns a list of names of other columns in the index. Returns an empty list if it's a single-column index. Returns nil if the column is not in a unique index.


23
24
25
26
27
# File 'lib/schema_plus/active_record/connection_adapters/column.rb', line 23

def unique_scope
  if index = indexes.select{|i| i.unique}.sort_by{|i| i.columns.size}.first
    index.columns.reject{|name| name == self.name}
  end
end