2008-01-05 08:32:06 -05:00
require 'abstract_unit'
2009-09-24 21:36:40 -04:00
require 'tzinfo'
2004-11-23 20:04:44 -05:00
2011-03-05 22:47:15 -05:00
class Map < Hash
def category
" <mus> "
end
end
2008-07-02 14:09:10 -04:00
TZInfo :: Timezone . cattr_reader :loaded_zones
2009-02-03 21:25:37 -05:00
class FormOptionsHelperTest < ActionView :: TestCase
tests ActionView :: Helpers :: FormOptionsHelper
silence_warnings do
2011-01-05 14:15:10 -05:00
Post = Struct . new ( 'Post' , :title , :author_name , :body , :secret , :written_on , :category , :origin , :allow_comments )
2009-02-03 21:25:37 -05:00
Continent = Struct . new ( 'Continent' , :continent_name , :countries )
Country = Struct . new ( 'Country' , :country_id , :country_name )
Firm = Struct . new ( 'Firm' , :time_zone )
Album = Struct . new ( 'Album' , :id , :title , :genre )
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def setup
2013-02-21 11:07:32 -05:00
@fake_timezones = %w( A B C D E ) . map do | id |
2009-02-03 21:25:37 -05:00
tz = TZInfo :: Timezone . loaded_zones [ id ] = stub ( :name = > id , :to_s = > id )
ActiveSupport :: TimeZone . stubs ( :[] ) . with ( id ) . returns ( tz )
2013-02-21 11:07:32 -05:00
tz
2008-07-02 14:09:10 -04:00
end
2009-02-03 21:25:37 -05:00
ActiveSupport :: TimeZone . stubs ( :all ) . returns ( @fake_timezones )
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_collection_options
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
2009-02-13 19:14:48 -05:00
options_from_collection_for_select ( dummy_posts , " author_name " , " title " )
2009-02-03 21:25:37 -05:00
)
end
2005-03-06 06:50:41 -05:00
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_collection_options_with_preselected_value
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" selected= \" selected \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
2009-02-13 19:14:48 -05:00
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , " Babe " )
2009-02-03 21:25:37 -05:00
)
end
def test_collection_options_with_preselected_value_array
2005-09-20 03:54:55 -04:00
assert_dom_equal (
2009-02-03 21:25:37 -05:00
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" selected= \" selected \" >Babe went home</option> \n <option value= \" Cabe \" selected= \" selected \" >Cabe went home</option> " ,
2009-02-13 19:14:48 -05:00
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , [ " Babe " , " Cabe " ] )
2004-11-23 20:04:44 -05:00
)
2009-02-03 21:25:37 -05:00
end
2004-11-23 20:04:44 -05:00
2009-02-13 19:37:24 -05:00
def test_collection_options_with_proc_for_selected
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" selected= \" selected \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , lambda { | p | p . author_name == 'Babe' } )
)
end
def test_collection_options_with_disabled_value
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" disabled= \" disabled \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , :disabled = > " Babe " )
)
end
def test_collection_options_with_disabled_array
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" disabled= \" disabled \" >Babe went home</option> \n <option value= \" Cabe \" disabled= \" disabled \" >Cabe went home</option> " ,
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , :disabled = > [ " Babe " , " Cabe " ] )
)
end
def test_collection_options_with_preselected_and_disabled_value
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" disabled= \" disabled \" >Babe went home</option> \n <option value= \" Cabe \" selected= \" selected \" >Cabe went home</option> " ,
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , :selected = > " Cabe " , :disabled = > " Babe " )
)
end
def test_collection_options_with_proc_for_disabled
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" disabled= \" disabled \" >Babe went home</option> \n <option value= \" Cabe \" disabled= \" disabled \" >Cabe went home</option> " ,
2012-08-05 18:27:56 -04:00
options_from_collection_for_select ( dummy_posts , " author_name " , " title " , :disabled = > lambda { | p | %w( Babe Cabe ) . include? ( p . author_name ) } )
2009-02-13 19:37:24 -05:00
)
end
2012-02-01 14:43:44 -05:00
def test_collection_options_with_proc_for_value_method
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
options_from_collection_for_select ( dummy_posts , lambda { | p | p . author_name } , " title " )
)
end
def test_collection_options_with_proc_for_text_method
assert_dom_equal (
" <option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option> " ,
options_from_collection_for_select ( dummy_posts , " author_name " , lambda { | p | p . title } )
)
end
2013-04-11 07:15:33 -04:00
def test_collection_options_with_element_attributes
assert_dom_equal (
" <option value= \" USA \" class= \" bold \" >USA</option> " ,
options_from_collection_for_select ( [ [ " USA " , " USA " , { :class = > 'bold' } ] ] , :first , :second )
)
end
2009-04-01 06:44:56 -04:00
def test_string_options_for_select
options = " <option value= \" Denmark \" >Denmark</option><option value= \" USA \" >USA</option><option value= \" Sweden \" >Sweden</option> "
assert_dom_equal (
options ,
options_for_select ( options )
)
end
2009-02-03 21:25:37 -05:00
def test_array_options_for_select
assert_dom_equal (
" <option value= \" <Denmark> \" ><Denmark></option> \n <option value= \" USA \" >USA</option> \n <option value= \" Sweden \" >Sweden</option> " ,
options_for_select ( [ " <Denmark> " , " USA " , " Sweden " ] )
)
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_array_options_for_select_with_selection
assert_dom_equal (
" <option value= \" Denmark \" >Denmark</option> \n <option value= \" <USA> \" selected= \" selected \" ><USA></option> \n <option value= \" Sweden \" >Sweden</option> " ,
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , " <USA> " )
)
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_array_options_for_select_with_selection_array
2005-09-20 03:54:55 -04:00
assert_dom_equal (
2009-02-03 21:25:37 -05:00
" <option value= \" Denmark \" >Denmark</option> \n <option value= \" <USA> \" selected= \" selected \" ><USA></option> \n <option value= \" Sweden \" selected= \" selected \" >Sweden</option> " ,
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , [ " <USA> " , " Sweden " ] )
2004-11-23 20:04:44 -05:00
)
2009-02-03 21:25:37 -05:00
end
2008-07-02 14:09:10 -04:00
2009-02-13 19:37:24 -05:00
def test_array_options_for_select_with_disabled_value
assert_dom_equal (
" <option value= \" Denmark \" >Denmark</option> \n <option value= \" <USA> \" disabled= \" disabled \" ><USA></option> \n <option value= \" Sweden \" >Sweden</option> " ,
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , :disabled = > " <USA> " )
)
end
def test_array_options_for_select_with_disabled_array
assert_dom_equal (
" <option value= \" Denmark \" >Denmark</option> \n <option value= \" <USA> \" disabled= \" disabled \" ><USA></option> \n <option value= \" Sweden \" disabled= \" disabled \" >Sweden</option> " ,
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , :disabled = > [ " <USA> " , " Sweden " ] )
)
end
def test_array_options_for_select_with_selection_and_disabled_value
assert_dom_equal (
" <option value= \" Denmark \" selected= \" selected \" >Denmark</option> \n <option value= \" <USA> \" disabled= \" disabled \" ><USA></option> \n <option value= \" Sweden \" >Sweden</option> " ,
options_for_select ( [ " Denmark " , " <USA> " , " Sweden " ] , :selected = > " Denmark " , :disabled = > " <USA> " )
)
end
2011-01-05 14:15:10 -05:00
def test_boolean_array_options_for_select_with_selection_and_disabled_value
assert_dom_equal (
" <option value= \" true \" >true</option> \n <option value= \" false \" selected= \" selected \" >false</option> " ,
options_for_select ( [ true , false ] , :selected = > false , :disabled = > nil )
)
end
2012-01-21 11:13:47 -05:00
def test_range_options_for_select
assert_dom_equal (
" <option value= \" 1 \" >1</option> \n <option value= \" 2 \" >2</option> \n <option value= \" 3 \" >3</option> " ,
options_for_select ( 1 .. 3 )
)
end
2009-02-03 21:25:37 -05:00
def test_array_options_for_string_include_in_other_string_bug_fix
2005-09-20 03:54:55 -04:00
assert_dom_equal (
2009-02-03 21:25:37 -05:00
" <option value= \" ruby \" >ruby</option> \n <option value= \" rubyonrails \" selected= \" selected \" >rubyonrails</option> " ,
options_for_select ( [ " ruby " , " rubyonrails " ] , " rubyonrails " )
2005-03-14 19:13:14 -05:00
)
2005-09-20 03:54:55 -04:00
assert_dom_equal (
2009-02-03 21:25:37 -05:00
" <option value= \" ruby \" selected= \" selected \" >ruby</option> \n <option value= \" rubyonrails \" >rubyonrails</option> " ,
options_for_select ( [ " ruby " , " rubyonrails " ] , " ruby " )
2005-03-14 19:13:14 -05:00
)
2006-05-09 01:19:32 -04:00
assert_dom_equal (
2009-02-03 21:25:37 -05:00
%( <option value="ruby" selected="selected">ruby</option> \n <option value="rubyonrails">rubyonrails</option> \n <option value=""></option> ) ,
options_for_select ( [ " ruby " , " rubyonrails " , nil ] , " ruby " )
2006-05-09 01:19:32 -04:00
)
2009-02-03 21:25:37 -05:00
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_hash_options_for_select
assert_dom_equal (
2012-02-20 14:55:46 -05:00
" <option value= \" Dollar \" >$</option> \n <option value= \" <Kroner> \" ><DKR></option> " ,
options_for_select ( " $ " = > " Dollar " , " <DKR> " = > " <Kroner> " ) . split ( " \n " ) . join ( " \n " )
2009-02-03 21:25:37 -05:00
)
assert_dom_equal (
2012-02-20 14:55:46 -05:00
" <option value= \" Dollar \" selected= \" selected \" >$</option> \n <option value= \" <Kroner> \" ><DKR></option> " ,
options_for_select ( { " $ " = > " Dollar " , " <DKR> " = > " <Kroner> " } , " Dollar " ) . split ( " \n " ) . join ( " \n " )
2009-02-03 21:25:37 -05:00
)
assert_dom_equal (
2012-02-20 14:55:46 -05:00
" <option value= \" Dollar \" selected= \" selected \" >$</option> \n <option value= \" <Kroner> \" selected= \" selected \" ><DKR></option> " ,
options_for_select ( { " $ " = > " Dollar " , " <DKR> " = > " <Kroner> " } , [ " Dollar " , " <Kroner> " ] ) . split ( " \n " ) . join ( " \n " )
2009-02-03 21:25:37 -05:00
)
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_ducktyped_options_for_select
quack = Struct . new ( :first , :last )
assert_dom_equal (
" <option value= \" <Kroner> \" ><DKR></option> \n <option value= \" Dollar \" >$</option> " ,
options_for_select ( [ quack . new ( " <DKR> " , " <Kroner> " ) , quack . new ( " $ " , " Dollar " ) ] )
)
assert_dom_equal (
" <option value= \" <Kroner> \" ><DKR></option> \n <option value= \" Dollar \" selected= \" selected \" >$</option> " ,
options_for_select ( [ quack . new ( " <DKR> " , " <Kroner> " ) , quack . new ( " $ " , " Dollar " ) ] , " Dollar " )
)
assert_dom_equal (
" <option value= \" <Kroner> \" selected= \" selected \" ><DKR></option> \n <option value= \" Dollar \" selected= \" selected \" >$</option> " ,
options_for_select ( [ quack . new ( " <DKR> " , " <Kroner> " ) , quack . new ( " $ " , " Dollar " ) ] , [ " Dollar " , " <Kroner> " ] )
)
end
2004-11-23 20:04:44 -05:00
2010-08-14 10:55:51 -04:00
def test_collection_options_with_preselected_value_as_string_and_option_value_is_integer
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( 1 , " first " , " rap " ) , Album . new ( 2 , " second " , " pop " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option selected="selected" value="1">rap</option> \n <option value="2">pop</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , :selected = > " 1 " )
)
end
def test_collection_options_with_preselected_value_as_integer_and_option_value_is_string
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( " 1 " , " first " , " rap " ) , Album . new ( " 2 " , " second " , " pop " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option selected="selected" value="1">rap</option> \n <option value="2">pop</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , :selected = > 1 )
)
end
def test_collection_options_with_preselected_value_as_string_and_option_value_is_float
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( 1 . 0 , " first " , " rap " ) , Album . new ( 2 . 0 , " second " , " pop " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option value="1.0">rap</option> \n <option value="2.0" selected="selected">pop</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , :selected = > " 2.0 " )
)
end
def test_collection_options_with_preselected_value_as_nil
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( 1 . 0 , " first " , " rap " ) , Album . new ( 2 . 0 , " second " , " pop " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option value="1.0">rap</option> \n <option value="2.0">pop</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , :selected = > nil )
)
end
def test_collection_options_with_disabled_value_as_nil
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( 1 . 0 , " first " , " rap " ) , Album . new ( 2 . 0 , " second " , " pop " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option value="1.0">rap</option> \n <option value="2.0">pop</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , :disabled = > nil )
)
end
def test_collection_options_with_disabled_value_as_array
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( 1 . 0 , " first " , " rap " ) , Album . new ( 2 . 0 , " second " , " pop " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option disabled="disabled" value="1.0">rap</option> \n <option disabled="disabled" value="2.0">pop</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , :disabled = > [ " 1.0 " , 2 . 0 ] )
)
end
def test_collection_options_with_preselected_values_as_string_array_and_option_value_is_float
2011-01-05 14:15:10 -05:00
albums = [ Album . new ( 1 . 0 , " first " , " rap " ) , Album . new ( 2 . 0 , " second " , " pop " ) , Album . new ( 3 . 0 , " third " , " country " ) ]
2010-08-14 10:55:51 -04:00
assert_dom_equal (
%( <option value="1.0" selected="selected">rap</option> \n <option value="2.0">pop</option> \n <option value="3.0" selected="selected">country</option> ) ,
options_from_collection_for_select ( albums , " id " , " genre " , [ " 1.0 " , " 3.0 " ] )
)
end
2009-02-03 21:25:37 -05:00
def test_option_groups_from_collection_for_select
assert_dom_equal (
" <optgroup label= \" <Africa> \" ><option value= \" <sa> \" ><South Africa></option> \n <option value= \" so \" >Somalia</option></optgroup><optgroup label= \" Europe \" ><option value= \" dk \" selected= \" selected \" >Denmark</option> \n <option value= \" ie \" >Ireland</option></optgroup> " ,
2010-06-17 03:17:31 -04:00
option_groups_from_collection_for_select ( dummy_continents , " countries " , " continent_name " , " country_id " , " country_name " , " dk " )
2009-02-03 21:25:37 -05:00
)
end
2009-01-29 12:59:44 -05:00
2010-06-17 03:17:31 -04:00
def test_option_groups_from_collection_for_select_returns_html_safe_string
assert option_groups_from_collection_for_select ( dummy_continents , " countries " , " continent_name " , " country_id " , " country_name " , " dk " ) . html_safe?
end
2009-02-03 21:25:37 -05:00
def test_grouped_options_for_select_with_array
assert_dom_equal (
" <optgroup label= \" North America \" ><option value= \" US \" >United States</option> \n <option value= \" Canada \" >Canada</option></optgroup><optgroup label= \" Europe \" ><option value= \" GB \" >Great Britain</option> \n <option value= \" Germany \" >Germany</option></optgroup> " ,
grouped_options_for_select ( [
[ " North America " ,
[ [ 'United States' , 'US' ] , " Canada " ] ] ,
[ " Europe " ,
[ [ " Great Britain " , " GB " ] , " Germany " ] ]
] )
)
2013-07-20 08:36:27 -04:00
end
def test_grouped_options_for_select_with_array_and_html_attributes
assert_dom_equal (
" <optgroup label= \" North America \" data-foo= \" bar \" ><option value= \" US \" >United States</option> \n <option value= \" Canada \" >Canada</option></optgroup><optgroup label= \" Europe \" disabled= \" disabled \" ><option value= \" GB \" >Great Britain</option> \n <option value= \" Germany \" >Germany</option></optgroup> " ,
grouped_options_for_select ( [
[ " North America " , [ [ 'United States' , 'US' ] , " Canada " ] , :data = > { :foo = > 'bar' } ] ,
[ " Europe " , [ [ " Great Britain " , " GB " ] , " Germany " ] , :disabled = > 'disabled' ]
] )
)
2009-02-03 21:25:37 -05:00
end
2009-01-29 12:59:44 -05:00
2012-05-15 13:45:12 -04:00
def test_grouped_options_for_select_with_optional_divider
2009-02-03 21:25:37 -05:00
assert_dom_equal (
2012-05-15 13:45:12 -04:00
" <optgroup label= \" ---------- \" ><option value= \" US \" >US</option> \n <option value= \" Canada \" >Canada</option></optgroup><optgroup label= \" ---------- \" ><option value= \" GB \" >GB</option> \n <option value= \" Germany \" >Germany</option></optgroup> " ,
2012-05-19 08:49:01 -04:00
grouped_options_for_select ( [ [ 'US' , " Canada " ] , [ " GB " , " Germany " ] ] , nil , divider : " ---------- " )
2012-05-15 13:45:12 -04:00
)
end
def test_grouped_options_for_select_with_selected_and_prompt
assert_dom_equal (
" <option value= \" \" >Choose a product...</option><optgroup label= \" Hats \" ><option value= \" Baseball Cap \" >Baseball Cap</option> \n <option selected= \" selected \" value= \" Cowboy Hat \" >Cowboy Hat</option></optgroup> " ,
grouped_options_for_select ( [ [ " Hats " , [ " Baseball Cap " , " Cowboy Hat " ] ] ] , " Cowboy Hat " , prompt : " Choose a product... " )
2009-02-03 21:25:37 -05:00
)
end
2009-01-29 12:59:44 -05:00
2012-05-16 19:52:27 -04:00
def test_grouped_options_for_select_with_selected_and_prompt_true
assert_dom_equal (
" <option value= \" \" >Please select</option><optgroup label= \" Hats \" ><option value= \" Baseball Cap \" >Baseball Cap</option> \n <option selected= \" selected \" value= \" Cowboy Hat \" >Cowboy Hat</option></optgroup> " ,
grouped_options_for_select ( [ [ " Hats " , [ " Baseball Cap " , " Cowboy Hat " ] ] ] , " Cowboy Hat " , prompt : true )
)
end
2010-04-04 00:31:52 -04:00
def test_grouped_options_for_select_returns_html_safe_string
assert grouped_options_for_select ( [ [ " Hats " , [ " Baseball Cap " , " Cowboy Hat " ] ] ] ) . html_safe?
end
2010-07-14 02:23:41 -04:00
def test_grouped_options_for_select_with_prompt_returns_html_escaped_string
assert_dom_equal (
" <option value= \" \" ><Choose One></option><optgroup label= \" Hats \" ><option value= \" Baseball Cap \" >Baseball Cap</option> \n <option value= \" Cowboy Hat \" >Cowboy Hat</option></optgroup> " ,
2012-05-15 13:45:12 -04:00
grouped_options_for_select ( [ [ " Hats " , [ " Baseball Cap " , " Cowboy Hat " ] ] ] , nil , prompt : '<Choose One>' ) )
2010-07-14 02:23:41 -04:00
end
2009-02-03 21:25:37 -05:00
def test_optgroups_with_with_options_with_hash
assert_dom_equal (
2012-11-27 11:00:31 -05:00
" <optgroup label= \" North America \" ><option value= \" United States \" >United States</option> \n <option value= \" Canada \" >Canada</option></optgroup><optgroup label= \" Europe \" ><option value= \" Denmark \" >Denmark</option> \n <option value= \" Germany \" >Germany</option></optgroup> " ,
2009-02-03 21:25:37 -05:00
grouped_options_for_select ( { 'North America' = > [ 'United States' , 'Canada' ] , 'Europe' = > [ 'Denmark' , 'Germany' ] } )
)
end
2005-02-23 07:54:58 -05:00
2013-02-21 11:07:32 -05:00
def test_time_zone_options_no_params
2009-02-03 21:25:37 -05:00
opts = time_zone_options_for_select
assert_dom_equal " <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> \n " +
" <option value= \" E \" >E</option> " ,
opts
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_options_with_selected
opts = time_zone_options_for_select ( " D " )
assert_dom_equal " <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " ,
opts
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_options_with_unknown_selected
opts = time_zone_options_for_select ( " K " )
assert_dom_equal " <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> \n " +
" <option value= \" E \" >E</option> " ,
opts
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_options_with_priority_zones
zones = [ ActiveSupport :: TimeZone . new ( " B " ) , ActiveSupport :: TimeZone . new ( " E " ) ]
opts = time_zone_options_for_select ( nil , zones )
assert_dom_equal " <option value= \" B \" >B</option> \n " +
" <option value= \" E \" >E</option> " +
" <option value= \" \" disabled= \" disabled \" >-------------</option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> " ,
opts
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_options_with_selected_priority_zones
zones = [ ActiveSupport :: TimeZone . new ( " B " ) , ActiveSupport :: TimeZone . new ( " E " ) ]
opts = time_zone_options_for_select ( " E " , zones )
assert_dom_equal " <option value= \" B \" >B</option> \n " +
" <option value= \" E \" selected= \" selected \" >E</option> " +
" <option value= \" \" disabled= \" disabled \" >-------------</option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" >D</option> " ,
opts
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_options_with_unselected_priority_zones
zones = [ ActiveSupport :: TimeZone . new ( " B " ) , ActiveSupport :: TimeZone . new ( " E " ) ]
opts = time_zone_options_for_select ( " C " , zones )
assert_dom_equal " <option value= \" B \" >B</option> \n " +
" <option value= \" E \" >E</option> " +
" <option value= \" \" disabled= \" disabled \" >-------------</option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" C \" selected= \" selected \" >C</option> \n " +
" <option value= \" D \" >D</option> " ,
opts
end
2013-02-21 08:39:28 -05:00
2013-02-19 20:51:23 -05:00
def test_time_zone_options_with_priority_zones_does_not_mutate_time_zones
original_zones = ActiveSupport :: TimeZone . all . dup
zones = [ ActiveSupport :: TimeZone . new ( " B " ) , ActiveSupport :: TimeZone . new ( " E " ) ]
2013-02-21 08:39:28 -05:00
time_zone_options_for_select ( nil , zones )
2013-02-19 20:51:23 -05:00
assert_equal original_zones , ActiveSupport :: TimeZone . all
end
2004-11-23 20:04:44 -05:00
2010-08-15 08:36:29 -04:00
def test_time_zone_options_returns_html_safe_string
assert time_zone_options_for_select . html_safe?
end
2009-02-03 21:25:37 -05:00
def test_select
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) )
)
end
2008-06-08 23:05:39 -04:00
2011-06-30 09:27:01 -04:00
def test_select_without_multiple
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ></select> " ,
select ( :post , :category , " " , { } , :multiple = > false )
)
end
2011-06-14 15:45:22 -04:00
def test_select_with_grouped_collection_as_nested_array
@post = Post . new
countries_by_continent = [
[ " <Africa> " , [ [ " <South Africa> " , " <sa> " ] , [ " Somalia " , " so " ] ] ] ,
[ " Europe " , [ [ " Denmark " , " dk " ] , [ " Ireland " , " ie " ] ] ] ,
]
assert_dom_equal (
[
%Q{ <select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option> } ,
%Q{ <option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk">Denmark</option> } ,
%Q{ <option value="ie">Ireland</option></optgroup></select> } ,
] . join ( " \n " ) ,
select ( " post " , " origin " , countries_by_continent )
)
end
def test_select_with_grouped_collection_as_hash
@post = Post . new
countries_by_continent = {
" <Africa> " = > [ [ " <South Africa> " , " <sa> " ] , [ " Somalia " , " so " ] ] ,
" Europe " = > [ [ " Denmark " , " dk " ] , [ " Ireland " , " ie " ] ] ,
}
assert_dom_equal (
[
%Q{ <select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option> } ,
%Q{ <option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk">Denmark</option> } ,
%Q{ <option value="ie">Ireland</option></optgroup></select> } ,
] . join ( " \n " ) ,
select ( " post " , " origin " , countries_by_continent )
)
end
2011-01-05 14:15:10 -05:00
def test_select_with_boolean_method
@post = Post . new
@post . allow_comments = false
assert_dom_equal (
" <select id= \" post_allow_comments \" name= \" post[allow_comments] \" ><option value= \" true \" >true</option> \n <option value= \" false \" selected= \" selected \" >false</option></select> " ,
select ( " post " , " allow_comments " , %w( true false ) )
)
end
2009-02-03 21:25:37 -05:00
def test_select_under_fields_for
@post = Post . new
@post . category = " <mus> "
2005-11-13 06:13:11 -05:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :post , @post do | f |
2009-02-03 21:25:37 -05:00
concat f . select ( :category , %w( abe <mus> hest ) )
end
2010-08-14 01:13:00 -04:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
output_buffer
)
end
2008-07-13 19:55:57 -04:00
2011-03-05 22:47:15 -05:00
def test_fields_for_with_record_inherited_from_hash
map = Map . new
output_buffer = fields_for :map , map do | f |
concat f . select ( :category , %w( abe <mus> hest ) )
end
assert_dom_equal (
" <select id= \" map_category \" name= \" map[category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
output_buffer
)
end
2009-02-03 21:25:37 -05:00
def test_select_under_fields_for_with_index
@post = Post . new
@post . category = " <mus> "
2008-07-13 19:55:57 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :post , @post , :index = > 108 do | f |
2009-02-03 21:25:37 -05:00
concat f . select ( :category , %w( abe <mus> hest ) )
2008-07-13 19:55:57 -04:00
end
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_108_category \" name= \" post[108][category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
output_buffer
)
end
2008-07-13 19:55:57 -04:00
2009-02-03 21:25:37 -05:00
def test_select_under_fields_for_with_auto_index
@post = Post . new
@post . category = " <mus> "
def @post . to_param ; 108 ; end
2008-07-13 19:55:57 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for " post[] " , @post do | f |
2009-02-03 21:25:37 -05:00
concat f . select ( :category , %w( abe <mus> hest ) )
2008-07-13 19:55:57 -04:00
end
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_108_category \" name= \" post[108][category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
output_buffer
)
end
2004-11-23 20:04:44 -05:00
2009-04-01 06:44:56 -04:00
def test_select_under_fields_for_with_string_and_given_prompt
@post = Post . new
2012-02-20 18:19:28 -05:00
options = " <option value= \" abe \" >abe</option><option value= \" mus \" >mus</option><option value= \" hest \" >hest</option> " . html_safe
2009-04-01 06:44:56 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :post , @post do | f |
2009-04-01 06:44:56 -04:00
concat f . select ( :category , options , :prompt = > 'The prompt' )
end
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >The prompt</option> \n #{ options } </select> " ,
output_buffer
)
end
2011-06-08 05:59:54 -04:00
def test_select_with_multiple_to_add_hidden_input
output_buffer = select ( :post , :category , " " , { } , :multiple = > true )
assert_dom_equal (
" <input type= \" hidden \" name= \" post[category][] \" value= \" \" /><select multiple= \" multiple \" id= \" post_category \" name= \" post[category][] \" ></select> " ,
output_buffer
)
end
2012-03-13 12:59:04 -04:00
def test_select_with_multiple_and_without_hidden_input
output_buffer = select ( :post , :category , " " , { :include_hidden = > false } , :multiple = > true )
assert_dom_equal (
" <select multiple= \" multiple \" id= \" post_category \" name= \" post[category][] \" ></select> " ,
2013-03-08 12:07:09 -05:00
output_buffer
)
end
def test_select_with_multiple_and_with_explicit_name_ending_with_brackets
output_buffer = select ( :post , :category , [ ] , { include_hidden : false } , multiple : true , name : 'post[category][]' )
assert_dom_equal (
" <select multiple= \" multiple \" id= \" post_category \" name= \" post[category][] \" ></select> " ,
2012-03-13 12:59:04 -04:00
output_buffer
)
end
2011-06-08 05:59:54 -04:00
def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input
output_buffer = select ( :post , :category , " " , { } , :multiple = > true , :disabled = > true )
assert_dom_equal (
" <input disabled= \" disabled \" type= \" hidden \" name= \" post[category][] \" value= \" \" /><select multiple= \" multiple \" disabled= \" disabled \" id= \" post_category \" name= \" post[category][] \" ></select> " ,
output_buffer
)
end
2009-02-03 21:25:37 -05:00
def test_select_with_blank
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" ></option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :include_blank = > true )
)
end
2007-05-18 01:29:22 -04:00
2009-02-03 21:25:37 -05:00
def test_select_with_blank_as_string
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >None</option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :include_blank = > 'None' )
)
end
2005-07-03 08:23:16 -04:00
2010-07-14 02:23:41 -04:00
def test_select_with_blank_as_string_escaped
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" ><None></option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :include_blank = > '<None>' )
)
end
2009-02-03 21:25:37 -05:00
def test_select_with_default_prompt
@post = Post . new
@post . category = " "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >Please select</option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :prompt = > true )
)
end
2005-07-03 08:23:16 -04:00
2009-02-03 21:25:37 -05:00
def test_select_no_prompt_when_select_has_value
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :prompt = > true )
)
end
2005-07-03 08:23:16 -04:00
2009-02-03 21:25:37 -05:00
def test_select_with_given_prompt
@post = Post . new
@post . category = " "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >The prompt</option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :prompt = > 'The prompt' )
)
end
2007-06-12 21:20:55 -04:00
2010-07-14 02:23:41 -04:00
def test_select_with_given_prompt_escaped
@post = Post . new
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" ><The prompt></option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :prompt = > '<The prompt>' )
)
end
2009-02-03 21:25:37 -05:00
def test_select_with_prompt_and_blank
@post = Post . new
@post . category = " "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >Please select</option> \n <option value= \" \" ></option> \n <option value= \" abe \" >abe</option> \n <option value= \" <mus> \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :prompt = > true , :include_blank = > true )
)
end
2011-10-07 19:56:18 -04:00
def test_empty
@post = Post . new
@post . category = " "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >Please select</option> \n <option value= \" \" ></option> \n </select> " ,
select ( " post " , " category " , [ ] , :prompt = > true , :include_blank = > true )
)
end
2011-12-20 15:19:55 -05:00
def test_select_with_nil
@post = Post . new
@post . category = " othervalue "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" ></option> \n <option value= \" othervalue \" selected= \" selected \" >othervalue</option></select> " ,
select ( " post " , " category " , [ nil , " othervalue " ] )
)
end
2012-05-12 23:41:26 -04:00
def test_required_select
assert_dom_equal (
%( <select id="post_category" name="post[category]" required="required"><option value=""></option> \n <option value="abe">abe</option> \n <option value="mus">mus</option> \n <option value="hest">hest</option></select> ) ,
select ( " post " , " category " , %w( abe mus hest ) , { } , required : true )
)
end
2012-05-12 23:42:19 -04:00
def test_required_select_with_include_blank_prompt
assert_dom_equal (
%( <select id="post_category" name="post[category]" required="required"><option value="">Select one</option> \n <option value="abe">abe</option> \n <option value="mus">mus</option> \n <option value="hest">hest</option></select> ) ,
select ( " post " , " category " , %w( abe mus hest ) , { include_blank : " Select one " } , required : true )
)
2012-05-12 23:41:26 -04:00
end
2012-05-12 23:47:34 -04:00
def test_required_select_with_prompt
assert_dom_equal (
%( <select id="post_category" name="post[category]" required="required"><option value="">Select one</option> \n <option value="abe">abe</option> \n <option value="mus">mus</option> \n <option value="hest">hest</option></select> ) ,
select ( " post " , " category " , %w( abe mus hest ) , { prompt : " Select one " } , required : true )
)
end
2012-05-12 23:41:26 -04:00
def test_required_select_display_size_equals_to_one
2012-05-11 17:15:40 -04:00
assert_dom_equal (
2012-05-12 23:41:26 -04:00
%( <select id="post_category" name="post[category]" required="required" size="1"><option value=""></option> \n <option value="abe">abe</option> \n <option value="mus">mus</option> \n <option value="hest">hest</option></select> ) ,
select ( " post " , " category " , %w( abe mus hest ) , { } , required : true , size : 1 )
2012-05-11 17:15:40 -04:00
)
end
2012-05-12 23:41:26 -04:00
def test_required_select_with_display_size_bigger_than_one
assert_dom_equal (
%( <select id="post_category" name="post[category]" required="required" size="2"><option value="abe">abe</option> \n <option value="mus">mus</option> \n <option value="hest">hest</option></select> ) ,
select ( " post " , " category " , %w( abe mus hest ) , { } , required : true , size : 2 )
)
2012-05-11 17:15:40 -04:00
end
2011-12-20 15:19:55 -05:00
2012-05-12 23:41:26 -04:00
def test_required_select_with_multiple_option
2012-05-11 17:15:40 -04:00
assert_dom_equal (
2012-05-12 23:41:26 -04:00
%( <input name="post[category][]" type="hidden" value=""/><select id="post_category" multiple="multiple" name="post[category][]" required="required"><option value="abe">abe</option> \n <option value="mus">mus</option> \n <option value="hest">hest</option></select> ) ,
select ( " post " , " category " , %w( abe mus hest ) , { } , required : true , multiple : true )
2012-05-11 17:15:40 -04:00
)
end
2012-05-12 23:41:26 -04:00
2011-12-20 15:19:55 -05:00
def test_select_with_fixnum
@post = Post . new
@post . category = " "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >Please select</option> \n <option value= \" \" ></option> \n <option value= \" 1 \" >1</option></select> " ,
select ( " post " , " category " , [ 1 ] , :prompt = > true , :include_blank = > true )
)
end
2011-10-25 20:44:17 -04:00
def test_list_of_lists
@post = Post . new
@post . category = " "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" \" >Please select</option> \n <option value= \" \" ></option> \n <option value= \" number \" >Number</option> \n <option value= \" text \" >Text</option> \n <option value= \" boolean \" >Yes/No</option></select> " ,
select ( " post " , " category " , [ [ " Number " , " number " ] , [ " Text " , " text " ] , [ " Yes/No " , " boolean " ] ] , :prompt = > true , :include_blank = > true )
)
end
2009-02-03 21:25:37 -05:00
def test_select_with_selected_value
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" selected= \" selected \" >abe</option> \n <option value= \" <mus> \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :selected = > 'abe' )
)
end
def test_select_with_index_option
@album = Album . new
@album . id = 1
2010-08-14 01:13:00 -04:00
expected = " <select id= \" album__genre \" name= \" album[][genre] \" ><option value= \" rap \" >rap</option> \n <option value= \" rock \" >rock</option> \n <option value= \" country \" >country</option></select> "
2008-03-19 22:15:29 -04:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
2010-08-14 01:13:00 -04:00
expected ,
2009-02-03 21:25:37 -05:00
select ( " album[] " , " genre " , %w[ rap rock country ] , { } , { :index = > nil } )
)
end
2005-11-23 16:59:20 -05:00
2012-02-20 18:19:28 -05:00
def test_select_escapes_options
assert_dom_equal (
'<select id="post_title" name="post[title]"><script>alert(1)</script></select>' ,
select ( 'post' , 'title' , '<script>alert(1)</script>' )
)
end
2009-02-03 21:25:37 -05:00
def test_select_with_selected_nil
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" ><mus></option> \n <option value= \" hest \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :selected = > nil )
)
end
2005-07-03 08:23:16 -04:00
2009-02-13 19:37:24 -05:00
def test_select_with_disabled_value
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" disabled= \" disabled \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :disabled = > 'hest' )
)
end
def test_select_with_disabled_array
@post = Post . new
@post . category = " <mus> "
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" abe \" disabled= \" disabled \" >abe</option> \n <option value= \" <mus> \" selected= \" selected \" ><mus></option> \n <option value= \" hest \" disabled= \" disabled \" >hest</option></select> " ,
select ( " post " , " category " , %w( abe <mus> hest ) , :disabled = > [ 'hest' , 'abe' ] )
)
end
2012-01-21 11:13:47 -05:00
def test_select_with_range
@post = Post . new
@post . category = 0
assert_dom_equal (
" <select id= \" post_category \" name= \" post[category] \" ><option value= \" 1 \" >1</option> \n <option value= \" 2 \" >2</option> \n <option value= \" 3 \" >3</option></select> " ,
select ( " post " , " category " , 1 .. 3 )
)
end
2009-02-03 21:25:37 -05:00
def test_collection_select
@post = Post . new
@post . author_name = " Babe "
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" ><option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
2009-02-13 19:14:48 -05:00
collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " )
2009-02-03 21:25:37 -05:00
)
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_collection_select_under_fields_for
@post = Post . new
@post . author_name = " Babe "
2008-06-08 23:05:39 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :post , @post do | f |
2009-02-13 19:14:48 -05:00
concat f . collection_select ( :author_name , dummy_posts , :author_name , :author_name )
2008-07-02 14:09:10 -04:00
end
2010-08-14 01:13:00 -04:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" ><option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
output_buffer
)
end
2005-11-13 06:13:11 -05:00
2009-02-03 21:25:37 -05:00
def test_collection_select_under_fields_for_with_index
@post = Post . new
@post . author_name = " Babe "
2008-07-13 19:55:57 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :post , @post , :index = > 815 do | f |
2009-02-13 19:14:48 -05:00
concat f . collection_select ( :author_name , dummy_posts , :author_name , :author_name )
2008-07-13 19:55:57 -04:00
end
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_815_author_name \" name= \" post[815][author_name] \" ><option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
output_buffer
)
end
2008-07-13 19:55:57 -04:00
2009-02-03 21:25:37 -05:00
def test_collection_select_under_fields_for_with_auto_index
@post = Post . new
@post . author_name = " Babe "
def @post . to_param ; 815 ; end
2008-07-13 19:55:57 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for " post[] " , @post do | f |
2009-02-13 19:14:48 -05:00
concat f . collection_select ( :author_name , dummy_posts , :author_name , :author_name )
2008-07-13 19:55:57 -04:00
end
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_815_author_name \" name= \" post[815][author_name] \" ><option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
output_buffer
)
end
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
def test_collection_select_with_blank_and_style
@post = Post . new
@post . author_name = " Babe "
2004-11-23 20:04:44 -05:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" style= \" width: 200px \" ><option value= \" \" ></option> \n <option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
2009-02-13 19:14:48 -05:00
collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " , { :include_blank = > true } , " style " = > " width: 200px " )
2009-02-03 21:25:37 -05:00
)
end
2007-05-18 01:29:22 -04:00
2009-02-03 21:25:37 -05:00
def test_collection_select_with_blank_as_string_and_style
@post = Post . new
@post . author_name = " Babe "
2007-05-18 01:29:22 -04:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" style= \" width: 200px \" ><option value= \" \" >No Selection</option> \n <option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> " ,
2009-02-13 19:14:48 -05:00
collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " , { :include_blank = > 'No Selection' } , " style " = > " width: 200px " )
2009-02-03 21:25:37 -05:00
)
end
2007-01-28 11:17:55 -05:00
2011-06-08 05:59:54 -04:00
def test_collection_select_with_multiple_option_appends_array_brackets_and_hidden_input
2009-02-03 21:25:37 -05:00
@post = Post . new
@post . author_name = " Babe "
2007-01-28 11:17:55 -05:00
2011-06-08 05:59:54 -04:00
expected = " <input type= \" hidden \" name= \" post[author_name][] \" value= \" \" /><select id= \" post_author_name \" name= \" post[author_name][] \" multiple= \" multiple \" ><option value= \" \" ></option> \n <option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" >Cabe</option></select> "
2007-01-28 11:17:55 -05:00
2009-02-03 21:25:37 -05:00
# Should suffix default name with [].
2009-02-13 19:14:48 -05:00
assert_dom_equal expected , collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " , { :include_blank = > true } , :multiple = > true )
2007-01-28 11:17:55 -05:00
2009-02-03 21:25:37 -05:00
# Shouldn't suffix custom name with [].
2009-02-13 19:14:48 -05:00
assert_dom_equal expected , collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " , { :include_blank = > true , :name = > 'post[author_name][]' } , :multiple = > true )
2009-02-03 21:25:37 -05:00
end
2008-09-12 17:56:56 -04:00
2009-02-03 21:25:37 -05:00
def test_collection_select_with_blank_and_selected
@post = Post . new
@post . author_name = " Babe "
2008-09-12 17:56:56 -04:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
%{ <select id="post_author_name" name="post[author_name]"><option value=""></option> \n <option value="<Abe>" selected="selected"><Abe></option> \n <option value="Babe">Babe</option> \n <option value="Cabe">Cabe</option></select> } ,
2009-02-13 19:14:48 -05:00
collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " , { :include_blank = > true , :selected = > " <Abe> " } )
2009-02-03 21:25:37 -05:00
)
end
2009-02-13 19:37:24 -05:00
def test_collection_select_with_disabled
@post = Post . new
@post . author_name = " Babe "
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" ><option value= \" <Abe> \" ><Abe></option> \n <option value= \" Babe \" selected= \" selected \" >Babe</option> \n <option value= \" Cabe \" disabled= \" disabled \" >Cabe</option></select> " ,
collection_select ( " post " , " author_name " , dummy_posts , " author_name " , " author_name " , :disabled = > 'Cabe' )
2012-02-01 14:43:44 -05:00
)
end
def test_collection_select_with_proc_for_value_method
@post = Post . new
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" ><option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option></select> " ,
collection_select ( " post " , " author_name " , dummy_posts , lambda { | p | p . author_name } , " title " )
)
end
def test_collection_select_with_proc_for_text_method
@post = Post . new
assert_dom_equal (
" <select id= \" post_author_name \" name= \" post[author_name] \" ><option value= \" <Abe> \" ><Abe> went home</option> \n <option value= \" Babe \" >Babe went home</option> \n <option value= \" Cabe \" >Cabe went home</option></select> " ,
collection_select ( " post " , " author_name " , dummy_posts , " author_name " , lambda { | p | p . title } )
)
2009-02-13 19:37:24 -05:00
end
2009-02-03 21:25:37 -05:00
def test_time_zone_select
@firm = Firm . new ( " D " )
html = time_zone_select ( " firm " , " time_zone " )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
end
def test_time_zone_select_under_fields_for
@firm = Firm . new ( " D " )
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :firm , @firm do | f |
2009-02-03 21:25:37 -05:00
concat f . time_zone_select ( :time_zone )
2008-07-13 19:55:57 -04:00
end
2010-08-14 01:13:00 -04:00
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
output_buffer
)
end
2008-07-13 19:55:57 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_under_fields_for_with_index
@firm = Firm . new ( " D " )
2008-07-13 19:55:57 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :firm , @firm , :index = > 305 do | f |
2009-02-03 21:25:37 -05:00
concat f . time_zone_select ( :time_zone )
2008-07-13 19:55:57 -04:00
end
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" firm_305_time_zone \" name= \" firm[305][time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
output_buffer
)
end
2008-07-13 19:55:57 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_under_fields_for_with_auto_index
@firm = Firm . new ( " D " )
def @firm . to_param ; 305 ; end
2008-07-13 19:55:57 -04:00
2010-03-10 02:41:39 -05:00
output_buffer = fields_for " firm[] " , @firm do | f |
2009-02-03 21:25:37 -05:00
concat f . time_zone_select ( :time_zone )
2008-07-13 19:55:57 -04:00
end
2009-02-03 21:25:37 -05:00
assert_dom_equal (
" <select id= \" firm_305_time_zone \" name= \" firm[305][time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
output_buffer
)
end
2008-07-13 19:55:57 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_blank
@firm = Firm . new ( " D " )
html = time_zone_select ( " firm " , " time_zone " , nil , :include_blank = > true )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" \" ></option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
end
2008-07-13 19:55:57 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_blank_as_string
@firm = Firm . new ( " D " )
html = time_zone_select ( " firm " , " time_zone " , nil , :include_blank = > 'No Zone' )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" \" >No Zone</option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_style
@firm = Firm . new ( " D " )
html = time_zone_select ( " firm " , " time_zone " , nil , { } ,
" style " = > " color: red " )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" style= \" color: red \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
assert_dom_equal html , time_zone_select ( " firm " , " time_zone " , nil , { } ,
:style = > " color: red " )
end
2005-02-23 07:54:58 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_blank_and_style
@firm = Firm . new ( " D " )
html = time_zone_select ( " firm " , " time_zone " , nil ,
{ :include_blank = > true } , " style " = > " color: red " )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" style= \" color: red \" > " +
" <option value= \" \" ></option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
assert_dom_equal html , time_zone_select ( " firm " , " time_zone " , nil ,
{ :include_blank = > true } , :style = > " color: red " )
end
2007-05-18 01:29:22 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_blank_as_string_and_style
@firm = Firm . new ( " D " )
html = time_zone_select ( " firm " , " time_zone " , nil ,
{ :include_blank = > 'No Zone' } , " style " = > " color: red " )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" style= \" color: red \" > " +
" <option value= \" \" >No Zone</option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
assert_dom_equal html , time_zone_select ( " firm " , " time_zone " , nil ,
{ :include_blank = > 'No Zone' } , :style = > " color: red " )
end
2007-12-21 17:18:07 -05:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_priority_zones
@firm = Firm . new ( " D " )
zones = [ ActiveSupport :: TimeZone . new ( " A " ) , ActiveSupport :: TimeZone . new ( " D " ) ]
html = time_zone_select ( " firm " , " time_zone " , zones )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> " +
" <option value= \" \" disabled= \" disabled \" >-------------</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
end
2008-06-28 22:27:32 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_priority_zones_as_regexp
@firm = Firm . new ( " D " )
2013-02-21 11:31:13 -05:00
2009-02-03 21:25:37 -05:00
@fake_timezones . each_with_index do | tz , i |
2013-03-27 17:03:49 -04:00
tz . stubs ( : =~ ) . returns ( i . zero? || i == 3 )
2009-02-03 21:25:37 -05:00
end
2013-03-27 17:03:49 -04:00
html = time_zone_select ( " firm " , " time_zone " , / A|D / )
2009-02-03 21:25:37 -05:00
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> " +
" <option value= \" \" disabled= \" disabled \" >-------------</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
end
2013-04-04 21:37:49 -04:00
2013-03-27 17:08:18 -04:00
def test_time_zone_select_with_priority_zones_as_regexp_using_grep_finds_no_zones
@firm = Firm . new ( " D " )
2013-04-04 21:37:49 -04:00
2013-03-27 17:08:18 -04:00
priority_zones = / A|D /
2013-04-02 15:06:16 -04:00
@fake_timezones . each do | tz |
2013-03-27 17:08:18 -04:00
priority_zones . stubs ( :=== ) . with ( tz ) . raises ( Exception )
end
2013-04-04 21:37:49 -04:00
2013-03-27 17:08:18 -04:00
html = time_zone_select ( " firm " , " time_zone " , priority_zones )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" \" disabled= \" disabled \" >-------------</option> \n " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
end
2008-06-28 22:27:32 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_default_time_zone_and_nil_value
@firm = Firm . new ( )
@firm . time_zone = nil
2013-04-04 21:37:49 -04:00
html = time_zone_select ( " firm " , " time_zone " , nil , :default = > 'B' )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
2007-12-21 17:18:07 -05:00
" <option value= \" A \" >A</option> \n " +
2009-02-03 21:25:37 -05:00
" <option value= \" B \" selected= \" selected \" >B</option> \n " +
2007-12-21 17:18:07 -05:00
" <option value= \" C \" >C</option> \n " +
2009-02-03 21:25:37 -05:00
" <option value= \" D \" >D</option> \n " +
2007-12-21 17:18:07 -05:00
" <option value= \" E \" >E</option> " +
" </select> " ,
html
2009-02-03 21:25:37 -05:00
end
2008-07-02 14:09:10 -04:00
2009-02-03 21:25:37 -05:00
def test_time_zone_select_with_default_time_zone_and_value
2013-04-04 21:37:49 -04:00
@firm = Firm . new ( 'D' )
html = time_zone_select ( " firm " , " time_zone " , nil , :default = > 'B' )
assert_dom_equal " <select id= \" firm_time_zone \" name= \" firm[time_zone] \" > " +
" <option value= \" A \" >A</option> \n " +
" <option value= \" B \" >B</option> \n " +
" <option value= \" C \" >C</option> \n " +
" <option value= \" D \" selected= \" selected \" >D</option> \n " +
" <option value= \" E \" >E</option> " +
" </select> " ,
html
2008-07-02 14:09:10 -04:00
end
2009-02-03 21:25:37 -05:00
2010-05-15 05:53:59 -04:00
def test_options_for_select_with_element_attributes
assert_dom_equal (
2012-09-03 05:42:24 -04:00
" <option value= \" <Denmark> \" class= \" bold \" ><Denmark></option> \n <option value= \" USA \" onclick= \" alert(& # 39;Hello World& # 39;); \" >USA</option> \n <option value= \" Sweden \" >Sweden</option> \n <option value= \" Germany \" >Germany</option> " ,
2010-05-15 05:53:59 -04:00
options_for_select ( [ [ " <Denmark> " , { :class = > 'bold' } ] , [ " USA " , { :onclick = > " alert('Hello World'); " } ] , [ " Sweden " ] , " Germany " ] )
)
end
2012-07-21 06:34:03 -04:00
def test_options_for_select_with_data_element
assert_dom_equal (
" <option value= \" <Denmark> \" data-test= \" bold \" ><Denmark></option> " ,
options_for_select ( [ [ " <Denmark> " , { :data = > { :test = > 'bold' } } ] ] )
)
end
2012-07-22 12:57:42 -04:00
def test_options_for_select_with_data_element_with_special_characters
assert_dom_equal (
" <option value= \" <Denmark> \" data-test= \" <bold> \" ><Denmark></option> " ,
options_for_select ( [ [ " <Denmark> " , { :data = > { :test = > '<bold>' } } ] ] )
)
end
2010-05-15 05:53:59 -04:00
def test_options_for_select_with_element_attributes_and_selection
assert_dom_equal (
" <option value= \" <Denmark> \" ><Denmark></option> \n <option value= \" USA \" class= \" bold \" selected= \" selected \" >USA</option> \n <option value= \" Sweden \" >Sweden</option> " ,
options_for_select ( [ " <Denmark> " , [ " USA " , { :class = > 'bold' } ] , " Sweden " ] , " USA " )
)
end
def test_options_for_select_with_element_attributes_and_selection_array
assert_dom_equal (
" <option value= \" <Denmark> \" ><Denmark></option> \n <option value= \" USA \" class= \" bold \" selected= \" selected \" >USA</option> \n <option value= \" Sweden \" selected= \" selected \" >Sweden</option> " ,
options_for_select ( [ " <Denmark> " , [ " USA " , { :class = > 'bold' } ] , " Sweden " ] , [ " USA " , " Sweden " ] )
)
end
2012-07-21 06:34:03 -04:00
def test_options_for_select_with_special_characters
assert_dom_equal (
" <option value= \" <Denmark> \" onclick= \" alert("<code>") \" ><Denmark></option> " ,
options_for_select ( [ [ " <Denmark> " , { :onclick = > %( alert ( "<code>" ) ) } ] ] )
)
end
2012-08-11 13:04:21 -04:00
def test_option_html_attributes_with_no_array_element
assert_equal ( { } , option_html_attributes ( 'foo' ) )
end
def test_option_html_attributes_without_hash
assert_equal ( { } , option_html_attributes ( [ 'foo' , 'bar' ] ) )
2010-05-15 05:53:59 -04:00
end
def test_option_html_attributes_with_single_element_hash
2012-02-20 14:55:46 -05:00
assert_equal (
{ :class = > 'fancy' } ,
2010-05-15 05:53:59 -04:00
option_html_attributes ( [ 'foo' , 'bar' , { :class = > 'fancy' } ] )
)
end
def test_option_html_attributes_with_multiple_element_hash
2012-02-20 14:55:46 -05:00
assert_equal (
{ :class = > 'fancy' , 'onclick' = > " alert('Hello World'); " } ,
2010-05-15 05:53:59 -04:00
option_html_attributes ( [ 'foo' , 'bar' , { :class = > 'fancy' , 'onclick' = > " alert('Hello World'); " } ] )
)
end
def test_option_html_attributes_with_multiple_hashes
2012-02-20 14:55:46 -05:00
assert_equal (
{ :class = > 'fancy' , 'onclick' = > " alert('Hello World'); " } ,
2010-05-15 05:53:59 -04:00
option_html_attributes ( [ 'foo' , 'bar' , { :class = > 'fancy' } , { 'onclick' = > " alert('Hello World'); " } ] )
)
end
2012-08-11 13:13:54 -04:00
def test_option_html_attributes_with_multiple_hashes_does_not_modify_them
options1 = { class : 'fancy' }
options2 = { onclick : " alert('Hello World'); " }
option_html_attributes ( [ 'foo' , 'bar' , options1 , options2 ] )
assert_equal ( { class : 'fancy' } , options1 )
assert_equal ( { onclick : " alert('Hello World'); " } , options2 )
end
2009-08-09 22:18:01 -04:00
def test_grouped_collection_select
@post = Post . new
@post . origin = 'dk'
assert_dom_equal (
%Q{ <select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option> \n <option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk" selected="selected">Denmark</option> \n <option value="ie">Ireland</option></optgroup></select> } ,
2010-06-17 03:17:31 -04:00
grouped_collection_select ( " post " , " origin " , dummy_continents , :countries , :continent_name , :country_id , :country_name )
2009-08-09 22:18:01 -04:00
)
end
2012-02-18 06:35:28 -05:00
def test_grouped_collection_select_with_selected
@post = Post . new
assert_dom_equal (
%Q{ <select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option> \n <option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk" selected="selected">Denmark</option> \n <option value="ie">Ireland</option></optgroup></select> } ,
grouped_collection_select ( " post " , " origin " , dummy_continents , :countries , :continent_name , :country_id , :country_name , :selected = > 'dk' )
)
end
def test_grouped_collection_select_with_disabled_value
@post = Post . new
assert_dom_equal (
%Q{ <select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option> \n <option value="so">Somalia</option></optgroup><optgroup label="Europe"><option disabled="disabled" value="dk">Denmark</option> \n <option value="ie">Ireland</option></optgroup></select> } ,
grouped_collection_select ( " post " , " origin " , dummy_continents , :countries , :continent_name , :country_id , :country_name , :disabled = > 'dk' )
)
end
2009-08-09 22:18:01 -04:00
def test_grouped_collection_select_under_fields_for
@post = Post . new
@post . origin = 'dk'
2010-03-10 02:41:39 -05:00
output_buffer = fields_for :post , @post do | f |
2010-06-17 03:17:31 -04:00
concat f . grouped_collection_select ( " origin " , dummy_continents , :countries , :continent_name , :country_id , :country_name )
2009-08-09 22:18:01 -04:00
end
assert_dom_equal (
%Q{ <select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option> \n <option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk" selected="selected">Denmark</option> \n <option value="ie">Ireland</option></optgroup></select> } ,
output_buffer
)
end
2009-02-13 19:14:48 -05:00
private
2012-02-01 14:43:44 -05:00
def dummy_posts
[ Post . new ( " <Abe> went home " , " <Abe> " , " To a little house " , " shh! " ) ,
Post . new ( " Babe went home " , " Babe " , " To a little house " , " shh! " ) ,
Post . new ( " Cabe went home " , " Cabe " , " To a little house " , " shh! " ) ]
end
2010-06-17 03:17:31 -04:00
2012-02-01 14:43:44 -05:00
def dummy_continents
[ Continent . new ( " <Africa> " , [ Country . new ( " <sa> " , " <South Africa> " ) , Country . new ( " so " , " Somalia " ) ] ) ,
Continent . new ( " Europe " , [ Country . new ( " dk " , " Denmark " ) , Country . new ( " ie " , " Ireland " ) ] ) ]
end
2008-11-23 19:12:41 -05:00
end