Module: ArSync::ModelBase::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_current_watch_values ⇒ Object
- #_sync_notify(action) ⇒ Object
- #_sync_notify_child_added(child, name, to_user) ⇒ Object
- #_sync_notify_child_changed(name, to_user) ⇒ Object
- #_sync_notify_child_removed(child, name, to_user) ⇒ Object
- #_sync_notify_parent(action) ⇒ Object
- #_sync_notify_self ⇒ Object
Instance Method Details
#_serializer_field_value(name) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/ar_sync/instance_methods.rb', line 33 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ar_sync/instance_methods.rb', line 42 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
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ar_sync/instance_methods.rb', line 17 def _sync_current_parents_info parents = [] self.class._each_sync_parent do |parent, inverse_name:, only_to:, watch:| 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 inverse_name = instance_exec(&inverse_name) if inverse_name.is_a? Proc owned = parent.class._sync_child_info(inverse_name).present? if parent parents << [parent, [inverse_name, to_user, owned, watch]] end parents end |
#_sync_current_watch_values ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/ar_sync/instance_methods.rb', line 7 def _sync_current_watch_values values = {} self.class._each_sync_parent do |_, info| [*info[:watch]].each do |watch| values[watch] = watch.is_a?(Proc) ? instance_exec(&watch) : __send__(watch) end end values end |
#_sync_notify(action) ⇒ Object
2 3 4 5 |
# File 'lib/ar_sync/instance_methods.rb', line 2 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) ⇒ Object
104 105 106 |
# File 'lib/ar_sync/instance_methods.rb', line 104 def _sync_notify_child_added(child, name, to_user) ArSync.sync_send to: self, action: :add, model: child, path: name, to_user: to_user end |
#_sync_notify_child_changed(name, to_user) ⇒ Object
108 109 110 |
# File 'lib/ar_sync/instance_methods.rb', line 108 def _sync_notify_child_changed(name, to_user) ArSync.sync_send to: self, action: :update, model: self, field: name, to_user: to_user end |
#_sync_notify_child_removed(child, name, to_user) ⇒ Object
100 101 102 |
# File 'lib/ar_sync/instance_methods.rb', line 100 def _sync_notify_child_removed(child, name, to_user) ArSync.sync_send to: self, action: :remove, model: child, path: name, to_user: to_user end |
#_sync_notify_parent(action) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ar_sync/instance_methods.rb', line 62 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 return unless parents_was parents = parents_was.map { nil } else parents_was = _sync_parents_info_before_mutation return unless parents_was parents = _sync_current_parents_info column_values_was = _sync_watch_values_before_mutation || {} column_values = _sync_current_watch_values end parents_was.zip(parents).each do |(parent_was, info_was), (parent, info)| name, to_user, owned, watch = info name_was, to_user_was, owned_was = info_was if parent_was != parent || info_was != info if owned_was parent_was&._sync_notify_child_removed self, name_was, to_user_was else parent_was&._sync_notify_child_changed name_was, to_user_was end if owned parent&._sync_notify_child_added self, name, to_user else parent&._sync_notify_child_changed name, to_user end elsif parent changed = [*watch].any? do |w| column_values_was[w] != column_values[w] end parent._sync_notify_child_changed name, to_user if changed end end end |
#_sync_notify_self ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ar_sync/instance_methods.rb', line 112 def _sync_notify_self belongs_was = _sync_belongs_to_info_before_mutation return unless belongs_was 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 if value.is_a? ArSerializer::Serializable _sync_notify_child_removed value, name, nil if value.nil? end ArSync.sync_send to: self, action: :update, model: self end |