Module: Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon

Included in:
AnchormodelCheckBoxesInput, AnchormodelInput, AnchormodelRadioButtonsInput
Defined in:
lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb', line 5

def input(wrapper_options = nil)
  unless object.respond_to?(:anchormodel_attributes)
    fail("The form field object does not appear to respond to `anchormodel_attributes`, \
did you `include Anchormodel::ModelMixin` in your `application_record.rb`? Affected object: #{object.inspect}")
  end

  am_attr = object.anchormodel_attributes[@attribute_name]
  unless am_attr
    fail("#{@attribute_name.inspect} does not look like an Anchormodel attribute, is `belongs_to_anchormodel` called in your model? \
Affected object: #{object.inspect}")
  end
  am_class = am_attr.anchormodel_class
  options[:multiple] = true if am_attr.multiple? && options[:multiple] != false # allow overriding

  # Attempt to read selected key from html input options "value", as the caller might not know that this is a select.
  selected_key = input_options[:value]
  if selected_key.blank? && object
    # No selected key override present and a model is present, use the model to find out what to select.
    selected_am = object.send(@attribute_name)
    if am_attr.multiple?
      selected_key = selected_am&.map(&:key)&.map(&:to_s) || []
    else
      selected_key = (selected_am&.key || am_class.all.first).to_s
    end
  end

  options.deep_merge!(
    label_method:  :first,
    value_method:  :second,
    sf_selection_key =>       selected_key
  )

  @collection = collect(am_class.all)

  before_render_input

  super
end