Skip to content

Commit

Permalink
addresses most recent comments - raises exception on invalid options …
Browse files Browse the repository at this point in the history
…instead of a warning
  • Loading branch information
stephenprater committed Aug 27, 2012
1 parent 9c82be6 commit 8957072
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
6 changes: 3 additions & 3 deletions lib/simple_form/wrappers/builder.rb
Expand Up @@ -57,9 +57,9 @@ def use(name, options=nil, &block)
return wrapper(name, options, &block)
end

if options && (self.class.non_tag_components.include?(name) \
&& !(options.except(:wrap_with).keys.empty?))
warn "Invalid options #{options.except(:wrap_with).keys.inspect} passed to #{name}."
if options && (self.class.non_tag_components.include?(name)) \
&& (options.keys != [:wrap_with])
raise ArgumentError, "Invalid options #{options.except(:wrap_with).keys.inspect} passed to #{name}."
end

if options
Expand Down
13 changes: 10 additions & 3 deletions lib/simple_form/wrappers/single.rb
Expand Up @@ -6,13 +6,15 @@ class Single < Many

def initialize(name, options={})
@wrapper_html_options = options.except(:wrap_with)
options = options[:wrap_with] || {}
super(name, [name], options)
super(name, [name], options.fetch(:wrap_with,{}))
end

def render(input)
options = input.options
if options[namespace] != false
#inputs store there html classes in a separate place from other tags
#or non-tag components - premerge them before collecting alll
#possible html_options
if [:input, :label_input].include? namespace
input.input_html_classes.concat Array.wrap(wrapper_html_options[:class])
input.input_html_options.merge! wrapper_html_options.except(:class)
Expand All @@ -28,7 +30,12 @@ def render(input)
private

def html_options_for_input(input, local_namespace = namespace)
options = ( input.options["#{local_namespace}_html".intern] ||= {} )
# because there are a lot of different places where you can set classes
# on inputs (globally, on the individual input, in the form options, in
# the wrappers api, etc) we merge them all together here so that that
# SimpleForm::Wrappers::Many#wrap know what do with them, taking care
# to leave options[:class] as an array if necessary
options = ( input.options["#{local_namespace}_html".to_sym] ||= {} )
if options[:class] || wrapper_html_options[:class]
options[:class] = Array.wrap(options[:class])
options[:class].concat Array.wrap(wrapper_html_options[:class])
Expand Down
7 changes: 2 additions & 5 deletions test/form_builder/wrapper_test.rb
Expand Up @@ -188,15 +188,12 @@ class WrapperTest < ActionView::TestCase
end
end

test 'adding invalid options to non-tag components issues a warning but otherwise works' do
out, err = capture_io do
test 'adding invalid options to non-tag components raises an exception' do
assert_raise ArgumentError, "Invalid options [:class] passed to placeholder." do
swap_wrapper :default, custom_wrapper_with_invalid_options do
with_form_for @user, :name
assert_select "div.custom_wrapper input[name='user[name]']"
end
end
assert_match /Invalid options \[:class\] passed to placeholder./, err
assert_match /Invalid options \[(:id|:class), (:class|:id)\] passed to hint./, err
end

test 'do not duplicate label classes for different inputs' do
Expand Down
1 change: 0 additions & 1 deletion test/support/misc_helpers.rb
Expand Up @@ -73,7 +73,6 @@ def custom_wrapper_with_wrong_wrapping_tag
def custom_wrapper_with_invalid_options
SimpleForm.build :tag => :div, :class => "custom_wrapper" do |b|
b.use :placeholder, :class => 'no_effect'
b.use :hint, :id => 'no_id', :class => 'no_effect'
b.use :input
end
end
Expand Down
16 changes: 0 additions & 16 deletions test/test_helper.rb
Expand Up @@ -87,20 +87,4 @@ def user_path(*args)
alias :validating_user_path :user_path
alias :validating_users_path :user_path
alias :other_validating_user_path :user_path

unless instance_methods.include? "capture_io"
def capture_io
require 'stringio'

orig_stdout, orig_stderr = $stdout, $stderr
captured_stdout, captured_stderr = StringIO.new, StringIO.new
$stdout, $stderr = captured_stdout, captured_stderr

yield
return captured_stdout.string, captured_stderr.string
ensure
$stdout = orig_stdout
$stderr = orig_stderr
end
end
end

0 comments on commit 8957072

Please sign in to comment.