class ActiveRecord::Reflection::AssociationReflection

Public Instance Methods

active_record_primary_key() click to toggle source
# File lib/composite_primary_keys/reflection.rb, line 42
def active_record_primary_key
  # CPK (Rails freezes the string returned in the expression that calculates PK here. But Rails uses the `-` method which is not available on Array for CPK, so we calculate it in one line and freeze it on the next)
  # @active_record_primary_key ||= -(options[:primary_key]&.to_s || primary_key(active_record))
  @active_record_primary_key ||= begin
    pk = options[:primary_key] || primary_key(active_record)
    pk.freeze
  end
end
association_foreign_key() click to toggle source
# File lib/composite_primary_keys/reflection.rb, line 36
def association_foreign_key
  # CPK
  # @association_foreign_key ||= -(options[:association_foreign_key]&.to_s || class_name.foreign_key)
  @association_foreign_key ||= extract_keys(options[:association_foreign_key]) || class_name.foreign_key
end
foreign_key() click to toggle source
# File lib/composite_primary_keys/reflection.rb, line 30
def foreign_key
  # CPK
  # @foreign_key ||= -(options[:foreign_key]&.to_s || derive_foreign_key)
  @foreign_key ||= extract_keys(options[:foreign_key]) || derive_foreign_key
end

Private Instance Methods

extract_keys(keys) click to toggle source
# File lib/composite_primary_keys/reflection.rb, line 53
def extract_keys(keys)
  case keys
    when Array
      keys.map { |k| k.to_s }
    when NilClass
      nil
    else
      keys.to_s
  end
end