Class: ExpressTemplates::Components::Forms::Select
- Inherits:
-
FormComponent
- Object
- Expander
- Base
- FormComponent
- ExpressTemplates::Components::Forms::Select
- Includes:
- OptionSupport
- Defined in:
- lib/express_templates/components/forms/select.rb
Overview
Provides a form Select component based on the Rails select_tag helper. Parameters: field_name, select_options, helper_options
Select options may be specified as an Array or Hash which will be supplied to the options_for_select helper.
If the select_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.
Instance Attribute Summary
Attributes inherited from Expander
#handlers, #locals, #stack, #template
Instance Method Summary collapse
- #field_options ⇒ Object
-
#select_options ⇒ Object
Returns the options which will be supplied to the select_tag helper.
Methods included from OptionSupport
#belongs_to_association, #related_collection
Methods inherited from FormComponent
#compile, #field_name, #field_name_attribute, #field_wrapper_class, #label_name, #label_text, #parent_form, #resource_class, #resource_name, #resource_var
Methods included from Capabilities::Adoptable
Methods included from Capabilities::Configurable
Methods inherited from Base
Methods included from Capabilities::Iterating
Methods included from Capabilities::Wrapping
Methods included from Capabilities::Rendering
Methods included from Capabilities::Templating
Methods included from Macro
Methods inherited from Expander
#expand, #initialize, #initialize_expander, #method_missing, #process_children!, register_macros_for
Constructor Details
This class inherits a constructor from ExpressTemplates::Expander
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ExpressTemplates::Expander
Instance Method Details
#field_options ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/express_templates/components/forms/select.rb', line 52 def # If field_otions is omitted the Expander will be # in last or 3rd position and we don't want that defaults = {include_blank: true} = if @args.size > 3 && @args[2].is_a?(Hash) @args[2] else {} end defaults.merge() end |
#select_options ⇒ Object
Returns the options which will be supplied to the select_tag helper.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/express_templates/components/forms/select.rb', line 29 def = [Array, Hash].include?(@args.second.class) && @args.size > 2 if = @args.second else = "@#{resource_var}.class.distinct(:#{field_name}).pluck(:#{field_name})" end if belongs_to_association && ! if belongs_to_association.polymorphic? "{{options_for_select([[]])}}" else "{{options_from_collection_for_select(#{related_collection}, :id, :#{option_name_method}, @#{resource_name}.#{field_name})}}" end else if selection = .delete(:selected) "{{options_for_select(#{options}, \"#{selection}\")}}" else "{{options_for_select(#{options}, @#{resource_name}.#{field_name})}}" end end end |