lib/express_templates/components/forms/radio.rb in express_templates-0.11.8 vs lib/express_templates/components/forms/radio.rb in express_templates-0.11.9
- old
+ new
@@ -4,10 +4,15 @@
class Radio < FormComponent
include OptionSupport
has_option :options, 'Supplies a list of options for the radio tag'
has_option :label_wrapper_class, 'Specify a class for labels which wrap each radio option.'
+ has_option(
+ :label_after,
+ "Specify (true of false) if the label should appear after the input (they will be siblings)",
+ default: false
+ )
contains -> {
label_tag(label_name, label_text)
if option_values_specified?
generate_options_from_specified_values
@@ -33,31 +38,50 @@
def option_collection
config[:options]
end
def generate_options_from_specified_values
- case
- when option_collection.kind_of?(Array)
- option_collection.each_with_index do |option, index|
- label(class: config[:label_wrapper_class]) {
- radio_button(resource_name, field_name.to_sym, option, class: 'radio')
- current_arbre_element.add_child option
- }
+ if !option_collection.kind_of?(Array) &&
+ !option_collection.kind_of?(Hash)
+ raise ArgumentError, "Radio collection should be Array or Hash: #{option_collection.inspect}"
+ end
+
+ option_collection_hash = option_collection
+ if option_collection.kind_of?(Array)
+ option_collection_hash = option_collection.inject({}) do |hash, val|
+ hash[val] = val
+ hash
end
- when option_collection.kind_of?(Hash)
- option_collection.each_pair do |key, value|
+ end
+
+ option_collection_hash.each_pair do |key, value|
+ if label_after?
+ div(class: "radio-input-container") {
+ radio_button(resource_name, field_name.to_sym, key, class: 'radio')
+ label({
+ class: config[:label_wrapper_class],
+ for: "#{resource_name}_#{field_name}_#{key}",
+ }) {
+ current_arbre_element.add_child value
+ }
+ }
+ else
label(class: config[:label_wrapper_class]) {
radio_button(resource_name, field_name.to_sym, key, class: 'radio')
current_arbre_element.add_child value
}
end
- else
- raise "Radio collection should be Array or Hash: #{option_collection.inspect}"
end
end
def collection_from_association
related_collection or raise "No association collection for: #{resource_name}.#{field_name}"
+ end
+
+ protected
+
+ def label_after?
+ !!config[:label_after]
end
end
end
end