#= require ultimate/underscore/underscore
#= require ultimate/underscore/underscore.string
#= require ultimate/underscore/underscore.outcasts
#= require vendors/backbone
#= require ultimate/helpers/form_options
module "Ultimate.Helpers.FormOptions"
_.extend @, Ultimate.Helpers.FormOptions
test "options_for_select", ->
options = ''
equal options_for_select(options), options
equal options_for_select([ "", "USA", "Sweden" ]),
"\n\n"
equal options_for_select([ "Denmark", "", "Sweden" ], ""),
"\n\n"
equal options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]),
"\n\n"
equal options_for_select([ "Denmark", "", "Sweden" ], disabled: ""),
"\n\n"
equal options_for_select([ "Denmark", "", "Sweden" ], disabled: ["", "Sweden"]),
"\n\n"
equal options_for_select([ "Denmark", "", "Sweden" ], selected: "Denmark", disabled: ""),
"\n\n"
equal options_for_select([ true, false ], selected: false, disabled: null),
"\n"
equal options_for_select([1..3]),
"\n\n"
equal options_for_select([ "ruby", "rubyonrails" ], "rubyonrails"),
"\n"
equal options_for_select([ "ruby", "rubyonrails" ], "ruby"),
"\n"
equal options_for_select([ "ruby", "rubyonrails", null ], "ruby"),
"\n\n"
equal options_for_select("$": "Dollar", "": ""),
"\n"
equal options_for_select({ "$": "Dollar", "": "" }, "Dollar"),
"\n"
equal options_for_select({ "$": "Dollar", "": "" }, [ "Dollar", "" ]),
"\n"
equal options_for_select([ [ "", { class: 'bold' } ], [ "USA", { onclick: "alert('Hello World');" } ], [ "Sweden" ], "Germany" ]),
"\n\n\n"
equal options_for_select([ [ "", { data: { test: 'bold' } } ] ]),
""
equal options_for_select([ [ "", { data: { test: '' } } ] ]),
""
equal options_for_select([ "", [ "USA", { class: 'bold' } ], "Sweden" ], "USA"),
"\n\n"
equal options_for_select([ "", [ "USA", { class: 'bold' } ], "Sweden" ], [ "USA", "Sweden" ]),
"\n\n"
equal options_for_select([ [ "", { onclick: 'alert("")' } ] ]),
""
class Struct
constructor: ->
for key, index in @constructor.keys
@[key] = arguments[index] ? null
class Post extends Struct
@keys = ['title', 'author_name', 'body', 'secret', 'written_on', 'category', 'origin', 'allow_comments']
dummy_posts =
[ new Post(" went home", "", "To a little house", "shh!"),
new Post("Babe went home", "Babe", "To a little house", "shh!"),
new Post("Cabe went home", "Cabe", (-> "To a little house"), (-> "shh!")) ]
test "options_from_collection_for_select", ->
equal options_from_collection_for_select(dummy_posts, "author_name", "title"),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", "Babe"),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", [ "Babe", "Cabe" ]),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", (p) -> p.author_name is 'Babe'),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: "Babe"),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: [ "Babe", "Cabe" ]),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", selected: "Cabe", disabled: "Babe"),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: (p) -> p.author_name in ['Babe', 'Cabe']),
"\n\n"
equal options_from_collection_for_select(dummy_posts, ((p) -> p.author_name), "title"),
"\n\n"
equal options_from_collection_for_select(dummy_posts, "author_name", (p) -> p.title),
"\n\n"
_objectsArrayToHashesArray = (objectsArray) ->
_.map objectsArray, (element) ->
hash = {}
for key in element.constructor.keys
hash[key] = element[key]
hash
test "options_from_collection_for_select with array of hashes", ->
equal options_from_collection_for_select(_objectsArrayToHashesArray(dummy_posts), "author_name", "title", "Babe"),
"\n\n"
class BBPost extends Backbone.Model
class BBPosts extends Backbone.Collection
model: BBPost
test "options_from_collection_for_select with Backbone.Collection", ->
bbPosts = new BBPosts(_objectsArrayToHashesArray(dummy_posts))
equal options_from_collection_for_select(bbPosts, "author_name", "title", "Babe"),
"\n\n"
equal options_from_collection_for_select(bbPosts, "author_name", "title", disabled: (p) -> p.get('author_name') in ['Babe', 'Cabe']),
"\n\n"
equal options_from_collection_for_select(bbPosts, ((p) -> p.get('author_name')), "title"),
"\n\n"
equal options_from_collection_for_select(bbPosts, "author_name", (p) -> p.get('title')),
"\n\n"
class Continent extends Struct
@keys = ['continent_name', 'countries']
class Country extends Struct
@keys = ['country_id', 'country_name']
dummy_continents =
[ new Continent("", [new Country("", ""), new Country("so", "Somalia")]),
new Continent("Europe", [new Country("dk", "Denmark"), new Country("ie", "Ireland")]) ]
test "option_groups_from_collection_for_select", ->
equal option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk"),
""
class BBContinent extends Backbone.Model
class BBContinents extends Backbone.Collection
model: BBContinent
test "option_groups_from_collection_for_select with Backbone.Collection", ->
BBContinents = new BBContinents(_objectsArrayToHashesArray(dummy_continents))
equal option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk"),
""
test "grouped_options_for_select", ->
equal grouped_options_for_select([
["North America",
[['United States','US'],"Canada"]],
["Europe",
[["Great Britain","GB"], "Germany"]]
]),
""
equal grouped_options_for_select([['US',"Canada"] , ["GB", "Germany"]], null, divider: "----------"),
""
equal grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", prompt: "Choose a product..."),
""
equal grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", prompt: true),
""
equal grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], null, prompt: ''),
""
equal grouped_options_for_select({'North America': ['United States','Canada'], 'Europe': ['Denmark','Germany']}),
""