class ExpressTemplates::Components::Forms::Select
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.
Public Instance Methods
field_name_attribute()
click to toggle source
Calls superclass method
ExpressTemplates::Components::Forms::FormComponent#field_name_attribute
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
Returns the options which will be supplied to the select_tag helper.
# 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?()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/express_templates/components/forms/select.rb, line 57 def selected_value config[:selected]||resource.send(field_name) end
simple_options_with_selection()
click to toggle source
# 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()
click to toggle source
# File lib/express_templates/components/forms/select.rb, line 37 def use_supplied_options opts = config[:options] if opts.respond_to?(:call) # can be a proc opts.call(resource) else opts end end
Protected Instance Methods
add_select2_class(helper_options)
click to toggle source
# File lib/express_templates/components/forms/select.rb, line 117 def add_select2_class(helper_options) classes = (helper_options[:class]||'').split(' ') classes << 'select2' if config[:select2] === true helper_options.merge(:class => classes.join(' ')) end