Class: ArSync::HasManyField

Inherits:
Field
  • Object
show all
Defined in:
lib/ar_sync/field.rb

Direct Known Subclasses

CollectionField

Instance Attribute Summary collapse

Attributes inherited from Field

#name

Instance Method Summary collapse

Methods inherited from Field

#action_convert

Constructor Details

#initialize(name, association: nil, limit: nil, order: nil, propagate_when: nil) ⇒ HasManyField

Returns a new instance of HasManyField.



60
61
62
63
64
65
66
# 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

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



55
56
57
# File 'lib/ar_sync/field.rb', line 55

def association
  @association
end

#limitObject (readonly)

Returns the value of attribute limit.



55
56
57
# File 'lib/ar_sync/field.rb', line 55

def limit
  @limit
end

#orderObject (readonly)

Returns the value of attribute order.



55
56
57
# File 'lib/ar_sync/field.rb', line 55

def order
  @order
end

#propagate_whenObject (readonly)

Returns the value of attribute propagate_when.



55
56
57
# File 'lib/ar_sync/field.rb', line 55

def propagate_when
  @propagate_when
end

Instance Method Details

#data(_parent, child, action:) ⇒ Object



79
80
81
# File 'lib/ar_sync/field.rb', line 79

def data(_parent, child, action:, **)
  child._sync_data new_record: action == :create
end

#order_paramObject



83
84
85
# File 'lib/ar_sync/field.rb', line 83

def order_param
  { limit: limit, order: order } if order
end

#path(child) ⇒ Object



87
88
89
# File 'lib/ar_sync/field.rb', line 87

def path(child)
  [name, child.id]
end

#skip_propagation?(parent, child, _path) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
# 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

#typeObject



56
57
58
# File 'lib/ar_sync/field.rb', line 56

def type
  :many
end