Skip to content

Commit

Permalink
cache checkboxes present?
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Dec 28, 2013
1 parent 5bb4cd8 commit d9a0c25
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/formbuilder/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,28 @@ def save_response(raw_value, response_field, response_field_params = {})
when "checkboxes"
# transform checkboxes into {label => on/off} pairs
values = {}
value_present = false

(response_field[:field_options]["options"] || []).each_with_index do |option, index|
label = response_field.field_options["options"][index]["label"]
values[option["label"]] = raw_value && raw_value[index.to_s] == "on"

if raw_value && raw_value[index.to_s] == "on"
value_present = true
values[option["label"]] = true
else
values[option["label"]] = false
end
end

if raw_value && raw_value['other_checkbox'] == 'on'
responses["#{response_field.id}_other"] = true
values['Other'] = raw_value['other']
value_present = true
else
values.delete('Other') # @todo this might cause unexpected behavior to the user. we should hide/show the other field in the frontend, too
values.delete('Other')
end

# Save 'other' value
responses["#{response_field.id}_other"] = raw_value && raw_value['other_checkbox'] == 'on' ?
true :
nil
responses["#{response_field.id}_present"] = value_present

values

Expand Down
11 changes: 11 additions & 0 deletions spec/lib/formbuilder/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def file_value
e2.responses["#{first_response_field.id}_sortable_values_Choice #1"].should == false
end

it 'should cache checkboxes present?' do
first_response_field.update_attributes(type: 'Formbuilder::ResponseFieldCheckboxes',
field_options: { 'options' => [{'checked' => 'false', 'label' => 'Choice #1'}] })

e1 = create_entry({ '0' => 'on' })
e2 = create_entry({})

e1.responses["#{first_response_field.id}_present"].should == true
e2.responses["#{first_response_field.id}_present"].should == false
end

it 'should sort prices properly' do
first_response_field.update_attributes(type: 'Formbuilder::ResponseFieldPrice')
e1 = create_entry({ 'dollars' => '05', 'cents' => '02' })
Expand Down

0 comments on commit d9a0c25

Please sign in to comment.