Permalink
Browse files

Merge pull request #338 from plataformatec/wrapper-lookup

Register and lookup wrappers with symbols
  • Loading branch information...
rafaelfranca committed Sep 28, 2011
2 parents 37a6f01 + b8939a1 commit d1f34e07c9f30c8af6b37e5898660e8ae5709fc1
Showing with 24 additions and 6 deletions.
  1. +9 −5 lib/simple_form.rb
  2. +1 −1 lib/simple_form/form_builder.rb
  3. +14 −0 test/form_builder/wrapper_test.rb
View
@@ -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
@@ -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
@@ -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
@@ -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.