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_field(current_user) ⇒ 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
- #_sync_notify_self_destroy ⇒ Object
- #sync_keys(current_user) ⇒ Object
Instance Method Details
#_serializer_field_value(name) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/ar_sync/instance_methods.rb', line 44 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ar_sync/instance_methods.rb', line 53 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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ar_sync/instance_methods.rb', line 28 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
18 19 20 21 22 23 24 25 26 |
# File 'lib/ar_sync/instance_methods.rb', line 18 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_field(current_user) ⇒ Object
6 7 8 |
# File 'lib/ar_sync/instance_methods.rb', line 6 def _sync_field(current_user) { id:, keys: sync_keys(current_user) } end |
#_sync_notify(action) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/ar_sync/instance_methods.rb', line 10 def _sync_notify(action) _sync_notify_parent action if self.class._sync_self? _sync_notify_self if action == :update _sync_notify_self_destroy if action == :destroy end end |
#_sync_notify_child_added(child, name, to_user) ⇒ Object
115 116 117 |
# File 'lib/ar_sync/instance_methods.rb', line 115 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
119 120 121 |
# File 'lib/ar_sync/instance_methods.rb', line 119 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
111 112 113 |
# File 'lib/ar_sync/instance_methods.rb', line 111 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
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 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ar_sync/instance_methods.rb', line 73 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
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ar_sync/instance_methods.rb', line 123 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 |
#_sync_notify_self_destroy ⇒ Object
136 137 138 |
# File 'lib/ar_sync/instance_methods.rb', line 136 def _sync_notify_self_destroy ArSync.sync_send to: self, action: :destroy, path: :_destroy, model: nil end |
#sync_keys(current_user) ⇒ Object
2 3 4 |
# File 'lib/ar_sync/instance_methods.rb', line 2 def sync_keys(current_user) [ArSync.sync_key(self), ArSync.sync_key(self, current_user)] end |