Class: ExpressTemplates::Components::Forms::Select

Inherits:
FormComponent show all
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

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

included

Methods included from Capabilities::Configurable

included

Methods inherited from Base

inherited

Methods included from Capabilities::Iterating

included

Methods included from Capabilities::Wrapping

included

Methods included from Capabilities::Rendering

included

Methods included from Capabilities::Templating

included

Methods included from Macro

included

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_optionsObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/express_templates/components/forms/select.rb', line 52

def field_options
  # If field_otions is omitted the Expander will be
  # in last or 3rd position and we don't want that
  defaults = {include_blank: true}
  field_options = if @args.size > 3 && @args[2].is_a?(Hash)
    @args[2]
  else
    {}
  end
  defaults.merge(field_options)
end

#select_optionsObject

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 select_options
  options_specified = [Array, Hash].include?(@args.second.class) && @args.size > 2
  if options_specified
    options = @args.second
  else
    options = "@#{resource_var}.class.distinct(:#{field_name}).pluck(:#{field_name})"
  end

  if belongs_to_association && !options_specified
    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 = field_options.delete(:selected)
      "{{options_for_select(#{options}, \"#{selection}\")}}"
    else
      "{{options_for_select(#{options}, @#{resource_name}.#{field_name})}}"
    end
  end
end