class NinjaModel::Associations::Association
Attributes
owner[R]
reflection[R]
target[R]
Public Class Methods
new(owner, reflection)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 9 def initialize(owner, reflection) @target = nil @owner, @reflection = owner, reflection reset end
Public Instance Methods
association_scope()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 53 def association_scope if klass @association_scope ||= AssociationScope.new(self).scope end end
klass()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 70 def klass reflection.klass end
load_target()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 78 def load_target if find_target? @target ||= find_target end loaded! unless loaded? target rescue NinjaModel::RecordNotFound reset end
loaded!()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 32 def loaded! @loaded = true end
loaded?()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 28 def loaded? @loaded end
reload()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 21 def reload reset reset_scope load_target self unless target.nil? end
reset()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 16 def reset @loaded = false @target = nil end
reset_scope()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 59 def reset_scope @association_scope = nil end
scoped()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 45 def scoped target_scope.merge(association_scope) end
set_inverse_instance(record)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 63 def set_inverse_instance(record) if record && invertible_for?(record) inverse = record.association(inverse_reflection_for(record).name) inverse.target = owner end end
stale_target?()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 36 def stale_target? false end
target=(target)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 40 def target=(target) @target = target loaded! end
target_scope()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 49 def target_scope klass.scoped end
Private Instance Methods
association_class()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 141 def association_class @reflection.klass end
build_record(attributes, options)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 94 def build_record(attributes, options) record = reflection.build_association(attributes, options) do |r| attrs = create_scope.except(*r.changed) r.assign_attributes( create_scope.except(*r.changed) ) end end
creation_attributes()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 103 def creation_attributes attributes = {} if options[:through] raise NotImplementedError, "NinjaModel does not support through associations yet." else if reflection.macro.in?([:has_one, :has_many]) attributes[reflection.foreign_key] = owner[reflection.ninja_model_primary_key] if reflection.options[:as] attributes[reflection.type] = owner.class.base_class.name end end attributes end end
find_target?()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 90 def find_target? !loaded? && (!owner.new_record? || foreign_key_present?) && klass end
foreign_key_present?()
click to toggle source
# File lib/ninja_model/associations/association.rb, line 122 def foreign_key_present? false end
inverse_reflection_for(record)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 133 def inverse_reflection_for(record) reflection.inverse_of end
invertible_for?(record)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 137 def invertible_for?(record) inverse_reflection_for(record) end
raise_on_type_mismatch(record)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 126 def raise_on_type_mismatch(record) unless record.is_a?(reflection.klass) || record.is_a?(reflection.class_name.constantize) message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})" raise ActiveRecord::AssociationTypeMismatch, message end end
set_owner_attributes(record)
click to toggle source
# File lib/ninja_model/associations/association.rb, line 118 def set_owner_attributes(record) creation_attributes.each { |key, value| record[key] = value } end