class ActiveScaffold::DataStructures::Association::Abstract

Attributes

reverse[W]

Public Class Methods

new(association) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 3
def initialize(association)
  @association = association
end

Public Instance Methods

allow_join?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 9
def allow_join?
  !polymorphic?
end
as() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 79
def as; end
belongs_to?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 21
def belongs_to?
  @association.macro == :belongs_to
end
collection?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 41
def collection?
  has_many? || habtm?
end
counter_cache() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 61
def counter_cache
  @association.options[:counter_cache]
end
counter_cache_hack?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 85
def counter_cache_hack?
  false
end
habtm?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 33
def habtm?
  @association.macro == :has_and_belongs_to_many
end
has_many?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 29
def has_many? # rubocop:disable Naming/PredicateName
  @association.macro == :has_many
end
has_one?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 25
def has_one? # rubocop:disable Naming/PredicateName
  @association.macro == :has_one
end
inverse_for?(klass) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 104
def inverse_for?(klass)
  inverse_class = reverse_association(klass)&.inverse_klass
  inverse_class.present? && (inverse_class == klass || klass < inverse_class)
end
klass(record = nil) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 13
def klass(record = nil)
  if polymorphic?
    record&.send(foreign_type)&.safe_constantize
  else
    @association.klass
  end
end
polymorphic?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 65
def polymorphic?
  false
end
primary_key() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 57
def primary_key
  @association.options[:primary_key]
end
quoted_primary_key() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 93
def quoted_primary_key
  raise "define quoted_primary_key method in #{self.class.name} class"
end
quoted_table_name() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 89
def quoted_table_name
  raise "define quoted_table_name method in #{self.class.name} class"
end
readonly?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 69
def readonly?
  false
end
respond_to_target?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 81
def respond_to_target?
  false
end
reverse(klass = nil) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 97
def reverse(klass = nil)
  unless polymorphic? || defined?(@reverse)
    @reverse ||= inverse || get_reverse&.name
  end
  @reverse || (get_reverse(klass)&.name unless klass.nil?)
end
reverse_association(klass = nil) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 109
def reverse_association(klass = nil)
  assoc =
    if polymorphic?
      get_reverse(klass) unless klass.nil?
    else
      reverse_name = reverse(klass)
      reflect_on_association(reverse_name) if reverse_name
    end
  self.class.new(assoc) if assoc
end
scope() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 77
def scope; end
singular?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 37
def singular?
  !collection?
end
source_reflection() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 75
def source_reflection; end
through?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 45
def through?
  false
end
through_collection?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 53
def through_collection?
  through? && through_reflection.collection?
end
through_reflection() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 73
def through_reflection; end
through_singular?() click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 49
def through_singular?
  through? && !through_reflection.collection?
end

Protected Instance Methods

get_reverse(klass = nil) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 126
def get_reverse(klass = nil)
  return nil if klass.nil? && polymorphic?

  # name-based matching (association name vs self.active_record.to_s)
  matches = reverse_matches(klass || self.klass)
  if matches.length > 1
    matches.select! do |assoc|
      inverse_klass.name.underscore.include? assoc.name.to_s.pluralize.singularize
    end
  end

  matches.first
end
reflect_on_association(name) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 122
def reflect_on_association(name)
  @association.klass.reflect_on_association(name)
end
reverse_direct_match?(assoc) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 167
def reverse_direct_match?(assoc)
  # skip over has_and_belongs_to_many associations
  return false if assoc.macro == :has_and_belongs_to_many

  if foreign_key.is_a?(Array) || assoc.foreign_key.is_a?(Array) # composite_primary_keys
    assoc.foreign_key == foreign_key
  else
    assoc.foreign_key.to_sym == foreign_key.to_sym
  end
end
reverse_habtm_match?(assoc) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 163
def reverse_habtm_match?(assoc)
  assoc.macro == :has_and_belongs_to_many
end
reverse_match?(assoc) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 148
def reverse_match?(assoc)
  return assoc.name == as if as || assoc.polymorphic?
  return false if assoc.class_name != inverse_klass&.name

  if through?
    reverse_through_match?(assoc)
  elsif habtm?
    reverse_habtm_match?(assoc)
  else
    reverse_direct_match?(assoc)
  end
end
reverse_matches(klass) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 140
def reverse_matches(klass)
  associations = self.class.reflect_on_all_associations(klass)
  # collect associations that point back to this model and use the same foreign_key
  associations.each_with_object([]) do |assoc, reverse_matches|
    reverse_matches << assoc if assoc != @association && reverse_match?(assoc)
  end
end
reverse_through_match?(assoc) click to toggle source
# File lib/active_scaffold/data_structures/association/abstract.rb, line 161
def reverse_through_match?(assoc); end