Sha256: 11d37b95d894b76d8a02e31e6d37077cf18f05fdfde647528696c63531166aba

Contents?: true

Size: 1.94 KB

Versions: 14

Compression:

Stored size: 1.94 KB

Contents

module SchemaPlus
  module ActiveRecord
    module ConnectionAdapters

      #
      # SchemaPlus adds several methods to Column
      #
      module Column

        attr_reader :default_expr
        attr_writer :model # model gets set by SchemaPlus::ActiveRecord::Base::columns_with_schema_plus

        # Returns the list of IndexDefinition instances for each index that
        # refers to this column.  Returns an empty list if there are no
        # such indexes.
        def indexes
          @indexes ||= @model.indexes.select{|index| index.columns.include? self.name}
        end

        # 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.
        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

        # Returns true if the column is in a unique index.  See also
        # unique_scope
        def unique?
          indexes.any?{|i| i.unique}
        end

        # Returns true if the column is in one or more indexes that are
        # case sensitive
        def case_sensitive?
          indexes.any?{|i| i.case_sensitive?}
        end

        # 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
        def required_on
          if null
            nil
          elsif default.nil?
            :save
          else
            :update
          end
        end

        # The default as_jon includes all instance variables.  but
        # @model can't be dumped (it contains circular references)
        def as_json(options=nil)
          instance_values.except "model", "adapter"
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
schema_plus-1.8.9 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.8 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.7 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.6 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.5 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.4 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.3 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.2 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.1 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.8.0 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.7.1 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.7.0 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.6.0 lib/schema_plus/active_record/connection_adapters/column.rb
schema_plus-1.5.3 lib/schema_plus/active_record/connection_adapters/column.rb