Class: Arara::Tags::Select
- Inherits:
-
ActionView::Helpers::Tags::Base
- Object
- ActionView::Helpers::Tags::Base
- Arara::Tags::Select
- Defined in:
- app/components/arara/tags/select.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#initialize(object_name, method_name, template_object, choices, options, html_options) ⇒ Select
constructor
A new instance of Select.
-
#options_for_select(container, selected = nil) ⇒ Object
my method.
- #render ⇒ Object
- #select_content_tag(option_tags, options, html_options) ⇒ Object
Constructor Details
#initialize(object_name, method_name, template_object, choices, options, html_options) ⇒ Select
Returns a new instance of Select.
6 7 8 9 10 11 12 13 |
# File 'app/components/arara/tags/select.rb', line 6 def initialize(object_name, method_name, template_object, choices, , ) @choices = block_given? ? template_object.capture { yield || "" } : choices @choices = @choices.to_a if @choices.is_a?(Range) @html_options = super(object_name, method_name, template_object, ) end |
Instance Method Details
#options_for_select(container, selected = nil) ⇒ Object
my method
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/components/arara/tags/select.rb', line 63 def (container, selected = nil) return container if String === container selected, disabled = extract_selected_and_disabled(selected).map do |r| Array(r).map(&:to_s) end container.map do |element| html_attributes = option_html_attributes(element) text, value = option_text_and_value(element).map(&:to_s) html_attributes[:selected] ||= option_value_selected?(value, selected) html_attributes[:disabled] ||= disabled && option_value_selected?(value, disabled) html_attributes[:value] = value # also set the label html_attributes[:label] = text # and render our component @template_object.template.render(Arara::SelectItemComponent, html_attributes.symbolize_keys) end.join("\n").html_safe end |
#render ⇒ Object
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 |
# File 'app/components/arara/tags/select.rb', line 15 def render = { selected: @options.fetch(:selected) { value.to_s }, disabled: @options[:disabled] } = if grouped_choices? (@choices, ) else (@choices, ) end if [:selected] selected_element = @choices.select { |x| x[1].to_s == [:selected] }.first if selected_element selected_label = selected_element[0].to_s selected_value = selected_element[1].to_s end @options.merge!( selected_value: selected_value, selected_label: selected_label, ) end select_content_tag(, @options, @html_options) end |
#select_content_tag(option_tags, options, html_options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/components/arara/tags/select.rb', line 86 def select_content_tag(, , ) = .stringify_keys add_default_name_and_id() if placeholder_required?() raise ArgumentError, "include_blank cannot be false for a required field." if [:include_blank] == false [:include_blank] ||= true unless [:prompt] end value = .fetch(:selected) { value() } select = content_tag("select", (, , value), ) if ["multiple"] && .fetch(:include_hidden, true) tag("input", disabled: ["disabled"], name: ["name"], type: "hidden", value: "") + select else select end = .merge() @template_object.template.render(Arara::SelectComponent, .symbolize_keys) do end end |