Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: clayallsopp/formotion
...
head fork: clayallsopp/formotion
  • 4 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
6 CHANGELOG.md
View
@@ -1,3 +1,9 @@
+## 1.1.5 - ??
+
+### Features
+
+- Added `form.values = { key: "value" }`, which will set all of the form's `value`s for each specified `key` en-masse.
+
## 1.1.4 - October 27, 2012
### Features
28 lib/formotion/form/form.rb
View
@@ -194,6 +194,34 @@ def sub_render
kv
end
+ def values=(data)
+ self.sections.each {|section|
+ if section.select_one?
+ # see if one of the select one value is used
+ unless (section.rows.map{ |r| r.key } & data.keys).empty?
+ section.rows.each { |row|
+ row.value = data.has_key?(row.key) ? true : nil
+ }
+ end
+ else
+ section.rows.each {|row|
+ next if row.button?
+ if row.template_parent
+ # If this row is part of a template
+ # use the parent's key
+ row.value = data[row.template_parent_key] if data.has_key?(row.template_parent_key)
+ elsif row.subform
+ row.subform.to_form.values = data
+ else
+ row.value = data[row.key] if data.has_key?(row.key)
+ end
+ }
+ end
+ }
+ end
+
+ alias_method :fill_out, :values=
+
#########################
# Persisting Forms
2  lib/formotion/version.rb
View
@@ -1,3 +1,3 @@
module Formotion
- VERSION = "1.1.4"
+ VERSION = "1.1.5"
end
93 spec/form_spec.rb
View
@@ -68,6 +68,75 @@
@form.render[:email].should == 'something@email.com'
end
+ it "fills out form values correctly" do
+ @form = Formotion::Form.new(sections: [{
+ rows: [{
+ key: :email,
+ type: :email,
+ editable: true,
+ title: 'Email'
+ }]}])
+
+ @form.values = { :email => 'something@email.com' }
+
+ row = @form.sections[0].rows[0]
+ row.value.should == 'something@email.com'
+ end
+
+ it "fills out form values correctly with a select one" do
+ @form = Formotion::Form.new(sections: [{
+ select_one: true,
+ rows: [{
+ title: "A",
+ key: :a,
+ type: :check,
+ value: true
+ }, {
+ title: "B",
+ key: :b,
+ type: :check,
+ }, {
+ title: "C",
+ key: :c,
+ type: :check,
+ }]
+ }])
+
+ @form.values = { :b => true }
+
+ row = @form.sections[0].rows[0]
+ row.value.should == nil
+ row = @form.sections[0].rows[1]
+ row.value.should == true
+ row = @form.sections[0].rows[2]
+ row.value.should == nil
+ end
+
+ it "fills out form values correctly with a template" do
+ @form = Formotion::Form.new(sections: [
+ {
+ title: 'Your nicknames',
+ rows: [{
+ title: "Add nickname",
+ key: :nicknames,
+ type: :template,
+ template: {
+ title: 'Nickname',
+ type: :string,
+ placeholder: 'Enter here',
+ indented: true,
+ deletable: true
+ }
+ }]
+ }])
+
+
+ @form.values = { :nicknames => ["Nici", "Sam"] }
+
+ row = @form.sections[0].rows[0]
+ row.value.should == ["Nici", "Sam"]
+ end
+
it "render with subforms works correctly" do
@form = Formotion::Form.new(sections: [{
rows: [{
@@ -92,6 +161,30 @@
@form.render[:subform][:email].should == 'something@email.com'
end
+ it "fills out subform values correctly" do
+ @form = Formotion::Form.new(sections: [{
+ rows: [{
+ type: :subform,
+ key: :subform,
+ subform: {
+ sections: [{
+ rows: [{
+ key: :email,
+ type: :email,
+ editable: true,
+ title: 'Email'
+ }]
+ }]
+ }
+ }]}])
+
+ @form.values = { :email => 'something@email.com' }
+
+ subform = @form.sections[0].rows[0].subform.to_form
+ row = subform.sections[0].rows[0]
+ row.value.should == 'something@email.com'
+ end
+
it "hashifying should be same as input" do
h = {
sections: [{

No commit comments for this range

Something went wrong with that request. Please try again.