Skip to content

Commit

Permalink
All tests pass with the new wrappers API.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 8, 2011
1 parent c6e6dbd commit 25a13a9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
7 changes: 5 additions & 2 deletions lib/simple_form/inputs/hidden_input.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module SimpleForm
module Inputs
class HiddenInput < Base
def render
def label; "" end
def error; end
def hint; end

def input
@builder.hidden_field(attribute_name, input_html_options)
end
alias :input :render

private

Expand Down
3 changes: 1 addition & 2 deletions lib/simple_form/wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ module Wrappers
autoload :Many, 'simple_form/wrappers/many'
autoload :Root, 'simple_form/wrappers/root'
autoload :Single, 'simple_form/wrappers/single'
autoload :Anonym, 'simple_form/wrappers/anonym'

# TODO: Test the anonym case
def self.find(name)
SimpleForm.components.find { |c| c.namespace == name } || SingleForm::Wrappers::Anonym.new(name)
SimpleForm.components.find { |c| c.namespace == name } || SingleForm::Wrappers::Many.new(name, name)
end

def self.wrap(array)
Expand Down
13 changes: 0 additions & 13 deletions lib/simple_form/wrappers/anonym.rb

This file was deleted.

10 changes: 8 additions & 2 deletions lib/simple_form/wrappers/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ def render(input)

private

def wrap(input, options, content)
return content if options[:wrapper] == false
super
end

def html_classes(input, options)
css = super
css << @defaults[:error_class] || options[:wrapper_error_class] if input.has_errors?
css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class]
css += input.input_html_classes
css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors?
css << "disabled" if input.disabled?
css
end
Expand Down
8 changes: 3 additions & 5 deletions test/components/hint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ def with_hint_for(object, attribute_name, type, options={}, &block)
end

test 'hint uses the current component tag set' do
swap SimpleForm, :hint_tag => :p do
swap SimpleForm, :components => [:hint] do
with_hint_for @user, :name, :string, :hint => 'Use with care...'
assert_select 'p.hint', 'Use with care...'
end
swap! SimpleForm, :hint_tag => :p do
with_hint_for @user, :name, :string, :hint => 'Use with care...'
assert_select 'p.hint', 'Use with care...'
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/components/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def with_form_for(object, *args, &block)
end

test 'wrapper should add chosen error class for attribute with errors' do
swap SimpleForm, :wrapper_error_class => "omgError" do
swap! SimpleForm, :wrapper_error_class => "omgError" do
with_error_for @user, :name
assert_select 'div.omgError'
end
end

test 'wrapper should add chosen wrapper class' do
swap SimpleForm, :wrapper_class => "wrapper" do
swap! SimpleForm, :wrapper_class => "wrapper" do
with_form_for @user, :active
assert_select 'div.wrapper'
assert_no_select 'div.input'
Expand Down
6 changes: 3 additions & 3 deletions test/form_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def with_association_for(object, *args)
# WRAPPERS

test 'builder support wrapping around an specific tag' do
swap SimpleForm, :wrapper_tag => :p do
swap! SimpleForm, :wrapper_tag => :p do
with_form_for @user, :name
assert_select 'form p label[for=user_name]'
assert_select 'form p input#user_name.string'
Expand All @@ -416,7 +416,7 @@ def with_association_for(object, *args)
end

test 'builder wrapping tag adds default css classes' do
swap SimpleForm, :wrapper_tag => :p do
swap! SimpleForm, :wrapper_tag => :p do
with_form_for @user, :name
assert_select 'form p.input.required.string'

Expand All @@ -426,7 +426,7 @@ def with_association_for(object, *args)
end

test 'builder wrapping tag allow custom options to be given' do
swap SimpleForm, :wrapper_tag => :p do
swap! SimpleForm, :wrapper_tag => :p do
with_form_for @user, :name, :wrapper_html => { :id => "super_cool", :class => 'yay' }
assert_select 'form p#super_cool.required.string.yay'
end
Expand Down
10 changes: 10 additions & 0 deletions test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def swap(object, new_values)
end
end

# Temporary hack to deal with components
def swap!(*args)
swap(*args) do
SimpleForm.components = [ :placeholder, :maxlength, :label_input, :hint, :error ]
yield
end
ensure
SimpleForm.components = [ :placeholder, :maxlength, :label_input, :hint, :error ]
end

def with_concat_form_for(object, &block)
concat simple_form_for(object, &block)
end
Expand Down

0 comments on commit 25a13a9

Please sign in to comment.