Module: ArSync::TreeSync::InstanceMethods

Defined in:
lib/ar_sync/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#_sync_data(new_record: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ar_sync/instance_methods.rb', line 10

def _sync_data(new_record: false)
  fallbacks = {}
  names = []
  self.class._each_sync_child do |name, info|
    names << name if info.type == :data
    if new_record
      fallbacks[name] = [] if info.type == :many
      fallbacks[name] = nil if info.type == :one
    end
  end
  data = ArSync.serialize self, names
  fallbacks.update data
end

#_sync_notify(action) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/ar_sync/instance_methods.rb', line 2

def _sync_notify(action)
  if self.class._sync_self?
    data = action == :destroy ? nil : _sync_data
    ArSync.sync_tree_send to: self, action: action, path: [], data: data
  end
  _sync_notify_parent action
end

#_sync_notify_parent(action, path: nil, data: nil, order_param: nil, only_to_user: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ar_sync/instance_methods.rb', line 33

def _sync_notify_parent(action, path: nil, data: nil, order_param: nil, only_to_user: nil)
  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
    next unless parent
    next if parent.respond_to?(:destroyed?) && parent.destroyed?
    inverse_name = instance_exec(&inverse_name) if inverse_name.is_a? Proc
    next unless inverse_name
    association_field = parent.class._sync_child_info inverse_name
    next if association_field.skip_propagation? parent, self, path
    action2 = association_field.action_convert action
    only_to_user2 = only_to_user
    if only_to
      to_user = only_to.is_a?(Symbol) ? instance_eval(&only_to) : instance_exec(&only_to)
      next unless to_user
      next if only_to_user && only_to_user != to_user
      only_to_user2 = to_user
    end
    data2 = path || action2 == :destroy ? data : association_field.data(parent, self, to_user: to_user, action: action)
    order_param2 = path ? order_param : association_field.order_param
    path2 = [*association_field.path(self), *path]
    ArSync.sync_tree_send(
      to: parent, action: action2, path: path2, data: data2,
      to_user: only_to_user2,
      ordering: order_param2
    )
    parent._sync_notify_parent action2, path: path2, data: data2, order_param: order_param2, only_to_user: to_user || only_to_user
  end
end

#sync_send_event(type:, relation: nil, to_user: nil, data:) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/ar_sync/instance_methods.rb', line 24

def sync_send_event(type:, relation: nil, to_user: nil, data:)
  path = [*relation]
  event_data = { type: type, data: data }
  if self.class._sync_self?
    ArSync.sync_tree_send to: self, action: :event, path: path, data: event_data, to_user: to_user
  end
  _sync_notify_parent(:event, path: path, data: event_data, only_to_user: to_user)
end