Module: Hibiki::Rails::NestedActions

Defined in:
lib/hibiki/rails/nested_actions.rb

Overview

Generic channel actions for reactive_nested forms — include next to Hibiki::Rails::Channel on channels that render nested fieldsets. Opt-in on purpose: every public method here becomes a client-invocable action.

A control addresses a node with path, alternating association names and child keys, any depth:

"credits"                       # the collection (add targets)
"credits/c3"                    # a child      (remove/set_field)
"credits/c3/contributions/n1"   # a grandchild

plus dom naming which open form the write aims at ("song_7" vs "song_new"), resolved by #nested_form_for — override it if the channel holds its forms in something other than the scaffold's @form/@editing_id and @new_form/@creating ivars.

Every hop is gated: the association against the form class's reactive_nested declarations, the key against live children, a field against the child class's hibiki_attributes. Anything stale or forged drops the action.

Instance Method Summary collapse

Instance Method Details

#nested_add(data) ⇒ Object



27
28
29
30
# File 'lib/hibiki/rails/nested_actions.rb', line 27

def nested_add(data)
  owner, name, child = __hibiki_nested_target(data)
  owner.nested_add(name) if owner && child.nil?
end

#nested_move(data) ⇒ Object

to is the index among the child's visible siblings; the form clamps it, so any integer is safe.



39
40
41
42
43
44
# File 'lib/hibiki/rails/nested_actions.rb', line 39

def nested_move(data)
  owner, name, child = __hibiki_nested_target(data)
  return unless child && !data["to"].nil?

  owner.nested_move(name, child, to: data["to"].to_i)
end

#nested_remove(data) ⇒ Object



32
33
34
35
# File 'lib/hibiki/rails/nested_actions.rb', line 32

def nested_remove(data)
  owner, name, child = __hibiki_nested_target(data)
  owner.nested_remove(name, child) if child
end

#nested_set_field(data) ⇒ Object

The changed control's value arrives under its own name, which the views build as "#path/#field" — per-child unique, and inert in a submit payload (assign only reads declared scalars).



49
50
51
52
53
54
55
56
57
58
# File 'lib/hibiki/rails/nested_actions.rb', line 49

def nested_set_field(data)
  _owner, _name, child = __hibiki_nested_target(data)
  return unless child

  field = data["field"].to_s.to_sym
  return unless child.class.hibiki_attributes.include?(field)

  key = "#{data['path']}/#{data['field']}"
  child.public_send(:"#{field}=", data[key]) if data.key?(key)
end