Module: FormProps::FormOptionsHelper
- Included in:
- Inputs::CollectionSelect, Inputs::GroupedCollectionSelect, Inputs::Select, Inputs::TimeZoneSelect, Inputs::WeekdaySelect
- Defined in:
- lib/form_props/form_options_helper.rb
Instance Method Summary collapse
- #extract_selected_and_disabled(selected) ⇒ Object
- #extract_values_from_collection(collection, value_method, selected) ⇒ Object
- #grouped_options_for_select(grouped_options, selected_key = nil, options = {}) ⇒ Object
- #option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil) ⇒ Object
- #option_html_attributes(element) ⇒ Object
- #options_for_select(container, selected = nil) ⇒ Object
- #options_from_collection_for_select(collection, value_method, text_method, selected = nil) ⇒ Object
- #selected_values ⇒ Object
- #time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone) ⇒ Object
- #value_for_collection(item, value) ⇒ Object
- #weekday_options_for_select(selected = nil, index_as_value: false, day_format: :day_names, beginning_of_week: Date.beginning_of_week) ⇒ Object
Instance Method Details
#extract_selected_and_disabled(selected) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/form_props/form_options_helper.rb', line 108 def extract_selected_and_disabled(selected) if selected.is_a?(Proc) [selected, nil] else selected = Array.wrap(selected) = selected..symbolize_keys selected_items = .fetch(:selected, selected) [selected_items, [:disabled]] end end |
#extract_values_from_collection(collection, value_method, selected) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/form_props/form_options_helper.rb', line 119 def extract_values_from_collection(collection, value_method, selected) if selected.is_a?(Proc) collection.map do |element| element.public_send(value_method) if selected.call(element) end.compact else selected end end |
#grouped_options_for_select(grouped_options, selected_key = nil, options = {}) ⇒ Object
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 |
# File 'lib/form_props/form_options_helper.rb', line 10 def (, selected_key = nil, = {}) prompt = [:prompt] divider = [:divider] = [] if prompt .push({ label: prompt_text(prompt), value: "" }) end .each do |container| html_attributes = option_html_attributes(container) if divider label = divider else label, container = container end .push({label: label, options: (container, selected_key)} .merge!(html_attributes)) end end |
#option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/form_props/form_options_helper.rb', line 75 def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil) collection.map do |group| = ( value_for_collection(group, group_method), option_key_method, option_value_method, selected_key ) { options: , label: value_for_collection(group, group_label_method) } end end |
#option_html_attributes(element) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/form_props/form_options_helper.rb', line 39 def option_html_attributes(element) if Array === element element.select { |e| Hash === e }.reduce({}, :merge!) else {} end end |
#options_for_select(container, selected = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/form_props/form_options_helper.rb', line 47 def (container, selected = nil) 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) if !html_attributes[:selected] && option_value_selected?(value, selected) selected_values.push(value) end if html_attributes[:selected] selected_values.push(value) end if !html_attributes[:disabled] && (disabled && option_value_selected?(value, disabled)) html_attributes[:disabled] = true end html_attributes[:value] = value html_attributes[:label] = text html_attributes end end |
#options_from_collection_for_select(collection, value_method, text_method, selected = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/form_props/form_options_helper.rb', line 91 def (collection, value_method, text_method, selected = nil) = collection.map do |element| [value_for_collection(element, text_method), value_for_collection(element, value_method), option_html_attributes(element)] end selected, disabled = extract_selected_and_disabled(selected) select_deselect = { selected: extract_values_from_collection(collection, value_method, selected), disabled: extract_values_from_collection(collection, value_method, disabled) } (, select_deselect) end |
#selected_values ⇒ Object
5 6 7 8 |
# File 'lib/form_props/form_options_helper.rb', line 5 def selected_values @selected_values ||= [] @selected_values end |
#time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/form_props/form_options_helper.rb', line 129 def (selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone) = [] zones = model.all convert_zones = lambda { |list| list.map { |z| [z.to_s, z.name] } } if priority_zones if priority_zones.is_a?(Regexp) priority_zones = zones.select { |z| z.match?(priority_zones) } end .concat((convert_zones[priority_zones], selected)) .push({label: "-------------", value: "", disabled: true}) zones -= priority_zones end .concat((convert_zones[zones], selected)) end |
#value_for_collection(item, value) ⇒ Object
104 105 106 |
# File 'lib/form_props/form_options_helper.rb', line 104 def value_for_collection(item, value) value.respond_to?(:call) ? value.call(item) : item.public_send(value) end |
#weekday_options_for_select(selected = nil, index_as_value: false, day_format: :day_names, beginning_of_week: Date.beginning_of_week) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/form_props/form_options_helper.rb', line 150 def (selected = nil, index_as_value: false, day_format: :day_names, beginning_of_week: Date.beginning_of_week) day_names = I18n.translate("date.#{day_format}") day_names = day_names.map.with_index.to_a if index_as_value day_names = day_names.rotate(Date::DAYS_INTO_WEEK.fetch(beginning_of_week)) (day_names, selected) end |