Skip to content

Commit

Permalink
Ensure :value_method gets used for collections of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonuschat committed Jul 13, 2010
1 parent 1420c6b commit 12029dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ def find_collection_for_column(column, options) #:nodoc:
# Return if we have an Array of strings, fixnums or arrays
return collection if (collection.instance_of?(Array) || collection.instance_of?(Range)) &&
[Array, Fixnum, String, Symbol].include?(collection.first.class) &&
!options.include?(:label_method)
!(options.include?(:label_method) || options.include?(:value_method))

label, value = detect_label_and_value_method!(collection, options)
collection.map { |o| [send_or_call(label, o), send_or_call(value, o)] }
Expand Down
13 changes: 11 additions & 2 deletions spec/label_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@
end
end

describe 'when label method is given' do
it 'should ignore the shortcut for simple collections if a label_method is supplied' do
describe 'when a collection is given' do
it 'should use a supplied label_method for simple collections' do
semantic_form_for(:project, :url => 'http://test.host') do |builder|
concat(builder.input(:author_id, :as => :check_boxes, :collection => [:a, :b, :c], :value_method => :to_s, :label_method => proc {|f| ('Label_%s' % [f])}))
end
output_buffer.should have_tag('form li fieldset ol li label', :with => /Label_[abc]/, :count => 3)
end

it 'should use a supplied value_method for simple collections' do
semantic_form_for(:project, :url => 'http://test.host') do |builder|
concat(builder.input(:author_id, :as => :check_boxes, :collection => [:a, :b, :c], :value_method => proc {|f| ('Value_%s' % [f.to_s])}))
end
output_buffer.should have_tag('form li fieldset ol li label input[value="Value_a"]')
output_buffer.should have_tag('form li fieldset ol li label input[value="Value_b"]')
output_buffer.should have_tag('form li fieldset ol li label input[value="Value_c"]')
end
end

describe 'when label is given' do
Expand Down

0 comments on commit 12029dc

Please sign in to comment.