2017-10-24 01:01:08 +00:00
|
|
|
# frozen_string_literal: true
|
2012-01-26 18:53:16 +00:00
|
|
|
module SimpleForm
|
|
|
|
module Inputs
|
2012-01-27 18:30:19 +00:00
|
|
|
class CollectionRadioButtonsInput < CollectionInput
|
2014-03-11 22:41:25 +00:00
|
|
|
def input(wrapper_options = nil)
|
2012-01-26 18:53:16 +00:00
|
|
|
label_method, value_method = detect_collection_methods
|
|
|
|
|
2014-03-11 22:41:25 +00:00
|
|
|
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
2014-03-11 18:22:45 +00:00
|
|
|
|
2015-08-01 06:57:20 +00:00
|
|
|
@builder.send(:"collection_#{input_type}",
|
2012-01-26 18:53:16 +00:00
|
|
|
attribute_name, collection, value_method, label_method,
|
2014-03-11 18:22:45 +00:00
|
|
|
input_options, merged_input_options,
|
2014-03-11 13:30:49 +00:00
|
|
|
&collection_block_for_nested_boolean_style
|
2012-01-26 18:53:16 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def input_options
|
|
|
|
options = super
|
|
|
|
apply_default_collection_options!(options)
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2012-01-26 19:11:10 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def apply_default_collection_options!(options)
|
2012-02-24 21:17:56 +00:00
|
|
|
options[:item_wrapper_tag] ||= options.fetch(:item_wrapper_tag, SimpleForm.item_wrapper_tag)
|
2012-01-27 18:09:45 +00:00
|
|
|
options[:item_wrapper_class] = [
|
|
|
|
item_wrapper_class, options[:item_wrapper_class], SimpleForm.item_wrapper_class
|
2013-11-12 22:18:52 +00:00
|
|
|
].compact.presence if SimpleForm.include_default_input_wrapper_class
|
2012-01-27 18:09:45 +00:00
|
|
|
|
2012-02-24 21:17:56 +00:00
|
|
|
options[:collection_wrapper_tag] ||= options.fetch(:collection_wrapper_tag, SimpleForm.collection_wrapper_tag)
|
2012-01-26 19:11:10 +00:00
|
|
|
options[:collection_wrapper_class] = [
|
|
|
|
options[:collection_wrapper_class], SimpleForm.collection_wrapper_class
|
|
|
|
].compact.presence
|
|
|
|
end
|
2012-01-26 18:53:16 +00:00
|
|
|
|
|
|
|
def collection_block_for_nested_boolean_style
|
|
|
|
return unless nested_boolean_style?
|
|
|
|
|
2012-02-14 01:50:16 +00:00
|
|
|
proc { |builder| build_nested_boolean_style_item_tag(builder) }
|
2012-01-26 18:53:16 +00:00
|
|
|
end
|
2012-01-26 19:15:32 +00:00
|
|
|
|
2012-02-14 01:50:16 +00:00
|
|
|
def build_nested_boolean_style_item_tag(collection_builder)
|
|
|
|
collection_builder.radio_button + collection_builder.text
|
2012-01-27 19:30:10 +00:00
|
|
|
end
|
|
|
|
|
2012-01-27 18:49:06 +00:00
|
|
|
def item_wrapper_class
|
|
|
|
"radio"
|
|
|
|
end
|
|
|
|
|
2012-01-26 21:30:45 +00:00
|
|
|
# Do not attempt to generate label[for] attributes by default, unless an
|
|
|
|
# explicit html option is given. This avoids generating labels pointing to
|
|
|
|
# non existent fields.
|
|
|
|
def generate_label_for_attribute?
|
|
|
|
false
|
|
|
|
end
|
2012-01-26 18:53:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|