Skip to content

Commit

Permalink
add Proc support to GroupedCollectionInput
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed Jan 24, 2012
1 parent 69b08c5 commit 37e9091
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/simple_form/inputs/grouped_collection_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def input
private

def grouped_collection
@grouped_collection ||= options.delete(:collection)
@grouped_collection ||= begin
grouped_collection = options.delete(:collection)
grouped_collection.respond_to?(:call) ? grouped_collection.call : grouped_collection.to_a
end
end

# Sample collection
Expand Down
18 changes: 18 additions & 0 deletions test/inputs/grouped_collection_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class GroupedCollectionInputTest < ActionView::TestCase
end
end

test 'grouped collection accepts proc as collection' do
with_input_for @user, :tag_ids, :grouped_select,
:collection => Proc.new { [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]] },
:group_method => :last

assert_select 'select.grouped_select#user_tag_ids' do
assert_select 'optgroup[label=Authors]' do
assert_select 'option', 'Jose'
assert_select 'option', 'Carlos'
end

assert_select 'optgroup[label=General]' do
assert_select 'option', 'Bob'
assert_select 'option', 'John'
end
end
end

test 'grouped collection accepts hash collection form' do
with_input_for @user, :tag_ids, :grouped_select,
:collection => { 'Authors' => ['Jose', 'Carlos'], 'General' => ['Bob', 'John'] },
Expand Down

0 comments on commit 37e9091

Please sign in to comment.