class ArSync::HasManyField

Attributes

association[R]
limit[R]
order[R]
propagate_when[R]

Public Class Methods

new(name, association: nil, limit: nil, order: nil, propagate_when: nil) click to toggle source
Calls superclass method ArSync::Field::new
# File lib/ar_sync/field.rb, line 60
def initialize(name, association: nil, limit: nil, order: nil, propagate_when: nil)
  super name
  @limit = limit
  @order = order
  @association = association || name
  @propagate_when = propagate_when
end

Public Instance Methods

data(_parent, child, action:, **) click to toggle source
# File lib/ar_sync/field.rb, line 79
def data(_parent, child, action:, **)
  child._sync_data new_record: action == :create
end
order_param() click to toggle source
# File lib/ar_sync/field.rb, line 83
def order_param
  { limit: limit, order: order } if order
end
path(child) click to toggle source
# File lib/ar_sync/field.rb, line 87
def path(child)
  [name, child.id]
end
skip_propagation?(parent, child, _path) click to toggle source
# File lib/ar_sync/field.rb, line 68
def skip_propagation?(parent, child, _path)
  return false unless limit
  return !propagate_when.call(child) if propagate_when
  ids = parent.send(association).order(id: order).limit(limit).ids
  if child.destroyed?
    ids.size == limit && (order == :asc ? ids.max < child.id : child.id < ids.min)
  else
    !ids.include? child.id
  end
end
type() click to toggle source
# File lib/ar_sync/field.rb, line 56
def type
  :many
end