lib/express_templates/components/forms/select.rb in express_templates-0.7.1 vs lib/express_templates/components/forms/select.rb in express_templates-0.8.0
- old
+ new
@@ -1,43 +1,43 @@
module ExpressTemplates
module Components
module Forms
# 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
+ # The :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
+ # 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.
class Select < FormComponent
include OptionSupport
- emits -> {
- div(class: field_wrapper_class) {
- label_tag(label_name, label_text)
- select_tag(*select_tag_args)
- }
+ has_option :options, 'Select options. Can be Array, Hash, or Proc.'
+ has_option :selected, 'The currently selected value; Used when options are supplied. Otherwise the value is taken from the resource.'
+ has_option :include_blank, 'Whether or not to include a blank option.', default: true
+ has_option :select2, 'Use select2 enhanced select box.', default: true
+
+ contains -> {
+ label_tag(label_name, label_text)
+ select_tag(*select_tag_args)
}
def select_tag_args
- args = [field_name_attribute, select_options, select_helper_options]
- args
+ [field_name_attribute, select_options, select_helper_options]
end
def select_options_supplied?
- [Array, Hash, Proc].include?(supplied_component_options[:options].class)
+ [Array, Hash, Proc].include?(config[:options].class)
end
def use_supplied_options
- opts = supplied_component_options[:options]
+ opts = config[:options]
if opts.respond_to?(:call) # can be a proc
opts.call(resource)
else
opts
end
@@ -53,11 +53,11 @@
opt.respond_to?(:id) ? opt.id : opt.to_s]
end
end
def selected_value
- field_options[:selected]||resource.send(field_name)
+ config[:selected]||resource.send(field_name)
end
def options_from_supplied_or_field_values
if select_options_supplied?
helpers.options_for_select(
@@ -102,36 +102,19 @@
else
super
end
end
- 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}
- defaults.merge(supplied_component_options)
- end
-
def select_helper_options
- component_option_names = [:select2, :options, :selected]
- add_select2_class( field_options.reject {|k,v| component_option_names.include?(k)})
+ add_select2_class( input_attributes.merge(include_blank: !!config[:include_blank]) )
end
protected
def add_select2_class(helper_options)
- add_class(helper_options[:class]) if helper_options[:class]
- add_class('select2') if supplied_component_options[:select2] === true
- helper_options[:class] = (class_list - ["select"]).to_s
- helper_options
- end
-
- def supplied_component_options
- if @args.last && @args.last.is_a?(Hash)
- @args.last
- else
- {}
- end
+ classes = (helper_options[:class]||'').split(' ')
+ classes << 'select2' if config[:select2] === true
+ helper_options.merge(:class => classes.join(' '))
end
end
end
end