Add test to support grouped collection with association

This commit is contained in:
Carlos Antonio da Silva 2012-01-24 11:48:38 -02:00
parent 03103ac344
commit 02ffe40bcc
3 changed files with 38 additions and 16 deletions

View File

@ -3,20 +3,20 @@ module SimpleForm
class GroupedCollectionInput < CollectionInput
def input
label_method, value_method = detect_collection_methods
@builder.grouped_collection_select(attribute_name, group_collection,
@builder.grouped_collection_select(attribute_name, grouped_collection,
group_method, group_label_method, value_method, label_method,
input_options, input_html_options)
end
private
def group_collection
@group_collection ||= options.delete(:collection)
def grouped_collection
@grouped_collection ||= options.delete(:collection)
end
# Sample collection
def collection
@collection ||= group_collection.first.try(:send, group_method)
@collection ||= grouped_collection.first.try(:send, group_method)
end
def group_method
@ -27,7 +27,7 @@ module SimpleForm
label = options.delete(:group_label_method)
unless label
common_method_for = detect_common_display_methods(detect_collection_classes(group_collection))
common_method_for = detect_common_display_methods(detect_collection_classes(grouped_collection))
label = common_method_for[:label]
end

View File

@ -3,11 +3,11 @@ require 'test_helper'
class GroupedCollectionInputTest < ActionView::TestCase
test 'grouped collection accepts array collection form' do
with_input_for @user, :name, :grouped_select,
with_input_for @user, :tag_ids, :grouped_select,
:collection => [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]],
:group_method => :last
assert_select 'select.grouped_select#user_name' do
assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Authors]' do
assert_select 'option', 'Jose'
assert_select 'option', 'Carlos'
@ -21,11 +21,11 @@ class GroupedCollectionInputTest < ActionView::TestCase
end
test 'grouped collection accepts hash collection form' do
with_input_for @user, :name, :grouped_select,
with_input_for @user, :tag_ids, :grouped_select,
:collection => { 'Authors' => ['Jose', 'Carlos'], 'General' => ['Bob', 'John'] },
:group_method => :last
assert_select 'select.grouped_select#user_name' do
assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Authors]' do
assert_select 'option', 'Jose'
assert_select 'option', 'Carlos'
@ -39,12 +39,12 @@ class GroupedCollectionInputTest < ActionView::TestCase
end
test 'grouped collection accepts group_label_method option' do
with_input_for @user, :name, :grouped_select,
with_input_for @user, :tag_ids, :grouped_select,
:collection => { ['Jose', 'Carlos'] => 'Authors' },
:group_method => :first,
:group_label_method => :last
assert_select 'select.grouped_select#user_name' do
assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Authors]' do
assert_select 'option', 'Jose'
assert_select 'option', 'Carlos'
@ -53,17 +53,39 @@ class GroupedCollectionInputTest < ActionView::TestCase
end
test 'grouped collection accepts label and value methods options' do
with_input_for @user, :name, :grouped_select,
with_input_for @user, :tag_ids, :grouped_select,
:collection => { 'Authors' => ['Jose', 'Carlos'] },
:group_method => :last,
:label_method => :upcase,
:value_method => :downcase
assert_select 'select.grouped_select#user_name' do
assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Authors]' do
assert_select 'option[value=jose]', 'JOSE'
assert_select 'option[value=carlos]', 'CARLOS'
end
end
end
test 'grouped collection with associations' do
tag_groups = [
TagGroup.new(1, "Group of Tags", [Tag.new(1, "Tag 1"), Tag.new(2, "Tag 2")]),
TagGroup.new(2, "Other group", [Tag.new(3, "Tag 3"), Tag.new(4,"Tag 4")])
]
with_input_for @user, :tag_ids, :grouped_select,
:collection => tag_groups, :group_method => :tags
assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Group of Tags]' do
assert_select 'option[value=1]', 'Tag 1'
assert_select 'option[value=2]', 'Tag 2'
end
assert_select 'optgroup[label=Other group]' do
assert_select 'option[value=3]', 'Tag 3'
assert_select 'option[value=4]', 'Tag 4'
end
end
end
end

View File

@ -26,14 +26,14 @@ class Company < Struct.new(:id, :name)
end
class Tag < Company
extend ActiveModel::Naming
include ActiveModel::Conversion
def self.all(options={})
(1..3).map{|i| Tag.new(i, "Tag #{i}")}
end
end
class TagGroup < Struct.new(:id, :name, :tags)
end
class User
extend ActiveModel::Naming
include ActiveModel::Conversion