Class: ArSync::HasManyField
Direct Known Subclasses
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#propagate_when ⇒ Object
readonly
Returns the value of attribute propagate_when.
Attributes inherited from Field
Instance Method Summary collapse
- #data(_parent, child, action:) ⇒ Object
-
#initialize(name, association: nil, limit: nil, order: nil, propagate_when: nil) ⇒ HasManyField
constructor
A new instance of HasManyField.
- #order_param ⇒ Object
- #path(child) ⇒ Object
- #skip_propagation?(parent, child, _path) ⇒ Boolean
- #type ⇒ Object
Methods inherited from Field
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
#association ⇒ Object (readonly)
Returns the value of attribute association.
55 56 57 |
# File 'lib/ar_sync/field.rb', line 55 def association @association end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
55 56 57 |
# File 'lib/ar_sync/field.rb', line 55 def limit @limit end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
55 56 57 |
# File 'lib/ar_sync/field.rb', line 55 def order @order end |
#propagate_when ⇒ Object (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_param ⇒ Object
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
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 |
#type ⇒ Object
56 57 58 |
# File 'lib/ar_sync/field.rb', line 56 def type :many end |