class ActiveAny::Associations::Association

Attributes

inversed[R]
loaded[R]
owner[R]
reflection[R]
target[R]

Public Class Methods

new(owner, reflection) click to toggle source
# File lib/active_any/associations/association.rb, line 8
def initialize(owner, reflection)
  @owner = owner
  @reflection = reflection

  reset
  reset_scope
end

Public Instance Methods

association_scope() click to toggle source
# File lib/active_any/associations/association.rb, line 32
def association_scope
  @association_scope ||= klass ? AssociationScope.scope(self) : nil
end
find_from_target?() click to toggle source
# File lib/active_any/associations/association.rb, line 71
def find_from_target?
  loaded?
end
find_target() click to toggle source
# File lib/active_any/associations/association.rb, line 75
def find_target
  raise NotImplementedError
end
klass() click to toggle source
# File lib/active_any/associations/association.rb, line 43
def klass
  reflection.klass
end
load_target() click to toggle source
# File lib/active_any/associations/association.rb, line 47
def load_target
  @target = find_target if find_target?

  loaded!
  target
  # TODO: implement
  # rescue ActiveRecord::RecordNotFound
  #   reset
end
loaded!() click to toggle source
# File lib/active_any/associations/association.rb, line 79
def loaded!
  @loaded = true
  @inversed = false
end
loaded?() click to toggle source
# File lib/active_any/associations/association.rb, line 67
def loaded?
  @loaded
end
reader() click to toggle source
# File lib/active_any/associations/association.rb, line 16
def reader
  raise NotImplementedError.new, 'reader is unimplemented'
end
reload() click to toggle source
# File lib/active_any/associations/association.rb, line 36
def reload
  reset
  reset_scope
  load_target
  self unless target.nil?
end
reset() click to toggle source
# File lib/active_any/associations/association.rb, line 57
def reset
  @loaded = false
  @stale_target = nil
  @inversed = false
end
reset_scope() click to toggle source
# File lib/active_any/associations/association.rb, line 63
def reset_scope
  @association_scope = nil
end
scope() click to toggle source
# File lib/active_any/associations/association.rb, line 24
def scope
  target_scope.merge!(association_scope)
end
set_inverse_instance(record) click to toggle source
# File lib/active_any/associations/association.rb, line 89
def set_inverse_instance(record)
  if invertible_for?(record)
    inverse = record.association(inverse_reflection_for(record).name)
    inverse.target = owner
    inverse.inversed = true
  end
  record
end
target=(target) click to toggle source
# File lib/active_any/associations/association.rb, line 84
def target=(target)
  @target = target
  loaded!
end
target_scope() click to toggle source
# File lib/active_any/associations/association.rb, line 28
def target_scope
  AssociationRelation.create(klass, self).merge!(klass.all)
end
writer(_records) click to toggle source
# File lib/active_any/associations/association.rb, line 20
def writer(_records)
  raise NotImplementedError.new, 'writer is unimplemented'
end

Private Instance Methods

find_target?() click to toggle source
# File lib/active_any/associations/association.rb, line 108
def find_target?
  !loaded? && (!owner.new_record? || foreign_key_present?) && klass
end
foreign_key_for?(record) click to toggle source
# File lib/active_any/associations/association.rb, line 104
def foreign_key_for?(record)
  record.has_attribute?(reflection.foreign_key)
end
foreign_key_present() click to toggle source
# File lib/active_any/associations/association.rb, line 112
def foreign_key_present
  false
end
inverse_reflection_for(_record) click to toggle source
# File lib/active_any/associations/association.rb, line 116
def inverse_reflection_for(_record)
  reflection.inverse_of
end
invertible_for?(record) click to toggle source
# File lib/active_any/associations/association.rb, line 100
def invertible_for?(record)
  foreign_key_for?(record) && inverse_reflection_for(record)
end