object_attorney 1.2.1 → 2.1.1

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.
Files changed (44) hide show
  1. checksums.yaml +8 -8
  2. data/db/migrate/20131205114000_create_users.rb +13 -0
  3. data/db/migrate/20131205114900_create_posts.rb +1 -1
  4. data/db/migrate/20131205114901_create_comments.rb +13 -0
  5. data/db/migrate/20131205114902_create_addresses.rb +13 -0
  6. data/lib/object_attorney/association_reflection.rb +52 -15
  7. data/lib/object_attorney/helpers.rb +4 -0
  8. data/lib/object_attorney/nested_objects.rb +88 -21
  9. data/lib/object_attorney/orm.rb +20 -10
  10. data/lib/object_attorney/orm_handlers/smooth_operator.rb +20 -15
  11. data/lib/object_attorney/reflection.rb +35 -0
  12. data/lib/object_attorney/version.rb +1 -1
  13. data/lib/object_attorney.rb +18 -10
  14. data/spec/object_attorney/bulk_post_form_spec.rb +52 -0
  15. data/spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb +37 -0
  16. data/spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb +39 -0
  17. data/spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb +91 -0
  18. data/spec/object_attorney/post_form_spec.rb +107 -41
  19. data/spec/object_attorney/post_with_comment_form_spec.rb +123 -0
  20. data/spec/object_attorney/post_with_comments_and_address_form_spec.rb +45 -0
  21. data/spec/object_attorney/user_form_spec.rb +61 -0
  22. data/spec/spec_helper.rb +15 -6
  23. data/spec/support/form_objects/bulk_posts_allow_only_existing_form.rb +19 -0
  24. data/spec/support/form_objects/bulk_posts_allow_only_new_form.rb +19 -0
  25. data/spec/support/form_objects/bulk_posts_form.rb +27 -0
  26. data/spec/support/form_objects/bulk_posts_with_form_objects_form.rb +27 -0
  27. data/spec/support/form_objects/comment_form.rb +11 -0
  28. data/spec/support/form_objects/post_form.rb +54 -0
  29. data/spec/support/form_objects/post_with_comment_form.rb +21 -0
  30. data/spec/support/form_objects/post_with_comments_and_address_form.rb +13 -0
  31. data/spec/support/form_objects/user_form.rb +11 -0
  32. data/spec/support/models/address.rb +5 -0
  33. data/spec/support/models/comment.rb +5 -0
  34. data/spec/support/models/post.rb +7 -1
  35. data/spec/support/models/user.rb +7 -0
  36. metadata +44 -19
  37. data/spec/object_attorney/bulk_posts_form_child_spec.rb +0 -191
  38. data/spec/object_attorney/bulk_posts_form_spec.rb +0 -178
  39. data/spec/object_attorney/post_form_child_spec.rb +0 -60
  40. data/spec/support/models/bulk_posts_form.rb +0 -21
  41. data/spec/support/models/bulk_posts_form_child.rb +0 -19
  42. data/spec/support/models/item.rb +0 -3
  43. data/spec/support/models/post_form.rb +0 -13
  44. data/spec/support/models/post_form_child.rb +0 -7
@@ -0,0 +1,54 @@
1
+ module PostForm
2
+
3
+ class Base
4
+
5
+ include ObjectAttorney
6
+
7
+ represents :post
8
+
9
+ delegate_properties :title, :body, to: :post
10
+
11
+ has_many :comments
12
+
13
+ validates_presence_of :title
14
+
15
+ end
16
+
17
+
18
+ class Explicit
19
+
20
+ include ObjectAttorney
21
+
22
+ represents :post
23
+
24
+ has_many :comments
25
+
26
+ validates_presence_of :title
27
+
28
+ def body=(value)
29
+ post.body = value
30
+ end
31
+
32
+ def body
33
+ post.body
34
+ end
35
+
36
+ def title=(value)
37
+ post.title = value
38
+ end
39
+
40
+ def title
41
+ post.title
42
+ end
43
+
44
+ def build_comment(attributes = {})
45
+ post.comments.build(attributes)
46
+ end
47
+
48
+ def existing_comments
49
+ post.comments
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,21 @@
1
+ module PostWithCommentForm
2
+
3
+ class Base < PostForm::Base
4
+
5
+ has_many :comments, class_name: CommentForm
6
+
7
+ end
8
+
9
+ class Explicit < PostForm::Base
10
+
11
+ def build_comment(attributes = {})
12
+ CommentForm.new(attributes)
13
+ end
14
+
15
+ def existing_comments
16
+ post.comments.map { |comment| CommentForm.new({}, comment) }
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,13 @@
1
+ class PostWithCommentsAndAddressForm
2
+
3
+ include ObjectAttorney
4
+
5
+ represents :post, properties: [:title, :body]
6
+
7
+ has_many :comments
8
+
9
+ has_one :address
10
+
11
+ validates_presence_of :title
12
+
13
+ end
@@ -0,0 +1,11 @@
1
+ class UserForm
2
+
3
+ include ObjectAttorney
4
+
5
+ represents :user, properties: [:email]
6
+
7
+ attr_accessor :terms_of_service
8
+
9
+ validates_acceptance_of :terms_of_service, accept: true, allow_nil: false
10
+
11
+ end
@@ -0,0 +1,5 @@
1
+ class Address < ActiveRecord::Base
2
+
3
+ belongs_to :post
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ class Comment < ActiveRecord::Base
2
+
3
+ belongs_to :post
4
+
5
+ end
@@ -1,3 +1,9 @@
1
1
  class Post < ActiveRecord::Base
2
- validates_presence_of :title
2
+
3
+ has_many :comments
4
+
5
+ has_one :address
6
+
7
+ belongs_to :user
8
+
3
9
  end
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ has_many :posts
4
+
5
+ validates_presence_of :email
6
+
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_attorney
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-18 00:00:00.000000000 Z
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,10 @@ files:
67
67
  - LICENSE.txt
68
68
  - README.md
69
69
  - Rakefile
70
+ - db/migrate/20131205114000_create_users.rb
70
71
  - db/migrate/20131205114900_create_posts.rb
72
+ - db/migrate/20131205114901_create_comments.rb
73
+ - db/migrate/20131205114902_create_addresses.rb
71
74
  - db/schema.rb
72
75
  - lib/object_attorney.rb
73
76
  - lib/object_attorney/association_reflection.rb
@@ -76,21 +79,33 @@ files:
76
79
  - lib/object_attorney/nested_uniqueness_validator.rb
77
80
  - lib/object_attorney/orm.rb
78
81
  - lib/object_attorney/orm_handlers/smooth_operator.rb
82
+ - lib/object_attorney/reflection.rb
79
83
  - lib/object_attorney/version.rb
80
84
  - object_attorney.gemspec
81
- - spec/object_attorney/bulk_posts_form_child_spec.rb
82
- - spec/object_attorney/bulk_posts_form_spec.rb
83
- - spec/object_attorney/post_form_child_spec.rb
85
+ - spec/object_attorney/bulk_post_form_spec.rb
86
+ - spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb
87
+ - spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
88
+ - spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb
84
89
  - spec/object_attorney/post_form_spec.rb
90
+ - spec/object_attorney/post_with_comment_form_spec.rb
91
+ - spec/object_attorney/post_with_comments_and_address_form_spec.rb
92
+ - spec/object_attorney/user_form_spec.rb
85
93
  - spec/spec_helper.rb
86
94
  - spec/support/active_model/validations.rb
87
95
  - spec/support/database_setup.rb
88
- - spec/support/models/bulk_posts_form.rb
89
- - spec/support/models/bulk_posts_form_child.rb
90
- - spec/support/models/item.rb
96
+ - spec/support/form_objects/bulk_posts_allow_only_existing_form.rb
97
+ - spec/support/form_objects/bulk_posts_allow_only_new_form.rb
98
+ - spec/support/form_objects/bulk_posts_form.rb
99
+ - spec/support/form_objects/bulk_posts_with_form_objects_form.rb
100
+ - spec/support/form_objects/comment_form.rb
101
+ - spec/support/form_objects/post_form.rb
102
+ - spec/support/form_objects/post_with_comment_form.rb
103
+ - spec/support/form_objects/post_with_comments_and_address_form.rb
104
+ - spec/support/form_objects/user_form.rb
105
+ - spec/support/models/address.rb
106
+ - spec/support/models/comment.rb
91
107
  - spec/support/models/post.rb
92
- - spec/support/models/post_form.rb
93
- - spec/support/models/post_form_child.rb
108
+ - spec/support/models/user.rb
94
109
  homepage: https://github.com/goncalvesjoao/object_attorney
95
110
  licenses:
96
111
  - MIT
@@ -118,17 +133,27 @@ summary: This gem allows you to extract the code responsible for 'validations',
118
133
  objects' and 'strong parameters' from your model onto a specific class for a specific
119
134
  use case.
120
135
  test_files:
121
- - spec/object_attorney/bulk_posts_form_child_spec.rb
122
- - spec/object_attorney/bulk_posts_form_spec.rb
123
- - spec/object_attorney/post_form_child_spec.rb
136
+ - spec/object_attorney/bulk_post_form_spec.rb
137
+ - spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb
138
+ - spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
139
+ - spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb
124
140
  - spec/object_attorney/post_form_spec.rb
141
+ - spec/object_attorney/post_with_comment_form_spec.rb
142
+ - spec/object_attorney/post_with_comments_and_address_form_spec.rb
143
+ - spec/object_attorney/user_form_spec.rb
125
144
  - spec/spec_helper.rb
126
145
  - spec/support/active_model/validations.rb
127
146
  - spec/support/database_setup.rb
128
- - spec/support/models/bulk_posts_form.rb
129
- - spec/support/models/bulk_posts_form_child.rb
130
- - spec/support/models/item.rb
147
+ - spec/support/form_objects/bulk_posts_allow_only_existing_form.rb
148
+ - spec/support/form_objects/bulk_posts_allow_only_new_form.rb
149
+ - spec/support/form_objects/bulk_posts_form.rb
150
+ - spec/support/form_objects/bulk_posts_with_form_objects_form.rb
151
+ - spec/support/form_objects/comment_form.rb
152
+ - spec/support/form_objects/post_form.rb
153
+ - spec/support/form_objects/post_with_comment_form.rb
154
+ - spec/support/form_objects/post_with_comments_and_address_form.rb
155
+ - spec/support/form_objects/user_form.rb
156
+ - spec/support/models/address.rb
157
+ - spec/support/models/comment.rb
131
158
  - spec/support/models/post.rb
132
- - spec/support/models/post_form.rb
133
- - spec/support/models/post_form_child.rb
134
- has_rdoc:
159
+ - spec/support/models/user.rb
@@ -1,191 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe BulkPostsFormChild do
4
-
5
- it "Creating multiple Posts, with a tabless model 'BulkPostsFormChild' has if it had 'accepts_nested_attributes_for :posts'", current: true do
6
- params = {
7
- bulk_post: {
8
- admin: true,
9
- posts_attributes: {
10
- "0" => { state: "draft", title: "My title1" },
11
- "1" => { state: "public", title: "My title2" }
12
- }
13
- }
14
- }
15
-
16
- bulk_posts_form_child = BulkPostsFormChild.new(params[:bulk_post])
17
- bulk_posts_form_child.save
18
-
19
- expect(Post.all.count).to(eq(2))
20
- end
21
-
22
- it "Trying to create multiple Posts, with the same title (testing the 'validates_nested_uniqueness')" do
23
- params = {
24
- bulk_post: {
25
- admin: true,
26
- posts_attributes: {
27
- "0" => { state: "draft", title: "My title1" },
28
- "1" => { state: "public", title: "My title1" }
29
- }
30
- }
31
- }
32
-
33
- bulk_posts_form = BulkPostsFormChild.new(params[:bulk_post])
34
- bulk_posts_form.save
35
-
36
- # TODO: Ensure that the nested objects remember their respective errors
37
- # see: http://stackoverflow.com/questions/13879700/rails-model-valid-flusing-custom-errors-and-falsely-returning-true
38
-
39
- expect(Post.all.count).to(eq(0))
40
- end
41
-
42
- it "Creating new Post and editing an existing one" do
43
- params = {
44
- bulk_post: {
45
- admin: true,
46
- posts_attributes: {
47
- "0" => { id: 1, state: "draft", title: "Changed title" },
48
- "1" => { state: "public", title: "My title2" }
49
- }
50
- }
51
- }
52
-
53
- existing_post = Post.create(title: "My title1")
54
- BulkPostsFormChild.new(params[:bulk_post]).save
55
- existing_post.reload
56
-
57
- expect(Post.all.count).to(eq(2)) && expect(existing_post.title).to(eq('Changed title'))
58
- end
59
-
60
- it "Creating new Post and deleting an existing one" do
61
- params = {
62
- bulk_post: {
63
- admin: true,
64
- posts_attributes: {
65
- "0" => { id: 1, state: "draft", title: "Changed title", _destroy: true },
66
- "1" => { state: "public", title: "My title2" }
67
- }
68
- }
69
- }
70
-
71
- existing_post = Post.create(title: "My title1")
72
- BulkPostsFormChild.new(params[:bulk_post]).save
73
-
74
- expect(Post.all.count).to(eq(1)) && expect(Post.where(id: existing_post.id).present?).to(eq(false))
75
- end
76
-
77
- it "Trying to create multiple Posts, but one of them is invalid" do
78
- params = {
79
- bulk_post: {
80
- admin: true,
81
- posts_attributes: {
82
- "0" => { title: "My title1" },
83
- "1" => { state: "public", title: "My title2" }
84
- }
85
- }
86
- }
87
-
88
- BulkPostsFormChild.new(params[:bulk_post]).save
89
-
90
- params = {
91
- bulk_post: {
92
- admin: true,
93
- posts_attributes: {
94
- "0" => { state: 'draft', title: "My title1" },
95
- "1" => { state: "public" }
96
- }
97
- }
98
- }
99
-
100
- BulkPostsFormChild.new(params[:bulk_post]).save
101
-
102
- expect(Post.all.count).to(eq(0))
103
- end
104
-
105
- it "Trying to create new Post and editing an existing one, but one of them is invalid" do
106
- params = {
107
- bulk_post: {
108
- admin: true,
109
- posts_attributes: {
110
- "0" => { id: 1, title: "Changed title" },
111
- "1" => { state: "public", title: "My title2" }
112
- }
113
- }
114
- }
115
-
116
- existing_post = Post.create(title: "My title1")
117
- BulkPostsFormChild.new(params[:bulk_post]).save
118
- existing_post.reload
119
-
120
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
121
-
122
- params = {
123
- bulk_post: {
124
- admin: true,
125
- posts_attributes: {
126
- "0" => { id: 1, state: 'draft', title: "Changed title" },
127
- "1" => { state: "public" }
128
- }
129
- }
130
- }
131
-
132
- BulkPostsFormChild.new(params[:bulk_post]).save
133
- existing_post.reload
134
-
135
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
136
- end
137
-
138
- it "Trying to create new Post and deleting an existing one, but the new one is invalid" do
139
- params = {
140
- bulk_post: {
141
- admin: true,
142
- posts_attributes: {
143
- "0" => { id: 1, state: "draft", title: "Changed title", _destroy: true },
144
- "1" => { state: "public" }
145
- }
146
- }
147
- }
148
-
149
- existing_post = Post.create(title: "My title1")
150
- BulkPostsFormChild.new(params[:bulk_post]).save
151
- existing_post.reload
152
-
153
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
154
- end
155
-
156
- it "Trying to create new Post and deleting an existing one, the existing one is invalid but since it is marked for destruction, it should be deleted" do
157
- params = {
158
- bulk_post: {
159
- admin: true,
160
- posts_attributes: {
161
- "0" => { id: 1, title: "Changed title", _destroy: true },
162
- "1" => { state: "public", title: "My title2" }
163
- }
164
- }
165
- }
166
-
167
- existing_post = Post.create(title: "My title1")
168
- bulk_posts_form_child = BulkPostsFormChild.new(params[:bulk_post])
169
- bulk_posts_form_child.save
170
-
171
- expect(Post.all.count).to(eq(1)) && expect(Post.where(id: existing_post.id).present?).to(eq(false))
172
- end
173
-
174
- it "Trying to create new Post and deleting an existing one, both of them are invalid, no changes should occur." do
175
- params = {
176
- bulk_post: {
177
- admin: true,
178
- posts_attributes: {
179
- "0" => { id: 1, title: "Changed title", _destroy: true },
180
- "1" => { state: "public" }
181
- }
182
- }
183
- }
184
-
185
- existing_post = Post.create(title: "My title1")
186
- BulkPostsFormChild.new(params[:bulk_post]).save
187
-
188
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
189
- end
190
-
191
- end
@@ -1,178 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe BulkPostsForm do
4
-
5
- it "Creating multiple Posts, with a tabless model 'BulkPostsForm' has if it had 'accepts_nested_attributes_for :posts'" do
6
- params = {
7
- bulk_post: {
8
- posts_attributes: {
9
- "0" => { state: "draft", title: "My title1" },
10
- "1" => { state: "public", title: "My title2" }
11
- }
12
- }
13
- }
14
-
15
- BulkPostsForm.new(params[:bulk_post]).save
16
-
17
- expect(Post.all.count).to(eq(2))
18
- end
19
-
20
- it "Creating multiple Posts, with the same title (testing the 'validates_nested_uniqueness')" do
21
- params = {
22
- bulk_post: {
23
- posts_attributes: {
24
- "0" => { state: "draft", title: "My title1" },
25
- "1" => { state: "public", title: "My title1" }
26
- }
27
- }
28
- }
29
-
30
- bulk_posts_form = BulkPostsForm.new(params[:bulk_post])
31
- bulk_posts_form.save
32
-
33
- # TODO: Ensure that the nested objects remember their respective errors
34
- # see: http://stackoverflow.com/questions/13879700/rails-model-valid-flusing-custom-errors-and-falsely-returning-true
35
-
36
- expect(Post.all.count).to(eq(0))
37
- end
38
-
39
- it "Creating new Post and editing an existing one" do
40
- params = {
41
- bulk_post: {
42
- posts_attributes: {
43
- "0" => { id: 1, state: "draft", title: "Changed title" },
44
- "1" => { state: "public", title: "My title2" }
45
- }
46
- }
47
- }
48
-
49
- existing_post = Post.create(title: "My title1")
50
- BulkPostsForm.new(params[:bulk_post]).save
51
- existing_post.reload
52
-
53
- expect(Post.all.count).to(eq(2)) && expect(existing_post.title).to(eq('Changed title'))
54
- end
55
-
56
- it "Creating new Post and deleting an existing one" do
57
- params = {
58
- bulk_post: {
59
- posts_attributes: {
60
- "0" => { id: 1, state: "draft", title: "Changed title", _destroy: true },
61
- "1" => { state: "public", title: "My title2" }
62
- }
63
- }
64
- }
65
-
66
- existing_post = Post.create(title: "My title1")
67
- BulkPostsForm.new(params[:bulk_post]).save
68
-
69
- expect(Post.all.count).to(eq(1)) && expect(Post.where(id: existing_post.id).present?).to(eq(false))
70
- end
71
-
72
- it "Trying to create multiple Posts, but one of them is invalid" do
73
- params = {
74
- bulk_post: {
75
- posts_attributes: {
76
- "0" => { title: "My title1" },
77
- "1" => { state: "public", title: "My title2" }
78
- }
79
- }
80
- }
81
-
82
- BulkPostsForm.new(params[:bulk_post]).save
83
-
84
- params = {
85
- bulk_post: {
86
- posts_attributes: {
87
- "0" => { state: 'draft', title: "My title1" },
88
- "1" => { state: "public" }
89
- }
90
- }
91
- }
92
-
93
- BulkPostsForm.new(params[:bulk_post]).save
94
-
95
- expect(Post.all.count).to(eq(0))
96
- end
97
-
98
- it "Trying to create new Post and editing an existing one, but one of them is invalid" do
99
- params = {
100
- bulk_post: {
101
- posts_attributes: {
102
- "0" => { id: 1, title: "Changed title" },
103
- "1" => { state: "public", title: "My title2" }
104
- }
105
- }
106
- }
107
-
108
- existing_post = Post.create(title: "My title1")
109
- BulkPostsForm.new(params[:bulk_post]).save
110
- existing_post.reload
111
-
112
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
113
-
114
- params = {
115
- bulk_post: {
116
- posts_attributes: {
117
- "0" => { id: 1, state: 'draft', title: "Changed title" },
118
- "1" => { state: "public" }
119
- }
120
- }
121
- }
122
-
123
- BulkPostsForm.new(params[:bulk_post]).save
124
- existing_post.reload
125
-
126
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
127
- end
128
-
129
- it "Trying to create new Post and deleting an existing one, but the new one is invalid" do
130
- params = {
131
- bulk_post: {
132
- posts_attributes: {
133
- "0" => { id: 1, state: "draft", title: "Changed title", _destroy: true },
134
- "1" => { state: "public" }
135
- }
136
- }
137
- }
138
-
139
- existing_post = Post.create(title: "My title1")
140
- BulkPostsForm.new(params[:bulk_post]).save
141
- existing_post.reload
142
-
143
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
144
- end
145
-
146
- it "Trying to create new Post and deleting an existing one, the existing one is invalid but since it is marked for destruction, it should be deleted" do
147
- params = {
148
- bulk_post: {
149
- posts_attributes: {
150
- "0" => { id: 1, title: "Changed title", _destroy: true },
151
- "1" => { state: "public", title: "My title2" }
152
- }
153
- }
154
- }
155
-
156
- existing_post = Post.create(title: "My title1")
157
- BulkPostsForm.new(params[:bulk_post]).save
158
-
159
- expect(Post.all.count).to(eq(1)) && expect(Post.where(id: existing_post.id).present?).to(eq(false))
160
- end
161
-
162
- it "Trying to create new Post and deleting an existing one, both of them are invalid, no changes should occur." do
163
- params = {
164
- bulk_post: {
165
- posts_attributes: {
166
- "0" => { id: 1, title: "Changed title", _destroy: true },
167
- "1" => { state: "public" }
168
- }
169
- }
170
- }
171
-
172
- existing_post = Post.create(title: "My title1")
173
- BulkPostsForm.new(params[:bulk_post]).save
174
-
175
- expect(Post.all.count).to(eq(1)) && expect(existing_post.title).to(eq('My title1'))
176
- end
177
-
178
- end
@@ -1,60 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe PostFormChild do
4
-
5
- it "PostFormChild becomes invalid when Post does and incorporates its errors" do
6
- post = Post.new
7
- post.should have(1).error_on(:title)
8
- post.title = "My title"
9
- post.should have(:no).errors_on(:title)
10
-
11
- post_form = PostFormChild.new({ state: 'draft', date: Date.today })
12
- post_form.should have(1).error_on(:title)
13
- post_form.title = "My title"
14
- post_form.should have(:no).errors_on(:title)
15
- end
16
-
17
- it "PostFormChild may require the validations of fields that Post doesn't have" do
18
- params = { post: { title: "My title" } }
19
-
20
- post = Post.new(params[:post])
21
- post.should have(:no).errors_on(:date)
22
-
23
- post_form = PostFormChild.new(params[:post].merge(state: 'public'))
24
- post_form.should have(1).error_on(:date)
25
- post_form.date = Date.today
26
- post_form.should have(:no).errors_on(:date)
27
- end
28
-
29
- it "Post creation through PostFormChild" do
30
- params = { post: { state: 'public', title: "My title", body: "My body", date: Date.today } }
31
- post_form = PostFormChild.new(params[:post])
32
-
33
- expect(post_form.save).to(eq(true)) && expect(post_form.post.persisted?).to(eq(true))
34
- end
35
-
36
- it "Post can't be created if PostFormChild isn't valid" do
37
- params = { post: { state: 'public', title: "My title", body: "My body" } }
38
- post_form = PostFormChild.new(params[:post])
39
-
40
- expect(post_form.save).to(eq(false)) && expect(post_form.post.persisted?).to(eq(false))
41
- end
42
-
43
- it "Post can't be created if Post isn't valid" do
44
- params = { post: { state: 'public', date: Date.today, body: "My body" } }
45
- post_form = PostFormChild.new(params[:post])
46
-
47
- expect(post_form.save).to(eq(false)) && expect(post_form.post.persisted?).to(eq(false))
48
- end
49
-
50
- it "PostFormChild won't allow weak params to be updated, unlike Post" do
51
- params = { post: { title: 'My title', body: "My body", admin: true } }
52
-
53
- post_form = PostFormChild.new(params[:post].merge({ state: 'public', date: Date.today }))
54
- expect(post_form.save).to(eq(true)) && expect(post_form.post.admin).to(eq(false))
55
-
56
- post = Post.new(params[:post])
57
- expect(post.save).to(eq(true)) && expect(post.admin).to(eq(true))
58
- end
59
-
60
- end
@@ -1,21 +0,0 @@
1
- require 'object_attorney/nested_uniqueness_validator'
2
-
3
- class BulkPostsForm
4
-
5
- include ObjectAttorney
6
-
7
- accepts_nested_objects :posts
8
-
9
- validates_nested_uniqueness :posts, uniq_value: :title
10
-
11
- ##################### BODY BELLOW THIS LINE ####################
12
-
13
- def build_post(attributes = {}, post = nil)
14
- PostForm.new(attributes, post)
15
- end
16
-
17
- def existing_posts
18
- Post.all.map { |post| build_post({}, post) }
19
- end
20
-
21
- end