Skip to content

Commit

Permalink
Tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 9, 2010
1 parent 7d7c6cd commit 0c0e632
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 127 deletions.
2 changes: 1 addition & 1 deletion lib/simple_form/form_builder.rb
Expand Up @@ -83,7 +83,7 @@ def input(attribute_name, options={})
if klass = self.class.mappings[input_type]
klass.new(self).render
else
const_get(:"#{input_type.to_s.camelize}Input").new(self).render
self.class.const_get(:"#{input_type.to_s.camelize}Input").new(self).render
end
end
alias :attribute :input
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/text_field_input.rb
Expand Up @@ -8,7 +8,7 @@ def input

def input_html_options
input_options = super
input_options[:max_length] ||= column.limit if column
input_options[:maxlength] ||= column.limit if column
input_options
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/action_view_extensions/builder_test.rb
Expand Up @@ -77,8 +77,8 @@ class BuilderTest < ActionView::TestCase
concat f.collection_check_boxes :tag_ids, collection, :id, :name
end

assert_select 'form label.collection_check_box[for=user_tag_ids_1]', 'Tag 1'
assert_select 'form label.collection_check_box[for=user_tag_ids_2]', 'Tag 2'
assert_select 'form label.collection_check_boxes[for=user_tag_ids_1]', 'Tag 1'
assert_select 'form label.collection_check_boxes[for=user_tag_ids_2]', 'Tag 2'
end

test 'collection check box accepts selected values as :checked option' do
Expand Down Expand Up @@ -150,15 +150,15 @@ class BuilderTest < ActionView::TestCase
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
form_for @user do |f|
f.fields_for :post do |p|
concat p.collection_check_box :tag_ids, collection, :id, :name
concat p.collection_check_boxes :tag_ids, collection, :id, :name
end
end

assert_select 'form input#user_post_tag_ids_1[type=checkbox][value=1]'
assert_select 'form input#user_post_tag_ids_2[type=checkbox][value=2]'

assert_select 'form label.collection_check_box[for=user_post_tag_ids_1]', 'Tag 1'
assert_select 'form label.collection_check_box[for=user_post_tag_ids_2]', 'Tag 2'
assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_1]', 'Tag 1'
assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_2]', 'Tag 2'
end

# SIMPLE FIELDS
Expand Down
28 changes: 10 additions & 18 deletions test/components/error_test.rb
Expand Up @@ -9,27 +9,19 @@ def with_error_for(object, attribute_name, type, options={}, &block)
f.input_type = type
f.options = options

concat(SimpleForm::Inputs::Base.new(f).error)
concat(SimpleForm::Inputs::Base.new(f).error.to_s)
end
end

# test 'error should not generate content for hidden fields' do
# with_error_for @user, :name, :hidden do |error|
# assert error.call.blank?
# end
# end
#
# test 'error should not generate content for attribute without errors' do
# with_error_for @user, :active, :boolean do |error|
# assert error.call.blank?
# end
# end
#
# test 'error should not generate messages when object is not present' do
# with_error_for :project, :name, :string do |error|
# assert error.call.blank?
# end
# end
test 'error should not generate content for attribute without errors' do
with_error_for @user, :active, :boolean
assert_no_select 'span.error'
end

test 'error should not generate messages when object is not present' do
with_error_for :project, :name, :string
assert_no_select 'span.error'
end

test 'error should generate messages for attribute with single error' do
with_error_for @user, :name, :string
Expand Down
15 changes: 6 additions & 9 deletions test/components/hint_test.rb
Expand Up @@ -9,25 +9,22 @@ def with_hint_for(object, attribute_name, type, options={}, &block)
f.input_type = type
f.options = options

concat(SimpleForm::Inputs::Base.new(f).hint)
concat(SimpleForm::Inputs::Base.new(f).hint.to_s)
end
end

# test 'hint should not be generated by default' do
# assert with_hint_for(@user, :name, :string).blank?
# end
#
# test 'hint should not be generated for hidden fields' do
# assert with_hint_for(@user, :name, :hidden, :hint => 'Use with care...').blank?
# end
test 'hint should not be generated by default' do
with_hint_for @user, :name, :string
assert_no_select 'span.hint'
end

test 'hint should be generated with input text' do
with_hint_for @user, :name, :string, :hint => 'Use with care...'
assert_select 'span.hint', 'Use with care...'
end

test 'hint uses the current component tag set' do
swap SimpleForm, :component_tag => :p do
swap SimpleForm, :hint_tag => :p do
with_hint_for @user, :name, :string, :hint => 'Use with care...'
assert_select 'p.hint', 'Use with care...'
end
Expand Down
22 changes: 0 additions & 22 deletions test/components/label_test.rb
Expand Up @@ -17,13 +17,6 @@ def with_label_for(object, attribute_name, type, options={})
end
end

# Fix me!
# test 'label should not be generated for hidden inputs' do
# with_label_for @user, :name, :hidden do |label|
# assert label.call.blank?
# end
# end

test 'label should generate a default humanized description' do
with_label_for @user, :name, :string
assert_select 'label[for=user_name]', /Name/
Expand Down Expand Up @@ -174,19 +167,4 @@ def with_label_for(object, attribute_name, type, options={})
with_label_for :project, :description, :string, :required => false
assert_no_select 'label.required[for=project_description]'
end

test 'label should point to first option when date input type' do
with_label_for :project, :created_at, :date
assert_select 'label[for=project_created_at_1i]'
end

test 'label should point to first option when datetime input type' do
with_label_for :project, :created_at, :datetime
assert_select 'label[for=project_created_at_1i]'
end

test 'label should point to first option when time input type' do
with_label_for :project, :created_at, :time
assert_select 'label[for=project_created_at_4i]'
end
end
19 changes: 10 additions & 9 deletions test/form_builder_test.rb
Expand Up @@ -38,6 +38,15 @@ def with_association_for(object, *args)
end
end

# All
test 'nested simple fields should yields an instance of FormBuilder' do
simple_form_for :user do |f|
f.simple_fields_for :posts do |posts_form|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
end
end
end

# INPUT TYPES
test 'builder should generate text fields for string columns' do
with_form_for @user, :name
Expand Down Expand Up @@ -209,19 +218,11 @@ def with_association_for(object, *args)

test 'builder allows wrapper tag to be given on demand' do
simple_form_for @user do |f|
concat f.input :name, :wrapper => :b
concat f.input :name, :wrapper_tag => :b
end
assert_select 'form b.required.string'
end

test 'nested simple fields should yields an instance of FormBuilder' do
simple_form_for :user do |f|
f.simple_fields_for :posts do |posts_form|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
end
end
end

# WITHOUT OBJECT
test 'builder should generate properly when object is not present' do
with_form_for :project, :name
Expand Down

0 comments on commit 0c0e632

Please sign in to comment.