Class: ExpressTemplates::Components::Forms::Select
Overview
Provides a form Select component based on the Rails select_tag helper.
The :options may be specified as an Array or Hash which will be supplied to the options_for_select helper.
If the :options are omitted, the component attempts to check whether the field is an association. If an association exists, the options will be generated using options_from_collection_for_select with the assumption that :id and :name are the value and name fields on the collection. If no association exists, we use all the existing unique values for the field on the collection to which the resource belongs as the list of possible values for the select.
Constant Summary
Constants inherited
from Base
Base::MAP
Instance Attribute Summary
#input_attributes
Instance Method Summary
collapse
#belongs_to_association, #has_many_through_association, #related_collection
#field_helper_options, #field_id_attribute, #field_name, #field_value, #label_name, #label_text, #parent_form, #resource, #resource_class, #resource_name
included
Methods inherited from Base
abstract_component, abstract_component?, before_build, #build, builder_method, builder_method_and_class, contains, descendants, has_attributes, inherited, #initialize, require_parent, required_parent, tag
Instance Method Details
#field_name_attribute ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/express_templates/components/forms/select.rb', line 103
def field_name_attribute
if has_many_through_association
"#{resource_name.singularize}[#{field_name.singularize}_ids]"
else
super
end
end
|
#generate_options_from_field_values ⇒ Object
46
47
48
|
# File 'lib/express_templates/components/forms/select.rb', line 46
def generate_options_from_field_values
resource.class.distinct(field_name.to_sym).pluck(field_name.to_sym)
end
|
#normalize_for_helper(supplied_options) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/express_templates/components/forms/select.rb', line 50
def normalize_for_helper(supplied_options)
supplied_options.map do |opt|
[opt.respond_to?(:name) ? opt.name : opt.to_s,
opt.respond_to?(:id) ? opt.id : opt.to_s]
end
end
|
#options_from_belongs_to ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/express_templates/components/forms/select.rb', line 76
def options_from_belongs_to
if belongs_to_association.polymorphic?
raise 'No options for Polymorphic association'
else
helpers.options_from_collection_for_select(related_collection, :id, option_name_method, resource.send(field_name))
end
end
|
#options_from_has_many_through ⇒ Object
84
85
86
|
# File 'lib/express_templates/components/forms/select.rb', line 84
def options_from_has_many_through
helpers.options_from_collection_for_select(related_collection, :id, option_name_method, resource.send(field_name).map(&:id))
end
|
#options_from_supplied_or_field_values ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/express_templates/components/forms/select.rb', line 61
def options_from_supplied_or_field_values
if select_options_supplied?
supplied_options = use_supplied_options
if supplied_options.respond_to?(:map)
helpers.options_for_select(
normalize_for_helper(supplied_options),
selected_value)
else
supplied_options
end
else
generate_options_from_field_values
end
end
|
#select_helper_options ⇒ Object
111
112
113
|
# File 'lib/express_templates/components/forms/select.rb', line 111
def select_helper_options
add_select2_class( input_attributes.merge(include_blank: !!config[:include_blank]) )
end
|
#select_options ⇒ Object
Returns the options which will be supplied to the select_tag helper.
93
94
95
96
97
98
99
100
101
|
# File 'lib/express_templates/components/forms/select.rb', line 93
def select_options
if belongs_to_association && !select_options_supplied?
options_from_belongs_to
elsif has_many_through_association
options_from_has_many_through
else
simple_options_with_selection
end
end
|
#select_options_supplied? ⇒ Boolean
33
34
35
|
# File 'lib/express_templates/components/forms/select.rb', line 33
def select_options_supplied?
[Array, Hash, Proc].include?(config[:options].class)
end
|
#select_tag_args ⇒ Object
29
30
31
|
# File 'lib/express_templates/components/forms/select.rb', line 29
def select_tag_args
[field_name_attribute, select_options, select_helper_options]
end
|
#selected_value ⇒ Object
57
58
59
|
# File 'lib/express_templates/components/forms/select.rb', line 57
def selected_value
config[:selected]||resource.send(field_name)
end
|
#simple_options_with_selection ⇒ Object
88
89
90
|
# File 'lib/express_templates/components/forms/select.rb', line 88
def simple_options_with_selection
helpers.options_for_select(options_from_supplied_or_field_values, selected_value)
end
|
#use_supplied_options ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/express_templates/components/forms/select.rb', line 37
def use_supplied_options
opts = config[:options]
if opts.respond_to?(:call) opts.call(resource)
else
opts
end
end
|