Skip to content

Commit

Permalink
Collections of strings in CheckBoxesInput should now be checked if th…
Browse files Browse the repository at this point in the history
…ey match the model.

A String responds to methods such as :first and :last, so instead of applying send_or_call to the String ("hello".last => "o"), simply return the String itself.

Fixes formtastic#630, Ref formtastic#619
  • Loading branch information
justinfrench committed Jul 26, 2011
1 parent a0d6092 commit d2c4ff7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/formtastic/inputs/base/collections.rb
Expand Up @@ -87,6 +87,13 @@ def send_or_call(duck, object)
object.send(duck)
end
end

# Avoids an issue where `send_or_call` can be a String and duck can be something simple like
# `:first`, which obviously String responds to.
def send_or_call_or_object(duck, object)
return object if object.is_a?(String) # TODO what about other classes etc?
send_or_call(duck, object)
end

end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/check_boxes_input.rb
Expand Up @@ -154,7 +154,7 @@ def disabled?(value)
def selected_values
if object.respond_to?(method)
selected_items = [object.send(method)].compact.flatten
[*selected_items.map { |o| send_or_call(value_method, o) }].compact
[*selected_items.map { |o| send_or_call_or_object(value_method, o) }].compact
else
[]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/check_boxes_input_spec.rb
Expand Up @@ -361,7 +361,7 @@
before do
@output_buffer = ''
mock_everything
@fred.stub(:genres) { ['ficton', 'biography'] }
@fred.stub(:genres) { ['fiction', 'biography'] }

concat(semantic_form_for(@fred) do |builder|
concat(builder.input(:genres, :as => :check_boxes, :collection => [['Fiction', 'fiction'], ['Non-fiction', 'non_fiction'], ['Biography', 'biography']]))
Expand Down

0 comments on commit d2c4ff7

Please sign in to comment.