Module: Mongoid::Mirrored::MirrorMethods
- Included in:
- ClassMethods
- Defined in:
- lib/mongoid-mirrored/mirror/mirror_methods.rb
Instance Method Summary collapse
- #define_after_create_callback ⇒ Object
- #define_after_create_siblings ⇒ Object
- #define_after_destroy_callback ⇒ Object
- #define_after_destroy_siblings ⇒ Object
- #define_after_update_callback ⇒ Object
- #define_after_update_siblings ⇒ Object
- #define_mirror_attributes ⇒ Object
-
#define_mirror_callbacks ⇒ Object
Define callbacks for mirror class that don’t trigger callbacks on the root class.
- #embeds_mirror ⇒ Object
Instance Method Details
#define_after_create_callback ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 30 def define_after_create_callback unless mirror_klass.instance_methods.include?(:_create_root) mirror_klass.class_eval <<-EOF after_create :_create_root EOF end # Comment.collection.insert(attributes.merge(:post_id => post.id)) mirror_klass.class_eval <<-EOF def _create_root mirror_attributes[:root][:klass].collection.insert(attributes.merge(mirror_attributes[:embedding][:id] => eval(mirror_attributes[:embedding][:instance]).id)) end EOF end |
#define_after_create_siblings ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 75 def define_after_create_siblings unless mirror_klass.instance_methods.include?(:_create_siblings) mirror_klass.class_eval <<-EOF after_create :_create_siblings EOF end mirror_klass.class_eval <<-EOF def _create_siblings mirror_attributes[:embedding][:models].each do |sibling| _sibling = sibling.to_s next if sibling == mirror_attributes[:embedding][:sym] sibling_klass = sibling.to_s.classify.constantize if self[_sibling+"_id"] sibling_klass.collection.update( { :_id => self[_sibling+"_id"] }, '$push' => { mirror_attributes[:root][:association] => attributes.merge(mirror_attributes[:embedding][:id] => eval(mirror_attributes[:embedding][:instance]).id) } ) end end end EOF end |
#define_after_destroy_callback ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 60 def define_after_destroy_callback unless mirror_klass.instance_methods.include?(:_destroy_root) mirror_klass.class_eval <<-EOF after_destroy :_destroy_root EOF end # Comment.collection.remove({ :id => id}, '$set' => attributes) mirror_klass.class_eval <<-EOF def _destroy_root #{@root_klass}.collection.remove({ :_id => id }) end EOF end |
#define_after_destroy_siblings ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 125 def define_after_destroy_siblings unless mirror_klass.instance_methods.include?(:_destroy_siblings) mirror_klass.class_eval <<-EOF after_destroy :_destroy_siblings EOF end mirror_klass.class_eval <<-EOF def _destroy_siblings mirror_attributes[:embedding][:models].each do |sibling| _sibling = sibling.to_s next if sibling == mirror_attributes[:embedding][:sym] sibling_klass = sibling.to_s.classify.constantize if self[_sibling+"_id"] sibling_klass.collection.update({ :_id => self[_sibling+"_id"] }, '$pull' => { mirror_attributes[:root][:association] => { :_id => id}}) end end end EOF end |
#define_after_update_callback ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 45 def define_after_update_callback unless mirror_klass.instance_methods.include?(:_update_root) mirror_klass.class_eval <<-EOF after_update :_update_root EOF end # Comment.collection.update({ :_id => id}, '$set' => attributes.except('_id')) mirror_klass.class_eval <<-EOF def _update_root #{@root_klass}.collection.update({ :_id => id }, '$set' => attributes.except('_id')) end EOF end |
#define_after_update_siblings ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 101 def define_after_update_siblings unless mirror_klass.instance_methods.include?(:_update_siblings) mirror_klass.class_eval <<-EOF after_update :_update_siblings EOF end mirror_klass.class_eval <<-EOF def _update_siblings #{[:all]}.each do |sibling| _sibling = sibling.to_s next if sibling == mirror_attributes[:embedding][:sym] sibling_klass = sibling.to_s.classify.constantize if self[_sibling+"_id"] nested_attr = {} attributes.except(mirror_attributes[:embedding][:id]).each_pair do |k,v| nested_attr[mirror_attributes[:root][:association].to_s + ".$." + k] = v end sibling_klass.collection.update({mirror_attributes[:root][:association].to_s+"._id" => id}, '$set' => nested_attr ) end end end EOF end |
#define_mirror_attributes ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 5 def define_mirror_attributes mirror_klass.class_eval <<-EOF cattr_accessor :mirror_attributes self.mirror_attributes = { :root => {:association => :#{root_association}, :klass => #{self}}, :embedding => { :sym => :#{[:current]}, :id => :#{[:current]}_id, :instance => "#{[:current]}", :klass => #{symbol_to_class([:current])}, :models => #{[:all]} } } EOF end |
#define_mirror_callbacks ⇒ Object
Define callbacks for mirror class that don’t trigger callbacks on the root class
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 146 def define_mirror_callbacks if [:both, :from_mirror].include?([:sync_direction]) if [:sync_events].include?(:create) || [:sync_events] == [:all] define_after_create_callback if [:replicate_to_siblings] && [:all].size > 1 define_after_create_siblings end end if [:sync_events].include?(:update) || [:sync_events] == [:all] define_after_update_callback if [:replicate_to_siblings] && [:all].size > 1 define_after_update_siblings end end if [:sync_events].include?(:destroy) || [:sync_events] == [:all] define_after_destroy_callback if [:replicate_to_siblings] && [:all].size > 1 define_after_destroy_siblings end end end end |
#embeds_mirror ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/mongoid-mirrored/mirror/mirror_methods.rb', line 21 def # mongoid macro embedded_in # eg: embedded_in :post, :inverse_of => :comments mirror_klass.class_eval <<-EOT embedded_in mirror_attributes[:embedding][:sym], :inverse_of => mirror_attributes[:root][:association] EOT end |