Module: ArSync::GraphSync::InstanceMethods
- Defined in:
- lib/ar_sync/instance_methods.rb
Instance Method Summary collapse
- #_serializer_field_value(name) ⇒ Object
- #_sync_current_belongs_to_info ⇒ Object
- #_sync_current_parents_info ⇒ Object
- #_sync_notify(action) ⇒ Object
- #_sync_notify_child_added(child, name, to_user, owned) ⇒ Object
- #_sync_notify_child_changed(_child, _name, to_user, owned) ⇒ Object
- #_sync_notify_child_removed(child, name, to_user, owned) ⇒ Object
- #_sync_notify_parent(action) ⇒ Object
- #_sync_notify_self ⇒ Object
Instance Method Details
#_serializer_field_value(name) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/ar_sync/instance_methods.rb', line 85 def _serializer_field_value(name) field = self.class._serializer_field_info name preloadeds = field.preloaders.map do |preloader| args = [[self], nil, {}] preloader.call(*(preloader.arity < 0 ? args : args.take(preloader.arity))) end instance_exec(*preloadeds, nil, {}, &field.data_block) end |
#_sync_current_belongs_to_info ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ar_sync/instance_methods.rb', line 94 def _sync_current_belongs_to_info belongs = {} self.class._each_sync_child do |name, (type, option, data_block)| next unless type == :one option ||= {} association_name = (option[:association] || name).to_s.underscore association = self.class.reflect_on_association association_name next if association && !association.belongs_to? if association && !option[:preload] && !data_block belongs[name] = { type: association.foreign_type && self[association.foreign_type], id: self[association.foreign_key] } else belongs[name] = { value: _serializer_field_value(name) } end end belongs end |
#_sync_current_parents_info ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ar_sync/instance_methods.rb', line 70 def _sync_current_parents_info parents = [] self.class._each_sync_parent do |parent, inverse_name:, only_to:| parent = send parent if parent.is_a? Symbol parent = instance_exec(&parent) if parent.is_a? Proc if only_to to_user = only_to.is_a?(Symbol) ? instance_eval(&only_to) : instance_exec(&only_to) parent = nil unless to_user end owned = parent.class._sync_child_info(inverse_name).present? if parent parents << [parent, [inverse_name, to_user, owned]] end parents end |
#_sync_notify(action) ⇒ Object
65 66 67 68 |
# File 'lib/ar_sync/instance_methods.rb', line 65 def _sync_notify(action) _sync_notify_parent action _sync_notify_self if self.class._sync_self? && action == :update end |
#_sync_notify_child_added(child, name, to_user, owned) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/ar_sync/instance_methods.rb', line 143 def _sync_notify_child_added(child, name, to_user, owned) if owned ArSync.sync_graph_send to: self, action: :add, model: child, path: name, to_user: to_user else ArSync.sync_graph_send to: self, action: :update, model: self, to_user: to_user end end |
#_sync_notify_child_changed(_child, _name, to_user, owned) ⇒ Object
151 152 153 154 |
# File 'lib/ar_sync/instance_methods.rb', line 151 def _sync_notify_child_changed(_child, _name, to_user, owned) return if owned ArSync.sync_graph_send(to: self, action: :update, model: self, to_user: to_user) end |
#_sync_notify_child_removed(child, name, to_user, owned) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/ar_sync/instance_methods.rb', line 135 def _sync_notify_child_removed(child, name, to_user, owned) if owned ArSync.sync_graph_send to: self, action: :remove, model: child, path: name, to_user: to_user else ArSync.sync_graph_send to: self, action: :update, model: self, to_user: to_user end end |
#_sync_notify_parent(action) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ar_sync/instance_methods.rb', line 114 def _sync_notify_parent(action) if action == :create parents = _sync_current_parents_info parents_was = parents.map { nil } elsif action == :destroy parents_was = _sync_parents_info_before_mutation parents = parents_was.map { nil } else parents_was = _sync_parents_info_before_mutation parents = _sync_current_parents_info end parents_was.zip(parents).each do |(parent_was, info_was), (parent, info)| if parent_was == parent && info_was == info parent&._sync_notify_child_changed self, *info else parent_was&._sync_notify_child_removed self, *info_was parent&._sync_notify_child_added self, *info end end end |
#_sync_notify_self ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ar_sync/instance_methods.rb', line 156 def _sync_notify_self belongs_was = _sync_belongs_to_info_before_mutation belongs = _sync_current_belongs_to_info belongs.each do |name, info| next if belongs_was[name] == info value = info.key?(:value) ? info[:value] : _serializer_field_value(name) _sync_notify_child_added value, name, nil, true if value.is_a? ArSerializer::Serializable _sync_notify_child_removed value, name, nil, true if value.nil? end ArSync.sync_graph_send(to: self, action: :update, model: self) end |