Skip to content

Instantly share code, notes, and snippets.

@slemiere
Created January 9, 2013 17:45
Show Gist options
  • Save slemiere/4495169 to your computer and use it in GitHub Desktop.
Save slemiere/4495169 to your computer and use it in GitHub Desktop.
module Mongoid
module Document
def instantiate(attrs = nil, criteria_instance_id = nil)
attributes = attrs || {}
doc = allocate
doc.criteria_instance_id = criteria_instance_id
doc.instance_variable_set(:@attributes, attributes)
doc.apply_defaults
IdentityMap.set(doc) if identity_map_set_possible?(criteria_instance_id)
doc.run_callbacks(:initialize) unless doc._initialize_callbacks.empty?
doc
end
def identity_map_set_possible?(criteria_instance_id)
(! _loading_revision?) && Threaded.selection(criteria_instance_id).nil?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment