Sha256: 19c0e9dca171eeb00a72b70a01c5c13c80257954f9542f6eed8cf48505816ab6

Contents?: true

Size: 1.7 KB

Versions: 27

Compression:

Stored size: 1.7 KB

Contents

module ActiveRecord
  module AttributeMethods
    module PrimaryKey
      extend ActiveSupport::Concern

      # Returns this record's primary key value wrapped in an Array
      # or nil if the record is a new_record?
      def to_key
        new_record? ? nil : [ id ]
      end

      module ClassMethods
        # Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
        # primary_key_prefix_type setting, though.
        def primary_key
          reset_primary_key
        end

        # Returns a quoted version of the primary key name, used to construct SQL statements.
        def quoted_primary_key
          @quoted_primary_key ||= connection.quote_column_name(primary_key)
        end

        def reset_primary_key #:nodoc:
          key = get_primary_key(base_class.name)
          set_primary_key(key)
          key
        end

        def get_primary_key(base_name) #:nodoc:
          key = 'id'
          case primary_key_prefix_type
            when :table_name
              key = base_name.to_s.foreign_key(false)
            when :table_name_with_underscore
              key = base_name.to_s.foreign_key
          end
          key
        end

        # Sets the name of the primary key column to use to the given value,
        # or (if the value is nil or false) to the value returned by the given
        # block.
        #
        #   class Project < ActiveRecord::Base
        #     set_primary_key "sysid"
        #   end
        def set_primary_key(value = nil, &block)
          @quoted_primary_key = nil
          define_attr_method :primary_key, value, &block
        end
        alias :primary_key= :set_primary_key
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
activerecord-3.0.20 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.19 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.18 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.17 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.16 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.15 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.14 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.13 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.13.rc1 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.12 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.12.rc1 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.11 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.10 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.10.rc1 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.9 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.9.rc5 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.9.rc4 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.9.rc3 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.9.rc1 lib/active_record/attribute_methods/primary_key.rb
activerecord-3.0.8 lib/active_record/attribute_methods/primary_key.rb