Sha256: c9b7eb43a5a8d29db25a02222db82c83c2d0dc9d582eead4b005fe6c81132af1
Contents?: true
Size: 1.96 KB
Versions: 5
Compression:
Stored size: 1.96 KB
Contents
# encoding: utf-8 module Mongoid #:nodoc: module Associations #:nodoc: # Represents a relational association to a "parent" object. class ReferencedIn < Proxy # Initializing a related association only requires looking up the object # by its id. # # Options: # # document: The +Document+ that contains the relationship. # options: The association +Options+. def initialize(document, options, target = nil) @options = options if target replace(target) else foreign_key = document.send(options.foreign_key) replace(options.klass.find(foreign_key)) unless foreign_key.blank? end extends(options) end # Replaces the target with a new object # # Returns the association proxy def replace(obj) @target = obj self end class << self # Returns the macro used to create the association. def macro :referenced_in end # Perform an update of the relationship of the parent and child. This # will assimilate the child +Document+ into the parent's object graph. # # Options: # # target: The target(parent) object # document: The +Document+ to update. # options: The association +Options+ # # Example: # # <tt>ReferencedIn.update(person, game, options)</tt> def update(target, document, options) document.send("#{options.foreign_key}=", target ? target.id : nil) new(document, options, target) end # Validate the options passed to the referenced in macro, to encapsulate # the behavior in this class instead of the associations module. # # Options: # # options: Thank you captain obvious. def validate_options(options = {}) check_dependent_not_allowed!(options) end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems