Module: ArSync::GraphSync::ClassMethods

Includes:
ClassMethodsBase
Defined in:
lib/ar_sync/class_methods.rb

Instance Method Summary collapse

Methods included from ClassMethodsBase

#_each_sync_child, #_each_sync_parent, #_sync_child_info, #_sync_children_info, #_sync_parents_info, #_sync_self?, #sync_parent

Instance Method Details

#_initialize_sync_callbacksObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ar_sync/class_methods.rb', line 175

def _initialize_sync_callbacks
  return if instance_variable_defined? '@_sync_callbacks_initialized'
  @_sync_callbacks_initialized = true
  mod = Module.new do
    def write_attribute(attr_name, value)
      self.class.default_scoped.scoping do
        @_sync_parents_info_before_mutation ||= _sync_current_parents_info
        @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
      end
      super attr_name, value
    end
  end
  prepend mod
  attr_reader :_sync_parents_info_before_mutation, :_sync_belongs_to_info_before_mutation

  _sync_define :id

  _sync_define :sync_keys, type: [:string] do |current_user|
    ArSync.sync_graph_keys self, current_user
  end

  _sync_define :defaults, namespace: :sync do |current_user|
    { sync_keys: ArSync.sync_graph_keys(self, current_user) }
  end

  before_destroy do
    @_sync_parents_info_before_mutation ||= _sync_current_parents_info
    @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
  end

  before_save on: :create do
    @_sync_parents_info_before_mutation ||= _sync_current_parents_info
    @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
  end

  %i[create update destroy].each do |action|
    after_commit on: action do
      next if ArSync.skip_notification?
      self.class.default_scoped.scoping { _sync_notify action }
      @_sync_parents_info_before_mutation = nil
      @_sync_belongs_to_info_before_mutation = nil
    end
  end
end

#_sync_define(name, **option, &data_block) ⇒ Object



163
164
165
166
167
# File 'lib/ar_sync/class_methods.rb', line 163

def _sync_define(name, **option, &data_block)
  _initialize_sync_callbacks
  serializer_field name, **option, &data_block unless _serializer_field_info name
  serializer_field name, **option, namespace: :sync, &data_block
end

#_sync_has_many(name, order: :asc, limit: nil, preload: nil, association: nil, **option, &data_block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ar_sync/class_methods.rb', line 136

def _sync_has_many(name, order: :asc, limit: nil, preload: nil, association: nil, **option, &data_block)
  raise "order not in [:asc, :desc] : #{order}" unless %i[asc desc].include? order
  if data_block.nil? && preload.nil?
    underscore_name = name.to_s.underscore.to_sym
    preload = lambda do |records, _context, params|
      ArSerializer::Field.preload_association(
        self, records, association || underscore_name,
        order: (!limit && params && params[:order]) || order,
        limit: [params && params[:limit]&.to_i, limit].compact.min
      )
    end
    data_block = lambda do |preloaded, _context, params|
      records = preloaded ? preloaded[id] || [] : send(name)
      next records unless limit || order == :asc
      ArSync::CollectionWithOrder.new(
        records,
        order: (!limit && params && params[:order]) || order,
        limit: [params && params[:limit]&.to_i, limit].compact.min
      )
    end
    params_type = { limit?: :int, order?: [{ :* => %w[asc desc] }, 'asc', 'desc'] }
  else
    params_type = {}
  end
  _sync_define name, preload: preload, association: association, **option.merge(params_type: params_type), &data_block
end

#sync_collection(name) ⇒ Object



114
115
116
# File 'lib/ar_sync/class_methods.rb', line 114

def sync_collection(name)
  ArSync::Collection::Graph.find self, name
end

#sync_define_collection(name, limit: nil, order: :asc) ⇒ Object



169
170
171
172
173
# File 'lib/ar_sync/class_methods.rb', line 169

def sync_define_collection(name, limit: nil, order: :asc)
  _initialize_sync_callbacks
  collection = ArSync::Collection::Graph.new self, name, limit: limit, order: order
  sync_parent collection, inverse_of: [self, name]
end

#sync_has_data(*names, **option, &data_block) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/ar_sync/class_methods.rb', line 118

def sync_has_data(*names, **option, &data_block)
  @_sync_self = true
  names.each do |name|
    _sync_children_info[name] = nil
    _sync_define name, option, &data_block
  end
end

#sync_has_many(name, **option, &data_block) ⇒ Object



126
127
128
129
# File 'lib/ar_sync/class_methods.rb', line 126

def sync_has_many(name, **option, &data_block)
  _sync_children_info[name] = [:many, option, data_block]
  _sync_has_many name, option, &data_block
end

#sync_has_one(name, **option, &data_block) ⇒ Object



131
132
133
134
# File 'lib/ar_sync/class_methods.rb', line 131

def sync_has_one(name, **option, &data_block)
  _sync_children_info[name] = [:one, option, data_block]
  _sync_define name, option, &data_block
end