Skip to content

Commit

Permalink
Merge pull request formtastic#723 from ContinuityControl/speedup-larg…
Browse files Browse the repository at this point in the history
…e-checkbox-set

memoize the selected values array
  • Loading branch information
justinfrench committed Oct 24, 2011
2 parents fc95a63 + dbc7452 commit 2befcea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/formtastic/inputs/check_boxes_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,7 @@ def disabled?(value)
end

def selected_values
if object.respond_to?(method)
selected_items = [object.send(method)].compact.flatten

[*selected_items.map { |o| send_or_call_or_object(value_method, o) }].compact
else
[]
end
@selected_values ||= make_selected_values
end

def disabled_values
Expand All @@ -175,6 +169,17 @@ def input_name
"#{object_name}[#{association_primary_key || method}][]"
end

protected

def make_selected_values
if object.respond_to?(method)
selected_items = [object.send(method)].compact.flatten

[*selected_items.map { |o| send_or_call_or_object(value_method, o) }].compact
else
[]
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/inputs/check_boxes_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
item.should_not_receive(:id)
item.stub!(:custom_value).and_return('custom_value')
item.should_receive(:custom_value).exactly(3).times
@new_post.author.should_receive(:custom_value).exactly(3).times
@new_post.author.should_receive(:custom_value).exactly(1).times
concat(semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :check_boxes, :member_value => :custom_value, :collection => [item, item, item]))
end)
Expand Down

0 comments on commit 2befcea

Please sign in to comment.