class NoBrainer::Document::Association::HasMany

Public Instance Methods

before_destroy_callback() click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 122
def before_destroy_callback
  case metadata.options[:dependent]
  when :destroy  then dependent_criteria.destroy_all
  when :delete   then dependent_criteria.delete_all
  when :nullify  then dependent_criteria.update_all(foreign_key => nil)
  when :restrict then raise NoBrainer::Error::ChildrenExist unless dependent_criteria.empty?
  end
end
dependent_criteria() click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 118
def dependent_criteria
  target_criteria.unscoped
end
loaded?() click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 92
def loaded?
  target_criteria.cached?
end
preload(new_targets) click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 96
def preload(new_targets)
  set_inverses_of(new_targets)
  target_criteria._override_cache(new_targets)
end
read() click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 83
def read
  target_criteria
end
set_inverse_proc() click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 114
def set_inverse_proc
  ->(target){ set_inverses_of([target]) if target.is_a?(NoBrainer::Document) }
end
set_inverses_of(new_targets) click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 101
def set_inverses_of(new_targets)
  @inverses ||= metadata.inverses
  return if @inverses.blank?

  new_targets.each do |target|
    # We don't care if target is a parent class where the inverse association
    # is defined, we set the association regardless.
    # The user won't be able to access it since the association accessors are
    # not defined on the parent class.
    @inverses.each { |inverse| target.associations[inverse].preload(self.owner) }
  end
end
target_criteria() click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 69
def target_criteria
  @target_criteria ||= begin
    query_criteria = { foreign_key => owner.__send__(primary_key) }

    if metadata.options[:as]
      query_criteria = query_criteria.merge(
        foreign_type => owner.root_class.name
      )
    end

    base_criteria.where(query_criteria).after_find(set_inverse_proc)
  end
end
write(new_children) click to toggle source
# File lib/no_brainer/document/association/has_many.rb, line 87
def write(new_children)
  raise "You can't assign `#{target_name}'. " \
        "Instead, you must modify delete and create `#{target_model}' manually."
end