Method: Compony::ModelFields::Association#value_for

Defined in:
lib/compony/model_fields/association.rb

#value_for(data, link_to_component: nil, link_opts: {}, controller: nil, **_) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/compony/model_fields/association.rb', line 9

def value_for(data, link_to_component: nil, link_opts: {}, controller: nil, **_)
  if link_to_component
    fail('Must pass controller if link_to_component is given.') unless controller
    return transform_and_join(data.send(@name), controller:) do |el|
      next nil if el.nil?
      if Compony.comp_class_for(link_to_component, el)
        next controller.helpers.compony_link(link_to_component, el, **link_opts)
      else
        next el.label
      end
    end
  else
    return transform_and_join(data.send(@name), controller:) { |el| el&.label }
  end
end