Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the value of keyed section #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/formotion/form/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ def open
row.value = saved_row_value
end
end
if section.key
section_value = saved_render[section.key]
selected_row = section.rows.find {|row| row.key.to_s == section_value}
selected_row.value = true if selected_row
end
end
}
rendered_data = load_state
Expand Down
39 changes: 39 additions & 0 deletions spec/form/persist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,43 @@
f.reset
r.value.should == "Nici"
end

it "can restore the value of keyed section" do
key = "test_#{rand(255)}"
App::Persistence["FORMOTION_#{key}"] = nil
App::Persistence["FORMOTION_#{key}_ORIGINAL"] = nil
hash = {
persist_as: key,
sections: [
key: :gender,
select_one: true,
rows: [ {
title: 'Male',
key: :male,
type: :check
}, {
title: 'Female',
key: :female,
type: :check
}, {
title: 'Unsaid',
key: :unsaid,
type: :check
}
]
]
}
# select 'Female'
f = Formotion::Form.persist(hash)
f.sections[0].rows[1].value = true

saved = f.send(:load_state)
# the saved value should be 'Female'
saved[:gender].should == 'female'

# Create another form with saved data
another = Formotion::Form.persist(hash)
# 'Female' should be selected
another.sections[0].rows[1].value.should == true
end
end