File: object.rb

package info (click to toggle)
ruby-bullet 7.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 6,133; javascript: 57; sh: 27; makefile: 4
file content (30 lines) | stat: -rw-r--r-- 781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

class Object
  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