2017-10-24 01:01:08 +00:00
# frozen_string_literal: true
2009-12-09 18:26:30 +00:00
require 'test_helper'
2011-04-06 00:46:36 +00:00
class BuilderTest < ActionView :: TestCase
2011-03-02 02:05:58 +00:00
def with_custom_form_for ( object , * args , & block )
with_concat_custom_form_for ( object ) do | f |
assert f . instance_of? ( CustomFormBuilder )
yield f
end
end
2014-03-11 22:12:08 +00:00
def with_collection_radio_buttons ( object , attribute , collection , value_method , text_method , options = { } , html_options = { } , & block )
2010-09-26 22:31:34 +00:00
with_concat_form_for ( object ) do | f |
2012-01-27 18:30:19 +00:00
f . collection_radio_buttons attribute , collection , value_method , text_method , options , html_options , & block
2010-09-26 22:31:34 +00:00
end
end
2014-03-11 22:12:08 +00:00
def with_collection_check_boxes ( object , attribute , collection , value_method , text_method , options = { } , html_options = { } , & block )
2010-09-26 22:31:34 +00:00
with_concat_form_for ( object ) do | f |
2012-01-26 17:34:23 +00:00
f . collection_check_boxes attribute , collection , value_method , text_method , options , html_options , & block
2010-09-26 22:31:34 +00:00
end
end
2011-04-06 00:46:36 +00:00
2009-12-11 15:09:00 +00:00
# COLLECTION RADIO
2013-01-02 13:20:30 +00:00
test " collection radio accepts a collection and generate inputs from value method " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s
2009-12-09 18:26:30 +00:00
assert_select 'form input[type=radio][value=true]#user_active_true'
assert_select 'form input[type=radio][value=false]#user_active_false'
end
2013-01-02 13:20:30 +00:00
test " collection radio accepts a collection and generate inputs from label method " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s
2009-12-09 18:26:30 +00:00
2012-01-27 18:30:19 +00:00
assert_select 'form label.collection_radio_buttons[for=user_active_true]' , 'true'
assert_select 'form label.collection_radio_buttons[for=user_active_false]' , 'false'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio handles camelized collection values for labels correctly " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ 'Yes' , 'No' ] , :to_s , :to_s
2010-10-12 18:57:38 +00:00
2012-01-27 18:30:19 +00:00
assert_select 'form label.collection_radio_buttons[for=user_active_yes]' , 'Yes'
assert_select 'form label.collection_radio_buttons[for=user_active_no]' , 'No'
2010-10-12 18:57:38 +00:00
end
2014-05-14 17:17:54 +00:00
test " collection radio sanitizes collection values for labels correctly " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :name , [ '$0.99' , '$1.99' ] , :to_s , :to_s
assert_select 'label.collection_radio_buttons[for=user_name_099]' , '$0.99'
assert_select 'label.collection_radio_buttons[for=user_name_199]' , '$1.99'
2010-10-27 22:24:25 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio checks the correct value to local variables " do
2013-06-24 18:41:58 +00:00
user = User . build ( active : false )
2012-02-26 15:57:30 +00:00
with_collection_radio_buttons user , :active , [ true , false ] , :to_s , :to_s
assert_select 'form input[type=radio][value=true]'
assert_select 'form input[type=radio][value=false][checked=checked]'
end
2013-01-02 13:20:30 +00:00
test " collection radio accepts checked item " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ [ 1 , true ] , [ 0 , false ] ] , :last , :first , checked : true
2009-12-11 15:09:00 +00:00
assert_select 'form input[type=radio][value=true][checked=checked]'
assert_no_select 'form input[type=radio][value=false][checked=checked]'
end
2012-05-06 11:14:23 +00:00
2013-01-02 13:20:30 +00:00
test " collection radio accepts checked item which has a value of false " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ [ 1 , true ] , [ 0 , false ] ] , :last , :first , checked : false
2012-03-09 00:49:27 +00:00
assert_no_select 'form input[type=radio][value=true][checked=checked]'
assert_select 'form input[type=radio][value=false][checked=checked]'
end
2009-12-11 15:09:00 +00:00
2013-01-02 13:20:30 +00:00
test " collection radio accepts multiple disabled items " do
2009-12-11 15:09:00 +00:00
collection = [ [ 1 , true ] , [ 0 , false ] , [ 2 , 'other' ] ]
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , collection , :last , :first , disabled : [ true , false ]
2009-12-11 15:09:00 +00: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
2013-01-02 13:20:30 +00:00
test " collection radio accepts single disable item " do
2009-12-11 15:09:00 +00:00
collection = [ [ 1 , true ] , [ 0 , false ] ]
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , collection , :last , :first , disabled : true
2009-12-11 15:09:00 +00: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 18:26:30 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio accepts html options as input " do
2010-11-21 16:51:49 +00:00
collection = [ [ 1 , true ] , [ 0 , false ] ]
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , collection , :last , :first , { } , class : 'special-radio'
2009-12-09 18:26:30 +00:00
2012-01-27 18:30:19 +00:00
assert_select 'form input[type=radio][value=true].special-radio#user_active_true'
assert_select 'form input[type=radio][value=false].special-radio#user_active_false'
2009-12-09 18:26:30 +00:00
end
2009-12-10 10:44:34 +00:00
2013-01-02 13:20:30 +00:00
test " collection radio wraps the collection in the given collection wrapper tag " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s , collection_wrapper_tag : :ul
2010-11-21 16:51:49 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form ul input[type=radio]' , count : 2
2011-07-27 15:15:32 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio does not render any wrapper tag by default " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s
2010-11-21 16:51:49 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=radio]' , count : 2
2010-11-21 16:51:49 +00:00
assert_no_select 'form ul'
end
2013-01-02 13:20:30 +00:00
test " collection radio does not wrap the collection when given falsy values " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s , collection_wrapper_tag : false
2011-10-10 17:06:53 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=radio]' , count : 2
2012-01-26 16:55:12 +00:00
assert_no_select 'form ul'
2011-10-10 17:06:53 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio uses the given class for collection wrapper tag " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
collection_wrapper_tag : :ul , collection_wrapper_class : " items-list "
2011-10-10 17:06:53 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form ul.items-list input[type=radio]' , count : 2
2011-10-10 17:06:53 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio uses no class for collection wrapper tag when no wrapper tag is given " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
collection_wrapper_class : " items-list "
2011-10-20 11:48:59 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=radio]' , count : 2
2012-01-26 16:55:12 +00:00
assert_no_select 'form ul'
assert_no_select '.items-list'
2011-10-20 11:48:59 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio uses no class for collection wrapper tag by default " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s , collection_wrapper_tag : :ul
2011-10-10 17:06:53 +00:00
2012-01-26 16:55:12 +00:00
assert_select 'form ul'
assert_no_select 'form ul[class]'
2011-10-10 17:06:53 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio wrap items in a span tag by default " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s
2010-11-21 16:51:49 +00:00
2012-01-26 16:55:12 +00:00
assert_select 'form span input[type=radio][value=true]#user_active_true + label'
assert_select 'form span input[type=radio][value=false]#user_active_false + label'
2010-11-21 16:51:49 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio wraps each item in the given item wrapper tag " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s , item_wrapper_tag : :li
2010-11-21 16:51:49 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form li input[type=radio]' , count : 2
2010-11-21 16:51:49 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio does not wrap each item when given explicitly falsy value " do
2013-01-28 21:02:59 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s , item_wrapper_tag : false
2011-07-26 18:45:36 +00:00
2012-01-26 16:55:12 +00:00
assert_select 'form input[type=radio]'
assert_no_select 'form span input[type=radio]'
2011-04-06 03:12:07 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio uses the given class for item wrapper tag " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
item_wrapper_tag : :li , item_wrapper_class : " inline "
2012-01-27 18:09:45 +00:00
2013-01-28 21:02:59 +00:00
assert_select " form li.inline input[type=radio] " , count : 2
2012-01-27 18:09:45 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio uses no class for item wrapper tag when no wrapper tag is given " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
item_wrapper_tag : nil , item_wrapper_class : " inline "
2012-01-27 18:09:45 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=radio]' , count : 2
2012-01-27 18:09:45 +00:00
assert_no_select 'form li'
assert_no_select '.inline'
end
2013-01-02 13:20:30 +00:00
test " collection radio uses no class for item wrapper tag by default " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
item_wrapper_tag : :li
2012-01-27 18:09:45 +00:00
2013-01-28 21:02:59 +00:00
assert_select " form li " , count : 2
2012-01-27 18:09:45 +00:00
assert_no_select " form li[class] "
end
2013-01-02 13:20:30 +00:00
test " collection radio does not wrap input inside the label " do
2012-01-27 18:30:19 +00:00
with_collection_radio_buttons @user , :active , [ true , false ] , :to_s , :to_s
2011-04-06 03:12:07 +00:00
2012-01-26 17:34:23 +00:00
assert_select 'form input[type=radio] + label'
2011-04-06 03:33:41 +00:00
assert_no_select 'form label input'
2010-11-21 16:51:49 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection radio accepts a block to render the label as radio button wrapper " do
2012-02-14 01:50:16 +00:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label { b . radio_button }
2012-01-26 17:34:23 +00:00
end
2012-02-14 01:50:16 +00:00
assert_select 'label[for=user_active_true] > input#user_active_true[type=radio]'
assert_select 'label[for=user_active_false] > input#user_active_false[type=radio]'
end
2013-01-02 13:20:30 +00:00
test " collection radio accepts a block to change the order of label and radio button " do
2012-02-14 01:50:16 +00:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label + b . radio_button
end
assert_select 'label[for=user_active_true] + input#user_active_true[type=radio]'
assert_select 'label[for=user_active_false] + input#user_active_false[type=radio]'
end
2013-01-02 13:20:30 +00:00
test " collection radio with block helpers accept extra html options " do
2012-02-14 01:50:16 +00:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
2013-01-28 21:02:59 +00:00
b . label ( class : " radio_button " ) + b . radio_button ( class : " radio_button " )
2012-02-14 01:50:16 +00:00
end
assert_select 'label.radio_button[for=user_active_true] + input#user_active_true.radio_button[type=radio]'
assert_select 'label.radio_button[for=user_active_false] + input#user_active_false.radio_button[type=radio]'
end
2013-01-02 13:20:30 +00:00
test " collection radio with block helpers allows access to current text and value " do
2012-02-14 01:50:16 +00:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label ( :" data-value " = > b . value ) { b . radio_button + b . text }
end
assert_select 'label[for=user_active_true][data-value=true]' , 'true' do
assert_select 'input#user_active_true[type=radio]'
end
assert_select 'label[for=user_active_false][data-value=false]' , 'false' do
assert_select 'input#user_active_false[type=radio]'
end
end
2013-01-02 13:20:30 +00:00
test " collection radio with block helpers allows access to the current object item in the collection to access extra properties " do
2012-02-14 01:50:16 +00:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
2013-01-28 21:02:59 +00:00
b . label ( class : b . object ) { b . radio_button + b . text }
2012-02-14 01:50:16 +00:00
end
assert_select 'label.true[for=user_active_true]' , 'true' do
assert_select 'input#user_active_true[type=radio]'
end
assert_select 'label.false[for=user_active_false]' , 'false' do
assert_select 'input#user_active_false[type=radio]'
end
2012-01-26 17:34:23 +00:00
end
2013-01-22 01:41:18 +00:00
test " collection radio with block helpers does not leak the template " do
with_concat_form_for ( @user ) do | f |
collection_input = f . collection_radio_buttons :active , [ true , false ] , :to_s , :to_s do | b |
2013-01-28 21:02:59 +00:00
b . label ( class : b . object ) { b . radio_button + b . text }
2013-01-22 01:41:18 +00:00
end
concat collection_input
concat f . hidden_field :name
end
assert_select 'label.true[for=user_active_true]' , text : 'true' , count : 1 do
assert_select 'input#user_active_true[type=radio]'
end
assert_select 'label.false[for=user_active_false]' , text : 'false' , count : 1 do
assert_select 'input#user_active_false[type=radio]'
end
end
2009-12-11 15:09:00 +00:00
# COLLECTION CHECK BOX
2013-01-02 13:20:30 +00:00
test " collection check box accepts a collection and generate a serie of checkboxes for value method " do
2009-12-11 15:09:00 +00:00
collection = [ Tag . new ( 1 , 'Tag 1' ) , Tag . new ( 2 , 'Tag 2' ) ]
2010-09-26 22:31:34 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :id , :name
2009-12-11 15:09:00 +00:00
2014-09-08 20:19:40 +00:00
assert_select 'form input#user_tag_ids_1[type=checkbox][value="1"]'
assert_select 'form input#user_tag_ids_2[type=checkbox][value="2"]'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box generates only one hidden field for the entire collection, to ensure something will be sent back to the server when posting an empty collection " do
2012-01-30 17:29:18 +00:00
collection = [ Tag . new ( 1 , 'Tag 1' ) , Tag . new ( 2 , 'Tag 2' ) ]
with_collection_check_boxes @user , :tag_ids , collection , :id , :name
2014-09-08 21:14:53 +00:00
assert_select " form input[type=hidden][name='user[tag_ids][]'][value=''] " , count : 1
2012-01-30 17:29:18 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts a collection and generate a serie of checkboxes with labels for label method " do
2009-12-11 15:09:00 +00:00
collection = [ Tag . new ( 1 , 'Tag 1' ) , Tag . new ( 2 , 'Tag 2' ) ]
2010-09-26 22:31:34 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :id , :name
2009-12-11 15:09:00 +00:00
2010-01-09 17:12:46 +00: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 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box handles camelized collection values for labels correctly " do
2010-10-12 18:57:38 +00:00
with_collection_check_boxes @user , :active , [ 'Yes' , 'No' ] , :to_s , :to_s
assert_select 'form label.collection_check_boxes[for=user_active_yes]' , 'Yes'
assert_select 'form label.collection_check_boxes[for=user_active_no]' , 'No'
end
2014-05-14 17:17:54 +00:00
test " collection check box sanitizes collection values for labels correctly " do
2010-10-27 22:24:25 +00:00
with_collection_check_boxes @user , :name , [ '$0.99' , '$1.99' ] , :to_s , :to_s
assert_select 'label.collection_check_boxes[for=user_name_099]' , '$0.99'
assert_select 'label.collection_check_boxes[for=user_name_199]' , '$1.99'
end
2013-01-02 13:20:30 +00:00
test " collection check box checks the correct value to local variables " do
2013-06-24 18:41:58 +00:00
user = User . build ( tag_ids : [ 1 , 3 ] )
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2013-06-24 18:41:58 +00:00
2012-02-26 15:57:30 +00:00
with_collection_check_boxes user , :tag_ids , collection , :first , :last
2014-09-08 20:19:40 +00: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]'
2012-02-26 15:57:30 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts selected values as :checked option " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , checked : [ 1 , 3 ]
2009-12-11 15:09:00 +00:00
2014-09-08 20:19:40 +00: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]'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes accepts selected string values as :checked option " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2013-01-28 21:02:59 +00:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , checked : [ '1' , '3' ]
2012-05-06 11:14:23 +00:00
2014-09-08 20:19:40 +00:00
assert_select 'input[type=checkbox][value="1"][checked=checked]'
assert_select 'input[type=checkbox][value="3"][checked=checked]'
assert_no_select 'input[type=checkbox][value="2"][checked=checked]'
2012-05-06 11:14:23 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts a single checked value " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , checked : 3
2009-12-11 15:09:00 +00:00
2014-09-08 20:19:40 +00: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]'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts selected values as :checked option and override the model values " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2011-10-05 22:58:17 +00:00
@user . tag_ids = [ 2 ]
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , checked : [ 1 , 3 ]
2011-10-05 22:58:17 +00:00
2014-09-08 20:19:40 +00: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]'
2011-10-05 22:58:17 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts multiple disabled items " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , disabled : [ 1 , 3 ]
2009-12-11 15:09:00 +00:00
2014-09-08 20:19:40 +00: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]'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts single disable item " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , disabled : 1
2009-12-11 15:09:00 +00:00
2014-09-08 20:19:40 +00: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]'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts a proc to disabled items " do
2013-04-13 22:50:14 +00:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Tag #{ i } " ] }
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , disabled : proc { | i | i . first == 1 }
2009-12-11 16:37:33 +00:00
2014-09-08 20:19:40 +00: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]'
2009-12-11 16:37:33 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box accepts html options " do
2009-12-11 15:09:00 +00:00
collection = [ [ 1 , 'Tag 1' ] , [ 2 , 'Tag 2' ] ]
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :tag_ids , collection , :first , :last , { } , class : 'check'
2009-12-11 15:09:00 +00:00
2014-09-08 20:19:40 +00:00
assert_select 'form input.check[type=checkbox][value="1"]'
assert_select 'form input.check[type=checkbox][value="2"]'
2009-12-11 15:09:00 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check box with fields for " do
2009-12-11 16:37:33 +00:00
collection = [ Tag . new ( 1 , 'Tag 1' ) , Tag . new ( 2 , 'Tag 2' ) ]
2010-09-26 22:24:30 +00:00
with_concat_form_for ( @user ) do | f |
2010-09-26 22:16:25 +00:00
f . fields_for ( :post ) do | p |
p . collection_check_boxes :tag_ids , collection , :id , :name
end
2010-09-26 22:24:30 +00:00
end
2009-12-11 16:37:33 +00:00
2014-09-08 20:19:40 +00: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"]'
2009-12-11 16:37:33 +00:00
2010-01-09 17:12:46 +00: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 16:37:33 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes wraps the collection in the given collection wrapper tag " do
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s , collection_wrapper_tag : :ul
2010-11-21 16:51:49 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form ul input[type=checkbox]' , count : 2
2011-07-26 18:45:36 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes does not render any wrapper tag by default " do
2010-11-21 16:51:49 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=checkbox]' , count : 2
2010-11-21 16:51:49 +00:00
assert_no_select 'form ul'
end
2013-01-02 13:20:30 +00:00
test " collection check boxes does not wrap the collection when given falsy values " do
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s , collection_wrapper_tag : false
2011-10-10 17:06:53 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=checkbox]' , count : 2
2012-01-26 16:55:12 +00:00
assert_no_select 'form ul'
2011-10-10 17:06:53 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes uses the given class for collection wrapper tag " do
2012-01-26 16:55:12 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
collection_wrapper_tag : :ul , collection_wrapper_class : " items-list "
2011-10-10 17:06:53 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form ul.items-list input[type=checkbox]' , count : 2
2011-10-10 17:06:53 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes uses no class for collection wrapper tag when no wrapper tag is given " do
2012-01-26 16:55:12 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
collection_wrapper_class : " items-list "
2011-10-20 11:48:59 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=checkbox]' , count : 2
2012-01-26 16:55:12 +00:00
assert_no_select 'form ul'
assert_no_select '.items-list'
2011-10-20 11:48:59 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes uses no class for collection wrapper tag by default " do
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s , collection_wrapper_tag : :ul
2011-10-10 17:06:53 +00:00
2012-01-26 16:55:12 +00:00
assert_select 'form ul'
assert_no_select 'form ul[class]'
2011-10-10 17:06:53 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes wrap items in a span tag by default " do
2012-01-26 16:55:12 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s
2010-11-21 16:51:49 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form span input[type=checkbox]' , count : 2
2010-11-21 16:51:49 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes wraps each item in the given item wrapper tag " do
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s , item_wrapper_tag : :li
2010-11-21 16:51:49 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form li input[type=checkbox]' , count : 2
2010-11-21 16:51:49 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes does not wrap each item when given explicitly falsy value " do
2013-01-28 21:02:59 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s , item_wrapper_tag : false
2011-07-26 18:45:36 +00:00
2012-01-26 16:55:12 +00:00
assert_select 'form input[type=checkbox]'
assert_no_select 'form span input[type=checkbox]'
2011-04-06 03:12:07 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes uses the given class for item wrapper tag " do
2012-01-27 18:09:45 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
item_wrapper_tag : :li , item_wrapper_class : " inline "
2012-01-27 18:09:45 +00:00
2013-01-28 21:02:59 +00:00
assert_select " form li.inline input[type=checkbox] " , count : 2
2012-01-27 18:09:45 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes uses no class for item wrapper tag when no wrapper tag is given " do
2012-01-27 18:09:45 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
item_wrapper_tag : nil , item_wrapper_class : " inline "
2012-01-27 18:09:45 +00:00
2013-01-28 21:02:59 +00:00
assert_select 'form input[type=checkbox]' , count : 2
2012-01-27 18:09:45 +00:00
assert_no_select 'form li'
assert_no_select '.inline'
end
2013-01-02 13:20:30 +00:00
test " collection check boxes uses no class for item wrapper tag by default " do
2012-01-27 18:09:45 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s ,
2013-01-28 21:02:59 +00:00
item_wrapper_tag : :li
2012-01-27 18:09:45 +00:00
2013-01-28 21:02:59 +00:00
assert_select " form li " , count : 2
2012-01-27 18:09:45 +00:00
assert_no_select " form li[class] "
end
2013-01-02 13:20:30 +00:00
test " collection check box does not wrap input inside the label " do
2010-11-21 16:51:49 +00:00
with_collection_check_boxes @user , :active , [ true , false ] , :to_s , :to_s
2012-01-26 17:34:23 +00:00
assert_select 'form input[type=checkbox] + label'
2011-04-06 03:33:41 +00:00
assert_no_select 'form label input'
2010-11-21 16:51:49 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes accepts a block to render the label as check box wrapper " do
2012-02-14 01:50:16 +00:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label { b . check_box }
2012-01-26 17:34:23 +00:00
end
2012-02-14 01:50:16 +00:00
assert_select 'label[for=user_active_true] > input#user_active_true[type=checkbox]'
assert_select 'label[for=user_active_false] > input#user_active_false[type=checkbox]'
2012-01-26 17:34:23 +00:00
end
2013-01-02 13:20:30 +00:00
test " collection check boxes accepts a block to change the order of label and check box " do
2012-02-14 01:50:16 +00:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label + b . check_box
end
assert_select 'label[for=user_active_true] + input#user_active_true[type=checkbox]'
assert_select 'label[for=user_active_false] + input#user_active_false[type=checkbox]'
end
2013-01-02 13:20:30 +00:00
test " collection check boxes with block helpers accept extra html options " do
2012-02-14 01:50:16 +00:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
2013-01-28 21:02:59 +00:00
b . label ( class : " check_box " ) + b . check_box ( class : " check_box " )
2012-02-14 01:50:16 +00:00
end
assert_select 'label.check_box[for=user_active_true] + input#user_active_true.check_box[type=checkbox]'
assert_select 'label.check_box[for=user_active_false] + input#user_active_false.check_box[type=checkbox]'
end
2013-01-02 13:20:30 +00:00
test " collection check boxes with block helpers allows access to current text and value " do
2012-02-14 01:50:16 +00:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label ( :" data-value " = > b . value ) { b . check_box + b . text }
end
assert_select 'label[for=user_active_true][data-value=true]' , 'true' do
assert_select 'input#user_active_true[type=checkbox]'
end
assert_select 'label[for=user_active_false][data-value=false]' , 'false' do
assert_select 'input#user_active_false[type=checkbox]'
end
end
2013-01-02 13:20:30 +00:00
test " collection check boxes with block helpers allows access to the current object item in the collection to access extra properties " do
2012-02-14 01:50:16 +00:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
2013-01-28 21:02:59 +00:00
b . label ( class : b . object ) { b . check_box + b . text }
2012-02-14 01:50:16 +00:00
end
assert_select 'label.true[for=user_active_true]' , 'true' do
assert_select 'input#user_active_true[type=checkbox]'
end
assert_select 'label.false[for=user_active_false]' , 'false' do
assert_select 'input#user_active_false[type=checkbox]'
end
end
2013-01-22 01:41:18 +00:00
test " collection check boxes with block helpers does not leak the template " do
with_concat_form_for ( @user ) do | f |
collection_input = f . collection_check_boxes :active , [ true , false ] , :to_s , :to_s do | b |
2013-01-28 21:02:59 +00:00
b . label ( class : b . object ) { b . check_box + b . text }
2013-01-22 01:41:18 +00:00
end
concat collection_input
concat f . hidden_field :name
end
assert_select 'label.true[for=user_active_true]' , text : 'true' , count : 1 do
assert_select 'input#user_active_true[type=checkbox]'
end
assert_select 'label.false[for=user_active_false]' , text : 'false' , count : 1 do
assert_select 'input#user_active_false[type=checkbox]'
end
end
2009-12-11 15:09:00 +00:00
# SIMPLE FIELDS
2013-01-02 13:20:30 +00:00
test " simple fields for is available and yields an instance of FormBuilder " do
2010-09-26 22:24:30 +00:00
with_concat_form_for ( @user ) do | f |
2010-09-26 22:16:25 +00:00
f . simple_fields_for ( :posts ) do | posts_form |
2009-12-10 10:44:34 +00:00
assert posts_form . instance_of? ( SimpleForm :: FormBuilder )
2010-09-26 22:16:25 +00:00
end
2010-09-26 22:24:30 +00:00
end
2009-12-10 10:44:34 +00:00
end
2011-03-02 02:05:58 +00:00
2013-01-02 13:20:30 +00:00
test " fields for with a hash like model yeilds an instance of FormBuilder " do
2011-07-19 15:25:40 +00:00
with_concat_form_for ( :user ) do | f |
2012-09-11 15:56:33 +00:00
f . simple_fields_for ( :author , HashBackedAuthor . new ) do | author |
2011-07-19 15:25:40 +00:00
assert author . instance_of? ( SimpleForm :: FormBuilder )
author . input :name
end
end
assert_select " input[name='user[author][name]'][value='hash backed author'] "
end
2013-01-02 13:20:30 +00:00
test " fields for yields an instance of CustomBuilder if main builder is a CustomBuilder " do
2011-03-02 02:05:58 +00:00
with_custom_form_for ( :user ) do | f |
f . simple_fields_for ( :company ) do | company |
assert company . instance_of? ( CustomFormBuilder )
end
end
end
2013-01-02 13:20:30 +00:00
test " fields for yields an instance of FormBuilder if it was set in options " do
2011-03-02 02:05:58 +00:00
with_custom_form_for ( :user ) do | f |
2013-01-28 21:02:59 +00:00
f . simple_fields_for ( :company , builder : SimpleForm :: FormBuilder ) do | company |
2011-03-02 02:05:58 +00:00
assert company . instance_of? ( SimpleForm :: FormBuilder )
end
end
end
2011-11-27 10:46:08 +00:00
2014-05-14 17:17:54 +00:00
test " fields inherits wrapper option from the parent form " do
2011-11-27 10:46:08 +00:00
swap_wrapper :another do
2013-01-28 21:02:59 +00:00
simple_form_for ( :user , wrapper : :another ) do | f |
2011-11-27 10:46:08 +00:00
f . simple_fields_for ( :company ) do | company |
assert_equal :another , company . options [ :wrapper ]
end
end
end
end
2013-01-02 13:20:30 +00:00
2013-01-02 13:21:39 +00:00
test " fields overrides wrapper option from the parent form " do
2012-12-29 18:04:13 +00:00
swap_wrapper :another do
2013-01-28 21:02:59 +00:00
simple_form_for ( :user , wrapper : :another ) do | f |
f . simple_fields_for ( :company , wrapper : false ) do | company |
2013-01-02 13:21:39 +00:00
assert_equal false , company . options [ :wrapper ]
2012-12-29 18:04:13 +00:00
end
end
end
end
2009-12-09 18:26:30 +00:00
end