Skip to content

Commit

Permalink
Consistent usage of setting component attributes through the wrapper …
Browse files Browse the repository at this point in the history
…api: mostly merging issues
  • Loading branch information
Bertg authored and butsjoh committed Nov 28, 2013
1 parent 38e5a04 commit ffecbe0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
15 changes: 12 additions & 3 deletions lib/simple_form/components/label_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/simple_form/wrappers/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/wrappers/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/simple_form/wrappers/single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ffecbe0

Please sign in to comment.