2009-12-09 13:26:30 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
2009-12-09 17:36:21 -05:00
|
|
|
class BuilderTest < ActionView::TestCase
|
2010-09-26 18:24:30 -04:00
|
|
|
|
|
|
|
def with_concat_form_for(object, &block)
|
|
|
|
concat form_for(object, &block)
|
|
|
|
end
|
|
|
|
|
2009-12-11 10:09:00 -05:00
|
|
|
# COLLECTION RADIO
|
2009-12-10 05:44:34 -05:00
|
|
|
test 'collection radio accepts a collection and generate inputs from value method' do
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_radio :active, [true, false], :to_s, :to_s
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-09 13:26:30 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=radio][value=true]#user_active_true'
|
|
|
|
assert_select 'form input[type=radio][value=false]#user_active_false'
|
|
|
|
end
|
|
|
|
|
2009-12-10 05:44:34 -05:00
|
|
|
test 'collection radio accepts a collection and generate inputs from label method' do
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_radio :active, [true, false], :to_s, :to_s
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-09 13:26:30 -05:00
|
|
|
|
2009-12-11 10:09:00 -05:00
|
|
|
assert_select 'form label.collection_radio[for=user_active_true]', 'true'
|
|
|
|
assert_select 'form label.collection_radio[for=user_active_false]', 'false'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection radio accepts checked item' do
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_radio :active, [[1, true], [0, false]], :last, :first, :checked => true
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=radio][value=true][checked=checked]'
|
|
|
|
assert_no_select 'form input[type=radio][value=false][checked=checked]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection radio accepts multiple disabled items' do
|
|
|
|
collection = [[1, true], [0, false], [2, 'other']]
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_radio :active, collection, :last, :first, :disabled => [true, false]
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=radio][value=true][disabled=disabled]'
|
|
|
|
assert_select 'form input[type=radio][value=false][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=radio][value=other][disabled=disabled]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection radio accepts single disable item' do
|
|
|
|
collection = [[1, true], [0, false]]
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_radio :active, collection, :last, :first, :disabled => true
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=radio][value=true][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=radio][value=false][disabled=disabled]'
|
2009-12-09 13:26:30 -05:00
|
|
|
end
|
|
|
|
|
2009-12-10 05:44:34 -05:00
|
|
|
test 'collection radio accepts html options as input' do
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_radio :active, [[1, true], [0, false]], :last, :first, {}, :class => 'radio'
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-09 13:26:30 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=radio][value=true].radio#user_active_true'
|
|
|
|
assert_select 'form input[type=radio][value=false].radio#user_active_false'
|
|
|
|
end
|
2009-12-10 05:44:34 -05:00
|
|
|
|
2009-12-11 10:09:00 -05:00
|
|
|
# COLLECTION CHECK BOX
|
|
|
|
test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do
|
|
|
|
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :id, :name
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select "form input[type=hidden][name='user[tag_ids][]'][value=]"
|
|
|
|
assert_select 'form input#user_tag_ids_1[type=checkbox][value=1]'
|
|
|
|
assert_select 'form input#user_tag_ids_2[type=checkbox][value=2]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection check box accepts a collection and generate a serie of checkboxes with labels for label method' do
|
|
|
|
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :id, :name
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
2010-01-09 12:12:46 -05:00
|
|
|
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'
|
2009-12-11 10:09:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection check box accepts selected values as :checked option' do
|
|
|
|
collection = (1..3).map{|i| [i, "Tag #{i}"] }
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :first, :last, :checked => [1, 3]
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=checkbox][value=1][checked=checked]'
|
|
|
|
assert_select 'form input[type=checkbox][value=3][checked=checked]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection check box accepts a single checked value' do
|
|
|
|
collection = (1..3).map{|i| [i, "Tag #{i}"] }
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :first, :last, :checked => 3
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=checkbox][value=3][checked=checked]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=1][checked=checked]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection check box accepts multiple disabled items' do
|
|
|
|
collection = (1..3).map{|i| [i, "Tag #{i}"] }
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :first, :last, :disabled => [1, 3]
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=checkbox][value=1][disabled=disabled]'
|
|
|
|
assert_select 'form input[type=checkbox][value=3][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=2][disabled=disabled]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'collection check box accepts single disable item' do
|
|
|
|
collection = (1..3).map{|i| [i, "Tag #{i}"] }
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :first, :last, :disabled => 1
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=checkbox][value=1][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=3][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=2][disabled=disabled]'
|
|
|
|
end
|
|
|
|
|
2009-12-11 11:37:33 -05:00
|
|
|
test 'collection check box accepts a proc to disabled items' do
|
|
|
|
collection = (1..3).map{|i| [i, "Tag #{i}"] }
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :first, :last, :disabled => proc { |i| i.first == 1 }
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 11:37:33 -05:00
|
|
|
|
|
|
|
assert_select 'form input[type=checkbox][value=1][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=3][disabled=disabled]'
|
|
|
|
assert_no_select 'form input[type=checkbox][value=2][disabled=disabled]'
|
|
|
|
end
|
|
|
|
|
2009-12-11 10:09:00 -05:00
|
|
|
test 'collection check box accepts html options' do
|
|
|
|
collection = [[1, 'Tag 1'], [2, 'Tag 2']]
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.collection_check_boxes :tag_ids, collection, :first, :last, {}, :class => 'check'
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 10:09:00 -05:00
|
|
|
|
|
|
|
assert_select 'form input.check[type=checkbox][value=1]'
|
|
|
|
assert_select 'form input.check[type=checkbox][value=2]'
|
|
|
|
end
|
|
|
|
|
2009-12-11 11:42:44 -05:00
|
|
|
test 'collection check box with fields for' do
|
2009-12-11 11:37:33 -05:00
|
|
|
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.fields_for(:post) do |p|
|
|
|
|
p.collection_check_boxes :tag_ids, collection, :id, :name
|
|
|
|
end
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-11 11:37:33 -05:00
|
|
|
|
|
|
|
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]'
|
|
|
|
|
2010-01-09 12:12:46 -05:00
|
|
|
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'
|
2009-12-11 11:37:33 -05:00
|
|
|
end
|
|
|
|
|
2009-12-11 10:09:00 -05:00
|
|
|
# SIMPLE FIELDS
|
2009-12-10 05:44:34 -05:00
|
|
|
test 'simple fields for is available and yields an instance of FormBuilder' do
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2010-09-26 18:16:25 -04:00
|
|
|
f.simple_fields_for(:posts) do |posts_form|
|
2009-12-10 05:44:34 -05:00
|
|
|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
|
2010-09-26 18:16:25 -04:00
|
|
|
end
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-10 05:44:34 -05:00
|
|
|
end
|
2009-12-09 13:26:30 -05:00
|
|
|
end
|