Skip to content

Commit

Permalink
Merge pull request heartcombo#338 from plataformatec/wrapper-lookup
Browse files Browse the repository at this point in the history
Register and lookup wrappers with symbols
  • Loading branch information
rafaelfranca committed Sep 28, 2011
2 parents 37a6f01 + b8939a1 commit d1f34e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/simple_form.rb
Expand Up @@ -117,16 +117,20 @@ module SimpleForm

# Retrieves a given wrapper
def self.wrapper(name)
@@wrappers[name]
@@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
end

# Raised when fails to find a given wrapper name
class WrapperNotFound < StandardError
end

# Define a new wrapper using SimpleForm::Wrappers::Builder
# and store it in the given name.
def self.wrappers(*args, &block)
if block_given?
options = args.extract_options!
name = args.first || :default
@@wrappers[name] = build(options, &block)
options = args.extract_options!
name = args.first || :default
@@wrappers[name.to_sym] = build(options, &block)
else
@@wrappers
end
Expand Down Expand Up @@ -166,4 +170,4 @@ def self.setup
"Updating to the new API is easy and fast. Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/simple_form/form_builder.rb
Expand Up @@ -93,7 +93,7 @@ def initialize(*) #:nodoc:
def input(attribute_name, options={}, &block)
chosen =
if name = options[:wrapper]
name.is_a?(Symbol) ? SimpleForm.wrapper(name) : name
name.respond_to?(:render) ? name : SimpleForm.wrapper(name)
else
wrapper
end
Expand Down
14 changes: 14 additions & 0 deletions test/form_builder/wrapper_test.rb
Expand Up @@ -112,4 +112,18 @@ class WrapperTest < ActionView::TestCase
assert_select "section.custom_wrapper div.another_wrapper label"
assert_select "section.custom_wrapper div.another_wrapper input.string"
end

test 'access wrappers with indifferent access' do
swap_wrapper :another do
with_form_for @user, :name, :wrapper => "another"
assert_select "section.custom_wrapper div.another_wrapper label"
assert_select "section.custom_wrapper div.another_wrapper input.string"
end
end

test 'raise error when wrapper not found' do
assert_raise SimpleForm::WrapperNotFound do
with_form_for @user, :name, :wrapper => :not_found
end
end
end

0 comments on commit d1f34e0

Please sign in to comment.