class MongoMapper::Plugins::Associations::BelongsToProxy

Public Instance Methods

build(attrs={}, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 21
def build(attrs={}, &block)
  instantiate_target(:new, attrs, &block)
end
create(attrs={}, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 25
def create(attrs={}, &block)
  instantiate_target(:create, attrs, &block)
end
create!(attrs={}, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 29
def create!(attrs={}, &block)
  instantiate_target(:create!, attrs, &block)
end
replace(doc) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 6
def replace(doc)
  if doc
    doc.save if !doc.persisted?
    id = doc.id
  end

  reset
  proxy_owner[association.foreign_key] = id
  unless doc.nil?
    loaded
    @target = doc
  end
  @target
end
save_to_collection(options={}) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 33
def save_to_collection(options={})
  @target.save(options) if @target
end

Protected Instance Methods

find_target() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 39
def find_target
  return nil if proxy_owner[association.foreign_key].nil?
  klass.find_by_id(proxy_owner[association.foreign_key])
end
instantiate_target(instantiator, attrs={}, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 44
def instantiate_target(instantiator, attrs={}, &block)
  @target = klass.send(instantiator, attrs, &block)
  proxy_owner[association.foreign_key] = @target.id
  loaded
  @target
end

Private Instance Methods

stale_target?() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb, line 53
def stale_target?
  loaded? && proxy_owner[association.foreign_key] != @target&.id
end