From ffecbe07d391ebb90d1ebc755d329fcb84174ad4 Mon Sep 17 00:00:00 2001 From: Bert Goethals Date: Thu, 28 Nov 2013 17:04:53 +0100 Subject: [PATCH] Consistent usage of setting component attributes through the wrapper api: mostly merging issues --- lib/simple_form/components/label_input.rb | 15 ++++++++++++--- lib/simple_form/inputs/base.rb | 4 ++-- lib/simple_form/wrappers/many.rb | 4 ++-- lib/simple_form/wrappers/root.rb | 2 +- lib/simple_form/wrappers/single.rb | 4 ++-- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/simple_form/components/label_input.rb b/lib/simple_form/components/label_input.rb index e9e5819bb..220f005f2 100644 --- a/lib/simple_form/components/label_input.rb +++ b/lib/simple_form/components/label_input.rb @@ -18,12 +18,21 @@ def label_input # Get the options given to the label_input component and apply to both # label and input components. def apply_label_input_options(options) + return unless options[:label_input_html] + label_input_defaults = options[:label_input_html].dup + + specific_defaults = { + label_html: label_input_defaults.delete(:label_html) || {}, + input_html: label_input_defaults.delete(:input_html) || {} + } + [:input_html, :label_html].each do |key| - if options.has_key?(key) - options[key].reverse_merge! options.fetch(:label_input_html, {}) - end + options[key] ||= {} + options[key] = specific_defaults[key].deep_merge(options[key]) + options[key] = label_input_defaults.deep_merge(options[key]) end end + end end end diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 923137c73..22175334c 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -66,7 +66,7 @@ def initialize(builder, attribute_name, column, input_type, options = {}) @input_html_classes = @html_classes.dup @input_html_options = {} - + if SimpleForm.input_class && !input_html_classes.empty? input_html_classes << SimpleForm.input_class end @@ -135,7 +135,7 @@ def reflection_or_attribute_name def html_options_for(namespace, css_classes) html_options = options[:"#{namespace}_html"] html_options = html_options ? html_options.dup : {} - html_options.reverse_merge!(@builder.wrapper.options[:"#{namespace}_html"] || {}) + html_options = (@builder.wrapper.options[:"#{namespace}_html"] || {}).deep_merge(html_options) css_classes << html_options[:class] if html_options.key?(:class) html_options[:class] = css_classes.uniq unless css_classes.empty? html_options diff --git a/lib/simple_form/wrappers/many.rb b/lib/simple_form/wrappers/many.rb index dc5eddfe5..45771e03f 100644 --- a/lib/simple_form/wrappers/many.rb +++ b/lib/simple_form/wrappers/many.rb @@ -58,11 +58,11 @@ def wrap(input, options, content) klass = html_classes(input, options) opts = html_options(options) opts[:class] = (klass << opts[:class]).join(' ').strip unless klass.empty? - input.template.content_tag(tag, content, opts) + input.template.content_tag(tag, content, opts.reject{|k,v| k =~ /_html/ }) end def html_options(options) - options[:"#{namespace}_html"] || {} + (options[:"#{namespace}_html"] || {}).deep_dup end def html_classes(input, options) diff --git a/lib/simple_form/wrappers/root.rb b/lib/simple_form/wrappers/root.rb index 227024e89..71ed6ce85 100644 --- a/lib/simple_form/wrappers/root.rb +++ b/lib/simple_form/wrappers/root.rb @@ -11,7 +11,7 @@ def initialize(*args) end def render(input) - input.options.reverse_merge!(@options) + input.options.merge!(@options.deep_merge(input.options)) super end diff --git a/lib/simple_form/wrappers/single.rb b/lib/simple_form/wrappers/single.rb index eda04208f..68146bf01 100644 --- a/lib/simple_form/wrappers/single.rb +++ b/lib/simple_form/wrappers/single.rb @@ -2,8 +2,8 @@ module SimpleForm module Wrappers # `Single` is an optimization for a wrapper that has only one component. class Single < Many - def initialize(name, options={}) - super(name, [name], options) + def initialize(name, defaults={}) + super(name, [name], defaults) end def render(input)