Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Proc support to GroupedCollectionInput

  • Loading branch information...
commit 37e90913fbef8d711922c092e981b5dfdae08f6e 1 parent 69b08c5
@nashby authored
View
5 lib/simple_form/inputs/grouped_collection_input.rb
@@ -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
View
18 test/inputs/grouped_collection_input_test.rb
@@ -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'] },
Please sign in to comment.
Something went wrong with that request. Please try again.