Sha256: 0112101ed0bee675c8fb53ef579238fba68022359f09d792d4abe573a1bb826f

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

Contents

# frozen_string_literal: true

module Bullet
  module Ext
    module Object
      refine ::Object do
        def bullet_key
          "#{self.class}:#{bullet_primary_key_value}"
        end

        def bullet_primary_key_value
          return if respond_to?(:persisted?) && !persisted?

          if self.class.respond_to?(:primary_keys) && self.class.primary_keys
            primary_key = self.class.primary_keys
          elsif self.class.respond_to?(:primary_key) && self.class.primary_key
            primary_key = self.class.primary_key
          else
            primary_key = :id
          end

          bullet_join_potential_composite_primary_key(primary_key)
        end

        private

        def bullet_join_potential_composite_primary_key(primary_keys)
          return send(primary_keys) unless primary_keys.is_a?(Enumerable)

          primary_keys.map { |primary_key| send primary_key }
                      .join(',')
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bullet-8.0.0 lib/bullet/ext/object.rb