Skip to content

Commit

Permalink
config.components => config.wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 8, 2011
1 parent da5e1d8 commit bf4d766
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
# Components are used by the form builder to generate a complete input.
# You can remove any of them, change the order or even add your own
# components to the stack. The options given to the components method
# are used by the wrapper input. You can remove them (to remove the wrapper)
# or change them as needed.
config.components :tag => :div, :class => :input, :error_class => :field_with_errors do |b|
# Wrappers are used by the form builder to generate a complete input.
# You can remove any component from the wrapper, change the order or even
# add your own to the stack. The options given to the wrappers method
# are used to wrap the whole input (if any exists).
config.wrappers :tag => :div, :class => :input, :error_class => :field_with_errors do |b|
b.use :placeholder
b.use :label_input
b.use :hint, :tag => :span, :class => :hint
Expand Down
60 changes: 36 additions & 24 deletions lib/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module SimpleForm
autoload :MapType, 'simple_form/map_type'
autoload :Wrappers, 'simple_form/wrappers'

# The wrapper object.
mattr_accessor :wrapper
@@wrapper = nil
## CONFIGURATION OPTIONS

# Method used to tidy up errors.
mattr_accessor :error_method
Expand Down Expand Up @@ -110,23 +108,23 @@ module SimpleForm
mattr_accessor :cache_discovery
@@cache_discovery = !Rails.env.development?

## DEPRECATED METHODS
## WRAPPER CONFIGURATION
@@wrappers = {}

DEPRECATED = %w(hint_tag= hint_class= error_tag= error_class= wrapper_tag= wrapper_class= wrapper_error_class= components=)
@@deprecated = false

DEPRECATED.each do |method|
class_eval "def #{method}; @@deprecated = true; end"
# Retrieves a given wrapper
def self.wrapper(name)
@@wrappers[name]
end

# Default way to setup SimpleForm. Run rails generate simple_form:install
# to create a fresh initializer with all configuration values.
def self.setup
yield self

if @@deprecated
raise "[SIMPLE FORM] Your simple form initializer file is using an outdated configuration API. " <<
"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"
# 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)
else
@@wrappers
end
end

Expand All @@ -137,16 +135,30 @@ def self.build(options={})
SimpleForm::Wrappers::Root.new(builder.to_a, options)
end

# Build a new wrapper using SimpleForm::Wrappers::Builder and
# sets it as the default wrapper.
def self.components(*args, &block)
self.wrapper = build(*args, &block)
end

components :tag => :div, :class => :input, :error_class => :field_with_errors do |b|
wrappers :tag => :div, :class => :input, :error_class => :field_with_errors do |b|
b.use :placeholder
b.use :label_input
b.use :hint, :tag => :span, :class => :hint
b.use :error, :tag => :span, :class => :error
end

## SETUP

DEPRECATED = %w(hint_tag= hint_class= error_tag= error_class= wrapper_tag= wrapper_class= wrapper_error_class= components=)
@@deprecated = false

DEPRECATED.each do |method|
class_eval "def #{method}; @@deprecated = true; end"
end

# Default way to setup SimpleForm. Run rails generate simple_form:install
# to create a fresh initializer with all configuration values.
def self.setup
yield self

if @@deprecated
raise "[SIMPLE FORM] Your simple form initializer file is using an outdated configuration API. " <<
"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
13 changes: 9 additions & 4 deletions lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
attr_reader :template, :object_name, :object
attr_reader :template, :object_name, :object, :wrapper

extend MapType
include SimpleForm::Inputs
Expand All @@ -20,6 +20,11 @@ def self.discovery_cache
@discovery_cache ||= {}
end

def initialize(*) #:nodoc:
super
@wrapper = SimpleForm.wrapper(options.delete(:wrapper) || :default)
end

# Basic input helper, combines all components in the stack to generate
# input html based on options the user define and some guesses through
# database column information. By default a call to input will generate
Expand Down Expand Up @@ -86,7 +91,7 @@ def self.discovery_cache
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
#
def input(attribute_name, options={}, &block)
SimpleForm.wrapper.render find_input(attribute_name, options, &block)
wrapper.render find_input(attribute_name, options, &block)
end
alias :attribute :input

Expand Down Expand Up @@ -202,7 +207,7 @@ def error(attribute_name, options={})
options[:error_html] = options.except(:error_tag, :error_prefix, :error_method)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
SimpleForm::Wrappers.find(:error).
wrapper.find(:error).
render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options))
end

Expand Down Expand Up @@ -243,7 +248,7 @@ def hint(attribute_name, options={})
input_type = default_input_type(attribute_name, column, options)
end

SimpleForm::Wrappers.find(:hint).
wrapper.find(:hint).
render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options))
end

Expand Down
4 changes: 0 additions & 4 deletions lib/simple_form/wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@ module Wrappers
autoload :Many, 'simple_form/wrappers/many'
autoload :Root, 'simple_form/wrappers/root'
autoload :Single, 'simple_form/wrappers/single'

def self.find(name)
SimpleForm.wrapper.find(name) || SingleForm::Wrappers::Many.new(name, [name])
end
end
end
2 changes: 1 addition & 1 deletion lib/simple_form/wrappers/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Wrappers
# Provides the builder syntax for components. The builder provides
# only one method (called `use`) and it allows the following invocations:
#
# config.components do |b|
# config.wrappers do |b|
# # Use a single component
# b.use :placeholder
#
Expand Down
5 changes: 5 additions & 0 deletions lib/simple_form/wrappers/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def initialize(*args)
super(:wrapper, *args)
end

# Provide a fallback if name cannot be found.
def find(name)
super || SingleForm::Wrappers::Many.new(name, [name])
end

private

def html_classes(input, options)
Expand Down
2 changes: 1 addition & 1 deletion test/form_builder/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def with_full_error_for(object, *args)
# CUSTOM WRAPPERS

test 'error with custom wrappers works' do
swap SimpleForm, :wrapper => custom_wrapper do
swap_wrapper do
with_error_for @user, :name
assert_select 'span.omg_error', "can't be blank"
end
Expand Down
2 changes: 1 addition & 1 deletion test/form_builder/hint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def with_hint_for(object, *args)
# Custom wrappers

test 'hint with custom wrappers works' do
swap SimpleForm, :wrapper => custom_wrapper do
swap_wrapper do
with_hint_for @user, :name, :hint => "can't be blank"
assert_select 'span.omg_hint', "can't be blank"
end
Expand Down
4 changes: 2 additions & 2 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WrapperTest < ActionView::TestCase
# Custom wrapper test

test 'custom wrappers works' do
swap SimpleForm, :wrapper => custom_wrapper do
swap_wrapper do
with_form_for @user, :name, :hint => "cool"
assert_select "section.custom_wrapper div.another_wrapper label"
assert_select "section.custom_wrapper div.another_wrapper input.string"
Expand All @@ -64,7 +64,7 @@ class WrapperTest < ActionView::TestCase
end

test 'custom wrappers can be turned off' do
swap SimpleForm, :wrapper => custom_wrapper do
swap_wrapper do
with_form_for @user, :name, :another => false
assert_no_select "section.custom_wrapper div.another_wrapper label"
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
Expand Down
8 changes: 8 additions & 0 deletions test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def swap(object, new_values)
end
end

def swap_wrapper(name=:default, wrapper=self.custom_wrapper)
old = SimpleForm.wrappers[name]
SimpleForm.wrappers[name] = wrapper
yield
ensure
SimpleForm.wrappers[name] = old
end

def custom_wrapper
SimpleForm.build :tag => :section, :class => "custom_wrapper" do |b|
b.use :another, :tag => :div, :class => "another_wrapper" do |ba|
Expand Down

0 comments on commit bf4d766

Please sign in to comment.