Module: FormProps::Inputs::CollectionHelpers

Included in:
CollectionCheckBoxes, CollectionRadioButtons
Defined in:
lib/form_props/inputs/collection_helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_html_options_for_collection(item, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/form_props/inputs/collection_helpers.rb', line 17

def default_html_options_for_collection(item, value)
  html_options = @html_options.dup

  [:checked, :selected, :disabled, :read_only].each do |option|
    current_value = @options[option]
    next if current_value.nil?

    accept = if current_value.respond_to?(:call)
      current_value.call(item)
    else
      Array(current_value).map(&:to_s).include?(value.to_s)
    end

    if accept
      html_options[option] = true
    elsif option == :checked
      html_options[option] = false
    end
  end

  html_options[:object] = @object
  html_options
end

#hidden_field_nameObject



48
49
50
# File 'lib/form_props/inputs/collection_helpers.rb', line 48

def hidden_field_name
  @html_options[:name] || tag_name(false, @options[:index]).to_s
end

#render_collectionObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/form_props/inputs/collection_helpers.rb', line 6

def render_collection
  json.array! @collection do |item|
    value = value_for_collection(item, @value_method)
    text = value_for_collection(item, @text_method)
    default_html_options = default_html_options_for_collection(item, value)
    additional_html_options = option_html_attributes(item)

    yield item, value, text, default_html_options.merge(additional_html_options)
  end
end

#render_collection_for(builder_class, &block) ⇒ Object



41
42
43
44
45
46
# File 'lib/form_props/inputs/collection_helpers.rb', line 41

def render_collection_for(builder_class, &block)
  render_collection do |item, value, text, default_html_options|
    builder = instantiate_builder(builder_class, item, value, text, default_html_options)
    builder.render
  end
end