Sha256: 24cf9eba88a3113301ba508a810f0cd85435c80f9efd722fd5934c10384e5c10

Contents?: true

Size: 1.59 KB

Versions: 11

Compression:

Stored size: 1.59 KB

Contents

module ActiveRemote
  module PrimaryKey
    extend ActiveSupport::Concern

    module ClassMethods
      ##
      # The default_primary_key is used to define what attribute is used
      # as a primary key for your global code base. If you use a primary
      # key other than this, you'll want to set it using the primary_key
      # method listed below
      #
      def default_primary_key
        :guid
      end

      ##
      # In the event that you use a different primary key than the default
      # primary key listed above (like :uuid, or :id), you'll use this method
      # to change that primary key. This will be used when making remote
      # calls to persist or refresh data.
      #
      def primary_key(value = nil)
        @primary_key = value if value
        @primary_key || default_primary_key
      end
    end

    ##
    # Instance level access to either the default primary key, or whatever
    # you configured the class level primary key to be.
    #
    def primary_key
      self.class.primary_key
    end

    # Returns an Array of all key attributes if any of the attributes is set, whether or not
    # the object is persisted. Returns +nil+ if there are no key attributes.
    #
    #   class Person
    #     include ActiveModel::Conversion
    #     attr_accessor :id
    #
    #     def initialize(id)
    #       @id = id
    #     end
    #   end
    #
    #   person = Person.new(1)
    #   person.to_key # => [1]
    def to_key
      @__to_key_key = respond_to?(primary_key) && send(primary_key) if @__to_key_key.nil?
      @__to_key_key ? [@__to_key_key] : nil
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_remote-7.1.0 lib/active_remote/primary_key.rb
active_remote-6.0.3 lib/active_remote/primary_key.rb
active_remote-6.1.2 lib/active_remote/primary_key.rb
active_remote-7.0.0 lib/active_remote/primary_key.rb
active_remote-6.1.1 lib/active_remote/primary_key.rb
active_remote-6.1.0 lib/active_remote/primary_key.rb
active_remote-6.0.2 lib/active_remote/primary_key.rb
active_remote-6.0.1 lib/active_remote/primary_key.rb
active_remote-6.0.0.beta lib/active_remote/primary_key.rb
active_remote-5.2.0 lib/active_remote/primary_key.rb
active_remote-5.2.0.beta lib/active_remote/primary_key.rb