Browse files

Collections of strings in CheckBoxesInput should now be checked if th…

…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 #630, Ref #619
  • Loading branch information...
1 parent a0d6092 commit d2c4ff73d9079f07f53efd193316d283bc332b7d @justinfrench committed Jul 26, 2011
View
7 lib/formtastic/inputs/base/collections.rb
@@ -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
View
2 lib/formtastic/inputs/check_boxes_input.rb
@@ -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
View
2 spec/inputs/check_boxes_input_spec.rb
@@ -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']]))

0 comments on commit d2c4ff7

Please sign in to comment.