form_objects 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +524 -0
- data/Rakefile +6 -0
- data/form_objects.gemspec +27 -0
- data/lib/form_objects/associated_validator.rb +9 -0
- data/lib/form_objects/base.rb +33 -0
- data/lib/form_objects/naming.rb +13 -0
- data/lib/form_objects/nesting.rb +13 -0
- data/lib/form_objects/params_converter/collection_converter.rb +51 -0
- data/lib/form_objects/params_converter/date_converter.rb +41 -0
- data/lib/form_objects/params_converter.rb +18 -0
- data/lib/form_objects/serializer.rb +18 -0
- data/lib/form_objects/version.rb +3 -0
- data/lib/form_objects.rb +11 -0
- data/spec/base_spec.rb +97 -0
- data/spec/naming_spec.rb +14 -0
- data/spec/nesting_spec.rb +88 -0
- data/spec/params_converter_spec.rb +91 -0
- data/spec/serializer_spec.rb +75 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/examples.rb +44 -0
- metadata +148 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormObjects::ParamsConverter do
|
4
|
+
let(:event_attributes) { { "0" => { "name" => "Name 0" }, "1" => { "name" => "Name 1" } } }
|
5
|
+
let(:params) { { "events_attributes" => event_attributes } }
|
6
|
+
let(:converted_attributes) { [{"name" => "Name 0"}, {"name" => "Name 1"}] }
|
7
|
+
|
8
|
+
subject { described_class.new(params) }
|
9
|
+
|
10
|
+
describe "#params" do
|
11
|
+
describe "date parameters" do
|
12
|
+
describe "only three params" do
|
13
|
+
let(:params) { { "event" => { "date(1i)" => "2014", "date(2i)" => "12", "date(3i)" => "16" } } }
|
14
|
+
|
15
|
+
it "returns date as string" do
|
16
|
+
subject.params["event"]["date"].should == "2014.12.16 00:00:00"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "only four params" do
|
21
|
+
let(:params) { { "event" => { "date(1i)" => "2014", "date(2i)" => "12", "date(3i)" => "16", "date(4i)" => "12" } } }
|
22
|
+
|
23
|
+
it "returns date as string" do
|
24
|
+
subject.params["event"]["date"].should == "2014.12.16 12:00:00"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "only five params" do
|
29
|
+
let(:params) { { "event" => { "date(1i)" => "2014", "date(2i)" => "12", "date(3i)" => "16", "date(4i)" => "12", "date(5i)" => "30" } } }
|
30
|
+
|
31
|
+
it "returns date as string" do
|
32
|
+
subject.params["event"]["date"].should == "2014.12.16 12:30:00"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "all parameters" do
|
37
|
+
let(:params) { { "event" => { "date(1i)" => "2014", "date(2i)" => "12", "date(3i)" => "16", "date(4i)" => "12", "date(5i)" => "30", "date(6i)" => "45" } } }
|
38
|
+
|
39
|
+
it "returns date as string" do
|
40
|
+
subject.params["event"]["date"].should == "2014.12.16 12:30:45"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "nested parameters" do
|
46
|
+
it "returns events_attributes converted to array" do
|
47
|
+
subject.params["events_attributes"].should == converted_attributes
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns HashWithIndifferentAccess" do
|
51
|
+
subject.params.should be_kind_of(HashWithIndifferentAccess)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "does not modify original params" do
|
55
|
+
subject.params.should_not == params
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "events_attributes in nested" do
|
59
|
+
let(:params) { { "event" => { "events_attributes" => event_attributes } } }
|
60
|
+
|
61
|
+
it "returns events_attributes converted to array" do
|
62
|
+
subject.params["event"]["events_attributes"].should == converted_attributes
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "events_attributes should keep sequence" do
|
67
|
+
let(:event_attributes) { { "1" => { "name" => "Name 1" }, "0" => { "name" => "Name 0" } } }
|
68
|
+
|
69
|
+
it "returns events_attributes converted to array" do
|
70
|
+
subject.params["events_attributes"].should == [{"name" => "Name 0"}, {"name" => "Name 1"}]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "event_attributes is not Hash which pretends Array" do
|
75
|
+
let(:event_attributes) { { "first_attribute" => { "name" => "Name 0" } } }
|
76
|
+
|
77
|
+
it "returns non-converted events_attributes" do
|
78
|
+
subject.params["events_attributes"].should == event_attributes
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "event_attributes is Hash which almost pretends Array (wrong attributes sequence)" do
|
83
|
+
let(:event_attributes) { { "0" => { "name" => "Name 0" }, "2" => {"name" => "Name 2"} } }
|
84
|
+
|
85
|
+
it "returns non-converted events_attributes" do
|
86
|
+
subject.params["events_attributes"].should == event_attributes
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormObjects::Serializer do
|
4
|
+
before(:each) do
|
5
|
+
Object.send(:remove_const, 'MessageForm')
|
6
|
+
load 'support/examples.rb'
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "UserForm" do
|
10
|
+
let(:attributes) do
|
11
|
+
{
|
12
|
+
"first_name" => "FirstName",
|
13
|
+
"last_name" => "LastName",
|
14
|
+
"terms" => "1",
|
15
|
+
"addresses_attributes" => [
|
16
|
+
{"address" => "Street1"},
|
17
|
+
{"address" => "Street2"}
|
18
|
+
]
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
subject { UserForm.new(attributes).serialized_attributes }
|
23
|
+
|
24
|
+
it "includes first_name" do
|
25
|
+
subject[:first_name].should === "FirstName"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "includes last_name" do
|
29
|
+
subject[:last_name].should == "LastName"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "includes terms" do
|
33
|
+
subject[:terms].should be_truthy
|
34
|
+
end
|
35
|
+
|
36
|
+
it "includes addresses" do
|
37
|
+
subject[:addresses].should be_kind_of(Array)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "includes converted LocationForm inside Array" do
|
41
|
+
subject[:addresses].first.should be_kind_of(Hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "includes address" do
|
45
|
+
subject[:addresses].first[:address].should == "Street1"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#serialized_attributes' do
|
50
|
+
subject { MessageForm.new }
|
51
|
+
|
52
|
+
it 'returns hash' do
|
53
|
+
subject.serialized_attributes.should be_kind_of Hash
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'calls #serialized_attributes' do
|
57
|
+
expect(subject).to receive(:attributes)
|
58
|
+
subject.serialized_attributes
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'when object in hash respond to #serialized_attributes' do
|
62
|
+
let(:nested_form) { double(described_class) }
|
63
|
+
before do
|
64
|
+
subject.stub(:attributes).and_return({value: nested_form})
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'calls recursively #clean_attributes' do
|
68
|
+
expect(nested_form).to receive(:serialized_attributes)
|
69
|
+
subject.serialized_attributes
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
class AddressForm < FormObjects::Base
|
2
|
+
attribute :street, String
|
3
|
+
attribute :city, String
|
4
|
+
|
5
|
+
validates :street, presence: true
|
6
|
+
end
|
7
|
+
|
8
|
+
class PersonalInfoForm < FormObjects::Base
|
9
|
+
attribute :first_name, String
|
10
|
+
attribute :last_name, String
|
11
|
+
|
12
|
+
validates :first_name, presence: true
|
13
|
+
end
|
14
|
+
|
15
|
+
class UserForm < FormObjects::Base
|
16
|
+
attribute :email, String
|
17
|
+
attribute :author, String
|
18
|
+
end
|
19
|
+
|
20
|
+
class MessageForm < FormObjects::Base
|
21
|
+
include FormObjects::Naming
|
22
|
+
|
23
|
+
field :body, String
|
24
|
+
field :author, String
|
25
|
+
end
|
26
|
+
|
27
|
+
class LocationForm < FormObjects::Base
|
28
|
+
field :address, String
|
29
|
+
|
30
|
+
validates :address, presence: true
|
31
|
+
end
|
32
|
+
|
33
|
+
class UserForm < FormObjects::Base
|
34
|
+
include FormObjects::Naming
|
35
|
+
|
36
|
+
field :first_name, String
|
37
|
+
field :last_name, String
|
38
|
+
field :terms, Boolean
|
39
|
+
|
40
|
+
nested_form :addresses, Array[LocationForm], default: proc { Array.new(2, LocationForm.new) }
|
41
|
+
|
42
|
+
validates :first_name, presence: true
|
43
|
+
validates :terms, acceptance: { accept: true }
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: form_objects
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Nielacny
|
8
|
+
- Przemek Lusar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-12-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: virtus
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: activemodel
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.2'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.2'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: activesupport
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '3.2'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '3.2'
|
84
|
+
description: Micro library for creating and managing complex forms
|
85
|
+
email:
|
86
|
+
- piotr.nielacny@gmail.com
|
87
|
+
- przemyslaw.lusar@gmail.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- form_objects.gemspec
|
100
|
+
- lib/form_objects.rb
|
101
|
+
- lib/form_objects/associated_validator.rb
|
102
|
+
- lib/form_objects/base.rb
|
103
|
+
- lib/form_objects/naming.rb
|
104
|
+
- lib/form_objects/nesting.rb
|
105
|
+
- lib/form_objects/params_converter.rb
|
106
|
+
- lib/form_objects/params_converter/collection_converter.rb
|
107
|
+
- lib/form_objects/params_converter/date_converter.rb
|
108
|
+
- lib/form_objects/serializer.rb
|
109
|
+
- lib/form_objects/version.rb
|
110
|
+
- spec/base_spec.rb
|
111
|
+
- spec/naming_spec.rb
|
112
|
+
- spec/nesting_spec.rb
|
113
|
+
- spec/params_converter_spec.rb
|
114
|
+
- spec/serializer_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
- spec/support/examples.rb
|
117
|
+
homepage: https://github.com/lluzak/form_objects
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.2.2
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Micro library for creating and managing complex forms
|
141
|
+
test_files:
|
142
|
+
- spec/base_spec.rb
|
143
|
+
- spec/naming_spec.rb
|
144
|
+
- spec/nesting_spec.rb
|
145
|
+
- spec/params_converter_spec.rb
|
146
|
+
- spec/serializer_spec.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
- spec/support/examples.rb
|