object_attorney 2.10.12 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +48 -17
  3. data/.rubocop.yml +19 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +4 -0
  6. data/Gemfile +0 -13
  7. data/Gemfile.lock +83 -0
  8. data/README.md +25 -18
  9. data/Rakefile +7 -16
  10. data/lib/object_attorney.rb +57 -44
  11. data/lib/object_attorney/accusation.rb +46 -0
  12. data/lib/object_attorney/allegation.rb +20 -0
  13. data/lib/object_attorney/base.rb +17 -0
  14. data/lib/object_attorney/class_methods.rb +50 -0
  15. data/lib/object_attorney/errors.rb +32 -0
  16. data/lib/object_attorney/helpers.rb +22 -21
  17. data/lib/object_attorney/validations/custom.rb +23 -0
  18. data/lib/object_attorney/version.rb +3 -1
  19. data/object_attorney.gemspec +24 -17
  20. data/spec/object_attorney/base_spec.rb +21 -0
  21. data/spec/object_attorney/custom_validation_spec.rb +60 -0
  22. data/spec/object_attorney/object_attorney_spec.rb +64 -0
  23. data/spec/spec_helper.rb +17 -56
  24. data/spec/support/post.rb +7 -0
  25. data/spec/support/user.rb +5 -0
  26. metadata +102 -127
  27. data/.rvmrc +0 -1
  28. data/Guardfile +0 -5
  29. data/console.rb +0 -36
  30. data/db/migrate/20131205114000_create_users.rb +0 -13
  31. data/db/migrate/20131205114900_create_posts.rb +0 -14
  32. data/db/migrate/20131205114901_create_comments.rb +0 -13
  33. data/db/migrate/20131205114902_create_addresses.rb +0 -14
  34. data/db/schema.rb +0 -22
  35. data/lib/object_attorney/association_reflection.rb +0 -72
  36. data/lib/object_attorney/attribute_assignment.rb +0 -69
  37. data/lib/object_attorney/delegation.rb +0 -48
  38. data/lib/object_attorney/naming.rb +0 -16
  39. data/lib/object_attorney/nested_objects.rb +0 -311
  40. data/lib/object_attorney/orm_handlers/smooth_operator.rb +0 -69
  41. data/lib/object_attorney/record.rb +0 -117
  42. data/lib/object_attorney/reflection.rb +0 -40
  43. data/lib/object_attorney/representation.rb +0 -93
  44. data/lib/object_attorney/serialization.rb +0 -43
  45. data/lib/object_attorney/translation.rb +0 -25
  46. data/lib/object_attorney/validations.rb +0 -46
  47. data/not_used/nested_uniqueness_validator.rb +0 -33
  48. data/spec/object_attorney/address_form_spec.rb +0 -45
  49. data/spec/object_attorney/bulk_post_form_spec.rb +0 -52
  50. data/spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb +0 -37
  51. data/spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb +0 -39
  52. data/spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb +0 -91
  53. data/spec/object_attorney/delegation_spec.rb +0 -142
  54. data/spec/object_attorney/nested_objects_spec.rb +0 -63
  55. data/spec/object_attorney/post_form_spec.rb +0 -154
  56. data/spec/object_attorney/post_validations_form_spec.rb +0 -46
  57. data/spec/object_attorney/post_with_comment_form_spec.rb +0 -123
  58. data/spec/object_attorney/post_with_comment_validations_form_spec.rb +0 -46
  59. data/spec/object_attorney/post_with_comments_and_address_form_spec.rb +0 -54
  60. data/spec/object_attorney/post_with_only_existing_comments_form_spec.rb +0 -38
  61. data/spec/object_attorney/post_with_only_new_comments_form_spec.rb +0 -42
  62. data/spec/object_attorney/serialization_spec.rb +0 -40
  63. data/spec/object_attorney/test_spec.rb +0 -23
  64. data/spec/object_attorney/user_and_comments_form_spec.rb +0 -56
  65. data/spec/object_attorney/user_form_spec.rb +0 -70
  66. data/spec/require_helper.rb +0 -32
  67. data/spec/support/active_model/validations.rb +0 -21
  68. data/spec/support/database_setup.rb +0 -14
  69. data/spec/support/form_objects/address_form.rb +0 -9
  70. data/spec/support/form_objects/bulk_posts_allow_only_existing_form.rb +0 -19
  71. data/spec/support/form_objects/bulk_posts_allow_only_new_form.rb +0 -19
  72. data/spec/support/form_objects/bulk_posts_form.rb +0 -27
  73. data/spec/support/form_objects/bulk_posts_with_form_objects_form.rb +0 -27
  74. data/spec/support/form_objects/comment_form.rb +0 -11
  75. data/spec/support/form_objects/post_form.rb +0 -120
  76. data/spec/support/form_objects/post_validations_form.rb +0 -17
  77. data/spec/support/form_objects/post_with_comment_form.rb +0 -21
  78. data/spec/support/form_objects/post_with_comment_validations_form.rb +0 -17
  79. data/spec/support/form_objects/post_with_comments_and_address_form.rb +0 -13
  80. data/spec/support/form_objects/post_with_only_existing_comments_form.rb +0 -9
  81. data/spec/support/form_objects/post_with_only_new_comments_form.rb +0 -9
  82. data/spec/support/form_objects/user_and_comments_form.rb +0 -11
  83. data/spec/support/form_objects/user_form.rb +0 -11
  84. data/spec/support/models/address.rb +0 -5
  85. data/spec/support/models/comment.rb +0 -5
  86. data/spec/support/models/post.rb +0 -9
  87. data/spec/support/models/user.rb +0 -7
@@ -1,154 +0,0 @@
1
- require "spec_helper"
2
-
3
- shared_examples "a PostForm" do
4
-
5
- it "1. Creating a 'Post' with nested 'Comment's, through 'FormObjects::Post'" do
6
- params = {
7
- post: {
8
- title: 'First post',
9
- body: 'post body',
10
- comments_attributes: {
11
- "0" => { body: "body1" },
12
- "1" => { body: "" }
13
- }
14
- }
15
- }
16
-
17
- post_form = described_class.new(params[:post])
18
-
19
- post_form.save.should == true
20
- post_form.comments.length.should == 2
21
-
22
- Post.all.count.should == 1
23
- post = Post.first
24
- post.title.should == 'First post'
25
- post.body.should == 'post body'
26
-
27
- post.comments.count.should == 2
28
-
29
- comment = post.comments.first
30
- comment.post_id.should == post.id
31
- comment.body.should == 'body1'
32
- end
33
-
34
- it "2. Editing a 'Post' and a nested 'Comment'." do
35
- params = {
36
- id: 1,
37
- post: {
38
- title: "altered post",
39
- comments_attributes: {
40
- "0" => { id: 1, body: "altered comment" }
41
- }
42
- }
43
- }
44
-
45
- Post.create(title: "My title1")
46
- Post.first.title.should == 'My title1'
47
- Comment.create(post_id: 1, body: "body1")
48
- Comment.first.body.should == 'body1'
49
-
50
- post_form = described_class.new(params[:post], Post.find(params[:id]))
51
-
52
- post_form.save.should == true
53
- post_form.comments.length.should == 1
54
-
55
- post = Post.first
56
- post.title.should == 'altered post'
57
-
58
- comment = post.comments.first
59
- comment.post_id.should == post.id
60
- comment.body.should == 'altered comment'
61
- end
62
-
63
- it "3. Editing a 'Post' and deleting a nested 'Comment'." do
64
- params = {
65
- id: 1,
66
- post: {
67
- title: "altered post",
68
- comments_attributes: {
69
- "0" => { id: 1, _destroy: true }
70
- }
71
- }
72
- }
73
-
74
- Post.create(title: "My title1")
75
- Post.first.title.should == 'My title1'
76
- Comment.create(post_id: 1, body: "body1")
77
- Comment.all.count.should == 1
78
-
79
- post_form = described_class.new(params[:post], Post.find(params[:id]))
80
-
81
- post_form.save.should == true
82
- post_form.comments.length.should == 1
83
-
84
- Post.first.title.should == 'altered post'
85
- Comment.all.count.should == 0
86
- end
87
-
88
- it "4. Editing a 'Post', creating new nested 'Comment', editing another and deleting yet another." do
89
- params = {
90
- id: 1,
91
- post: {
92
- title: "altered post",
93
- comments_attributes: {
94
- "0" => { body: "new comment" },
95
- "1" => { id: 1, body: 'to be destroyed', _destroy: true },
96
- "2" => { id: 2, body: 'altered comment' }
97
- }
98
- }
99
- }
100
-
101
- Post.create(title: "My title1")
102
- Post.first.title.should == 'My title1'
103
- Comment.create(post_id: 1, body: "body1")
104
- Comment.create(post_id: 1, body: "body2")
105
- Comment.all.count.should == 2
106
-
107
- post_form = described_class.new(params[:post], Post.find(params[:id]))
108
-
109
- post_form.save.should == true
110
- post_form.comments.length.should == 3
111
-
112
- post = Post.first
113
- post.title.should == 'altered post'
114
- post.comments.count.should == 2
115
-
116
- comment = post.comments.where(id: 2).first
117
- comment.post_id.should == post.id
118
- comment.body.should == 'altered comment'
119
-
120
- comment = post.comments.where(id: 3).first
121
- comment.post_id.should == post.id
122
- comment.body.should == 'new comment'
123
- end
124
-
125
- it "5. Creating new 'Comment's and deleting them at the same time." do
126
- params = {
127
- id: 1,
128
- post: {
129
- title: "altered post",
130
- comments_attributes: {
131
- "0" => { body: '1', _destroy: true },
132
- "1" => { body: '2' },
133
- "2" => { body: '3', _destroy: true }
134
- }
135
- }
136
- }
137
-
138
- post_form = described_class.new(params[:post])
139
-
140
- post_form.comments.length.should == 1
141
- post_form.save.should == true
142
- post_form.comments.length.should == 1
143
- Comment.all.count.should == 1
144
- end
145
-
146
- end
147
-
148
- describe PostForm::Base do
149
- it_behaves_like 'a PostForm'
150
- end
151
-
152
- describe PostForm::Explicit do
153
- it_behaves_like 'a PostForm'
154
- end
@@ -1,46 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe PostValidationsForm do
4
-
5
- it "1. 'PostValidationsForm' becomes invalid if 'Post' has errors after the #submit method and incorporates its errors." do
6
- params = {
7
- post: {
8
- title: 'First post',
9
- body: 'post body',
10
- comments_attributes: {
11
- "0" => { body: "body1" }
12
- }
13
- }
14
- }
15
-
16
- post_form = PostValidationsForm.new(params[:post])
17
- post_form.valid?.should == true
18
-
19
- post_form.save
20
-
21
- post_form.should have(1).error_on(:title)
22
- post_form.should have(1).error_on(:comments)
23
- post_form.errors.size.should == 2
24
- post_form.comments.first.should have(1).error_on(:body)
25
-
26
- post_form.valid?.should == false
27
- post_form.should have(1).error_on(:title)
28
- post_form.should have(1).error_on(:comments)
29
- post_form.errors.size.should == 2
30
- post_form.comments.first.should have(1).error_on(:body)
31
-
32
- post_form.save
33
-
34
- post_form.should have(1).error_on(:title)
35
- post_form.should have(1).error_on(:comments)
36
- post_form.errors.size.should == 2
37
- post_form.comments.first.should have(1).error_on(:body)
38
-
39
- post_form.valid?.should == false
40
- post_form.should have(1).error_on(:title)
41
- post_form.should have(1).error_on(:comments)
42
- post_form.errors.size.should == 2
43
- post_form.comments.first.should have(1).error_on(:body)
44
- end
45
-
46
- end
@@ -1,123 +0,0 @@
1
- require "spec_helper"
2
-
3
- shared_examples "a PostWithCommentForm" do
4
-
5
- it "1. 'Post' can't be created if any of the nested comments on 'FormObjects::PostWithCommentForm' isn't valid" do
6
- params = {
7
- post: {
8
- title: 'First post',
9
- body: 'post body',
10
- comments_attributes: {
11
- "0" => { body: "body1" },
12
- "1" => { body: "" }
13
- }
14
- }
15
- }
16
-
17
- post2_form = described_class.new(params[:post])
18
- post2_form.save
19
-
20
- Post.all.count.should == 0
21
- Comment.all.count.should == 0
22
- post2_form.comments.second.should have(1).errors_on(:body)
23
- end
24
-
25
- it "2. Editing a 'Post', creating new nested 'Comment' (with errors), editing another and deleting yet another (none of the changes should take place)." do
26
- params = {
27
- id: 1,
28
- post: {
29
- title: "altered post",
30
- comments_attributes: {
31
- "0" => {},
32
- "1" => { id: 1, body: 'to be destroyed', _destroy: true },
33
- "2" => { id: 2, body: 'altered comment' }
34
- }
35
- }
36
- }
37
-
38
- Post.create(title: "My title1")
39
- Post.first.title.should == 'My title1'
40
- Comment.create(post_id: 1, body: "body1")
41
- Comment.create(post_id: 1, body: "body2")
42
- Comment.all.count.should == 2
43
-
44
- post_form = described_class.new(params[:post], Post.find(params[:id]))
45
- post_form.save
46
-
47
- Post.first.title.should == 'My title1'
48
- Comment.all.count.should == 2
49
- Comment.find_by_id(1).body.should == 'body1'
50
- Comment.find_by_id(2).body.should == 'body2'
51
- end
52
-
53
- it "3. Editing a 'Post' (with errors), creating new nested 'Comment', editing another and deleting yet another (none of the changes should take place)." do
54
- params = {
55
- id: 1,
56
- post: {
57
- title: "",
58
- comments_attributes: {
59
- "0" => { body: "new comment" },
60
- "1" => { id: 1, body: 'to be destroyed', _destroy: true },
61
- "2" => { id: 2, body: 'altered comment' }
62
- }
63
- }
64
- }
65
-
66
- Post.create(title: "My title1")
67
- Post.first.title.should == 'My title1'
68
- Comment.create(post_id: 1, body: "body1")
69
- Comment.create(post_id: 1, body: "body2")
70
- Comment.all.count.should == 2
71
-
72
- post_form = described_class.new(params[:post], Post.find(params[:id]))
73
- post_form.save
74
-
75
- Post.first.title.should == 'My title1'
76
- Comment.all.count.should == 2
77
- Comment.find_by_id(1).body.should == 'body1'
78
- Comment.find_by_id(2).body.should == 'body2'
79
- end
80
-
81
- it "4. Editing a 'Post', creating new nested 'Comment', editing another and deleting (with errors) yet another (all changes should take place!)." do
82
- params = {
83
- id: 1,
84
- post: {
85
- title: "altered post",
86
- comments_attributes: {
87
- "0" => { body: "new comment" },
88
- "1" => { id: 1, _destroy: true },
89
- "2" => { id: 2, body: 'altered comment' }
90
- }
91
- }
92
- }
93
-
94
- Post.create(title: "My title1")
95
- Post.first.title.should == 'My title1'
96
- Comment.create(post_id: 1, body: "body1")
97
- Comment.create(post_id: 1, body: "body2")
98
- Comment.all.count.should == 2
99
-
100
- post_form = described_class.new(params[:post], Post.find(params[:id]))
101
- post_form.save
102
-
103
- Post.first.title.should == 'altered post'
104
- Comment.all.count.should == 2
105
-
106
- comment = Comment.find_by_id(2)
107
- comment.body.should == 'altered comment'
108
- comment.post_id.should == 1
109
-
110
- comment = Comment.find_by_id(3)
111
- comment.body.should == 'new comment'
112
- comment.post_id.should == 1
113
- end
114
-
115
- end
116
-
117
- describe PostWithCommentForm::Base do
118
- it_behaves_like 'a PostWithCommentForm'
119
- end
120
-
121
- describe PostWithCommentForm::Explicit do
122
- it_behaves_like 'a PostWithCommentForm'
123
- end
@@ -1,46 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe PostWithCommentValidationsForm do
4
-
5
- it "1. 'PostWithCommentValidationsForm' becomes invalid if 'Post' or nested 'Comment's has errors after the #submit method and incorporates its errors." do
6
- params = {
7
- post: {
8
- title: 'First post',
9
- body: 'post body',
10
- comments_attributes: {
11
- "0" => { body: "body1" }
12
- }
13
- }
14
- }
15
-
16
- post_form = PostWithCommentValidationsForm.new(params[:post])
17
- post_form.valid?.should == true
18
-
19
- post_form.save
20
-
21
- post_form.should have(1).error_on(:title)
22
- post_form.should have(1).error_on(:comments)
23
- post_form.errors.size.should == 2
24
- post_form.comments.first.should have(1).error_on(:body)
25
-
26
- post_form.valid?.should == false
27
- post_form.should have(1).error_on(:title)
28
- post_form.should have(1).error_on(:comments)
29
- post_form.errors.size.should == 2
30
- post_form.comments.first.should have(1).error_on(:body)
31
-
32
- post_form.save
33
-
34
- post_form.should have(1).error_on(:title)
35
- post_form.should have(1).error_on(:comments)
36
- post_form.errors.size.should == 2
37
- post_form.comments.first.should have(1).error_on(:body)
38
-
39
- post_form.valid?.should == false
40
- post_form.should have(1).error_on(:title)
41
- post_form.should have(1).error_on(:comments)
42
- post_form.errors.size.should == 2
43
- post_form.comments.first.should have(1).error_on(:body)
44
- end
45
-
46
- end
@@ -1,54 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe PostWithCommentsAndAddressForm do
4
-
5
- it "1. Creating a 'Post' with nested 'Comment's and a single 'Address'" do
6
- params = {
7
- post: {
8
- title: 'First post',
9
- body: 'post body',
10
- comments_attributes: {
11
- "0" => { body: "body1" },
12
- "1" => { body: "" }
13
- },
14
- address_attributes: {
15
- street: 'street',
16
- city: 'city'
17
- }
18
- }
19
- }
20
-
21
- post_form = described_class.new(params[:post])
22
-
23
- post_form.save.should == true
24
- post_form.address.present?.should == true
25
-
26
- Post.all.count.should == 1
27
- post = Post.first
28
- post.title.should == 'First post'
29
- post.body.should == 'post body'
30
-
31
- post.comments.count.should == 2
32
-
33
- comment = post.comments.first
34
- comment.post_id.should == post.id
35
- comment.body.should == 'body1'
36
-
37
- comment = post.comments.second
38
- comment.post_id.should == post.id
39
- comment.body.should == ''
40
-
41
- post.address.present?.should == true
42
- address = Address.first
43
- address.post_id.should == post.id
44
- address.street.should == 'street'
45
- address.city.should == 'city'
46
- end
47
-
48
- it "2. Checking is nestes attributes ':comments' and ':address' are initialized" do
49
- post_form = described_class.new
50
- post_form.comments_attributes.should == {}
51
- post_form.address_attributes.should == {}
52
- end
53
-
54
- end
@@ -1,38 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe PostWithOnlyExistingCommentsForm do
4
-
5
- it "1. Editing nested 'Comments' inside a PostForm that only accepts existing Comments changes and ignores new Comments." do
6
- params = {
7
- id: 1,
8
- post: {
9
- title: "altered post",
10
- comments_attributes: {
11
- "0" => { body: "new comment" },
12
- "1" => { id: 1, _destroy: true },
13
- "2" => { id: 2, body: 'altered comment' }
14
- }
15
- }
16
- }
17
-
18
- Post.create(title: "My title1")
19
- Post.first.title.should == 'My title1'
20
- Comment.create(post_id: 1, body: "body1")
21
- Comment.create(post_id: 1, body: "body2")
22
- Comment.all.count.should == 2
23
-
24
- post_form = PostWithOnlyExistingCommentsForm.new(params[:post], Post.find(params[:id]))
25
- post_form.save
26
-
27
- comment = Comment.find_by_id(1)
28
- comment.should == nil
29
-
30
- comment = Comment.find_by_id(2)
31
- comment.body.should == 'altered comment'
32
-
33
- Comment.all.count.should == 1
34
- Post.first.title.should == 'altered post'
35
-
36
- end
37
-
38
- end