lib/express_templates/components/forms/select.rb in express_templates-0.7.0 vs lib/express_templates/components/forms/select.rb in express_templates-0.7.1
- old
+ new
@@ -35,23 +35,37 @@
end
def use_supplied_options
opts = supplied_component_options[:options]
if opts.respond_to?(:call) # can be a proc
- opts.call()
+ opts.call(resource)
else
opts
end
end
def generate_options_from_field_values
resource.class.distinct(field_name.to_sym).pluck(field_name.to_sym)
end
+ 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
+
+ def selected_value
+ field_options[:selected]||resource.send(field_name)
+ end
+
def options_from_supplied_or_field_values
if select_options_supplied?
- use_supplied_options
+ helpers.options_for_select(
+ normalize_for_helper(use_supplied_options),
+ selected_value
+ )
else
generate_options_from_field_values
end
end
@@ -66,14 +80,10 @@
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
def simple_options_with_selection
- if selection = field_options[:selected]
- helpers.options_for_select(options_from_supplied_or_field_values, selection)
- else
- helpers.options_for_select(options_from_supplied_or_field_values, resource.send(field_name))
- end
+ helpers.options_for_select(options_from_supplied_or_field_values, selected_value)
end
# Returns the options which will be supplied to the select_tag helper.
def select_options
if belongs_to_association && !select_options_supplied?