Module: Mongoid::Mirrored::RootMethods
- Included in:
- ClassMethods
- Defined in:
- lib/mongoid-mirrored/root/root_methods.rb
Instance Method Summary collapse
- #define_create_mirrors ⇒ Object
- #define_destroy_mirrors ⇒ Object
-
#define_root_callbacks ⇒ Object
Define callbacks for root class that don’t trigger callbacks on the mirror classes.
- #define_update_mirrors ⇒ Object
-
#write_fields_with_options(&block) ⇒ Object
writes the block passed to the mirrored_in method in the Root Calss defines instance methods used in callbacks triggered by the root documents.
Instance Method Details
#define_create_mirrors ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mongoid-mirrored/root/root_methods.rb', line 21 def define_create_mirrors unless self.instance_methods.include?(:_create_mirrors) self.class_eval <<-EOF after_create :_create_mirrors EOF end define_method :_create_mirrors do # each embedded class will be touched by the callbacks # this should be used with care or write operations could get very slow [:all].each do || # attributes that will be used to define the root and embedding classes and instances = .to_s = self.class.symbol_to_class() = eval() # Only tries to create mirrored document if the embedding instance is given if .collection.update({ :_id => .id }, '$push' => {self.class.root_association => attributes.except("#{}_id")}) end end end end |
#define_destroy_mirrors ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongoid-mirrored/root/root_methods.rb', line 81 def define_destroy_mirrors unless self.instance_methods.include?(:_destroy_mirrors) self.class_eval <<-EOF after_destroy :_destroy_mirrors EOF end # destroys the mirrored document when the destroy method is called on the root document # or when the root document establishes a relationship with another embedding document define_method :_destroy_mirrors do | = nil| [:all].each do || # attributes that will be used to define the root and embedding classes and instances = .to_s = self.class.symbol_to_class() = eval() if id_to_destroy = || .id .collection.update({ :_id => id_to_destroy }, '$pull' => {self.class.root_association => { :_id => id}}) end end end end |
#define_root_callbacks ⇒ Object
Define callbacks for root class that don’t trigger callbacks on the mirror classes
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mongoid-mirrored/root/root_methods.rb', line 5 def define_root_callbacks if [:both, :from_root].include?([:sync_direction]) if [:sync_events].include?(:create) || [:sync_events] == [:all] define_create_mirrors end if [:sync_events].include?(:update) || [:sync_events] == [:all] define_update_mirrors end if [:sync_events].include?(:destroy) || [:sync_events] == [:all] define_destroy_mirrors end end end |
#define_update_mirrors ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid-mirrored/root/root_methods.rb', line 45 def define_update_mirrors unless self.instance_methods.include?(:_update_mirrors) self.class_eval <<-EOF before_update :_update_mirrors EOF end # updates the mirrored document when one or more attributes of the parent document is changed # if the root document changes the embedding document, the mirrored document is deleted from the previous list # and another mirrored document is created for the new embedding document define_method :_update_mirrors do [:all].each do || # attributes that will be used to define the root and embedding classes and instances = .to_s = eval() = self.class.symbol_to_class() if if eval("#{}_id_changed?") _create_mirrors _destroy_mirrors(eval("#{}_id_was")) else # using positional modifier $ to find embedded document to be updated # traverses the attributes hash to inject positional modifier # eg: contents => ;comments.$.contents => nested_attr = {} attributes.except("#{}_id").each_pair do |k,v| nested_attr["#{self.class.root_association}.$.#{k}"] = v end .collection.update({"#{self.class.root_association}._id" => id}, '$set' => nested_attr ) end end end end end |
#write_fields_with_options(&block) ⇒ Object
writes the block passed to the mirrored_in method in the Root Calss defines instance methods used in callbacks triggered by the root documents
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mongoid-mirrored/root/root_methods.rb', line 106 def (&block) index_params = "" index_params << ", :index => true" if [:index] index_params << ", :background => true" if [:index] && [:background_index] [:all].each do || self.class_eval <<-EOT belongs_to :#{} #{index_params} EOT end # writes shared fields and methods to root document yield define_root_callbacks end |