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,45 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe AddressForm do
4
-
5
- it "FormObject with a belongs_to with a different class then the represented_object's relation" do
6
- params = {
7
- address: {
8
- post_attributes: { title: 'asd', body: 'body' }
9
- }
10
- }
11
-
12
- address_form = AddressForm.new(params[:address])
13
-
14
- address_form.address.post.should == nil
15
- address_form.post
16
- address_form.address.post.should_not == nil
17
- end
18
-
19
- it "FormObject receiving a _destroy attribute, should mark the relevant represented_object, for destruction too" do
20
- Post.create(title: 'title', body: 'body')
21
-
22
- address = Address.create(street: 'street', city: 'city', post_id: 1)
23
-
24
- params = {
25
- address: {
26
- post_attributes: { id: 1, _destroy: true }
27
- }
28
- }
29
-
30
- address_form = AddressForm.new(params[:address], address)
31
-
32
- address_form.post.marked_for_destruction?.should == true
33
- address_form.address.post.marked_for_destruction?.should == true
34
- end
35
-
36
- it "FormObject initialized with a marked_for_destruction object, should reflect that" do
37
- address = Address.create(street: 'street', city: 'city')
38
- address.mark_for_destruction
39
-
40
- address_form = AddressForm.new({}, address)
41
-
42
- address_form.marked_for_destruction?.should == true
43
- end
44
-
45
- end
@@ -1,52 +0,0 @@
1
- require "spec_helper"
2
-
3
-
4
- shared_examples "a BulkPostsForm" do
5
-
6
- it "1. Creating multiple 'Post's, with a tabless model 'BulkPostsForm' has if it had 'accepts_nested_attributes_for :posts'" do
7
- params = {
8
- bulk_post: {
9
- posts_attributes: {
10
- "0" => { title: "My title1" },
11
- "1" => { title: "My title2" }
12
- }
13
- }
14
- }
15
-
16
- described_class.new(params[:bulk_post]).save
17
-
18
- Post.all.count.should == 2
19
- end
20
-
21
- it "2. Creating new 'Post', editing another and deleting yet another." do
22
- params = {
23
- bulk_post: {
24
- posts_attributes: {
25
- "0" => { title: "new post" },
26
- "2" => { id: 1, title: 'altered post' },
27
- "1" => { id: 2, title: 'to be destroyed', _destroy: true }
28
- }
29
- }
30
- }
31
-
32
- Post.create(title: "My title1")
33
- Post.create(title: "My title2")
34
- Post.all.count.should == 2
35
- Post.first.title.should == 'My title1'
36
-
37
- described_class.new(params[:bulk_post]).save
38
-
39
- Post.all.count.should == 2
40
- Post.find_by_id(1).title.should == 'altered post'
41
- Post.find_by_id(3).title.should == 'new post'
42
- end
43
-
44
- end
45
-
46
- describe BulkPostsForm::Base do
47
- it_behaves_like 'a BulkPostsForm'
48
- end
49
-
50
- describe BulkPostsForm::Explicit do
51
- it_behaves_like 'a BulkPostsForm'
52
- end
@@ -1,37 +0,0 @@
1
- require "spec_helper"
2
-
3
- shared_examples "a BulkPostsAllowOnlyExistingForm" do
4
-
5
- it "1. Tabless model 'BulkPostsAllowOnlyNew' only accepts editings 'Post' requests and ignores new requests." do
6
- params = {
7
- bulk_post: {
8
- posts_attributes: {
9
- "0" => { title: "new post" },
10
- "1" => { id: 1, title: 'altered post' },
11
- "2" => { id: 2, title: '', _destroy: true }
12
- }
13
- }
14
- }
15
-
16
- Post.create(title: "My title1")
17
- Post.create(title: "My title2")
18
- Post.all.count.should == 2
19
- Post.find_by_id(1).title.should == 'My title1'
20
- Post.find_by_id(2).title.should == 'My title2'
21
-
22
- buld_posts_form = described_class.new(params[:bulk_post])
23
- buld_posts_form.save
24
-
25
- Post.all.count.should == 1
26
- Post.first.title.should == 'altered post'
27
- end
28
-
29
- end
30
-
31
- describe BulkPostsAllowOnlyExistingForm::Base do
32
- it_behaves_like 'a BulkPostsAllowOnlyExistingForm'
33
- end
34
-
35
- describe BulkPostsAllowOnlyExistingForm::Explicit do
36
- it_behaves_like 'a BulkPostsAllowOnlyExistingForm'
37
- end
@@ -1,39 +0,0 @@
1
- require "spec_helper"
2
-
3
- shared_examples "a BulkPostsAllowOnlyNewForm" do
4
-
5
- it "1. Tabless model 'BulkPostsAllowOnlyNewForm' only accepts new 'Post' requests and ignores editing requests." do
6
- params = {
7
- bulk_post: {
8
- posts_attributes: {
9
- "0" => { title: "new post" },
10
- "1" => { id: 1, title: 'altered post' },
11
- "2" => { id: 2, title: '', _destroy: true }
12
- }
13
- }
14
- }
15
-
16
- Post.create(title: "My title1")
17
- Post.create(title: "My title2")
18
- Post.all.count.should == 2
19
- Post.find_by_id(1).title.should == 'My title1'
20
- Post.find_by_id(2).title.should == 'My title2'
21
-
22
- buld_posts_form = described_class.new(params[:bulk_post])
23
- buld_posts_form.save
24
-
25
- Post.all.count.should == 3
26
- Post.find_by_id(1).title.should == 'My title1'
27
- Post.find_by_id(2).title.should == 'My title2'
28
- Post.find_by_id(3).title.should == 'new post'
29
- end
30
-
31
- end
32
-
33
- describe BulkPostsAllowOnlyNewForm::Base do
34
- it_behaves_like 'a BulkPostsAllowOnlyNewForm'
35
- end
36
-
37
- describe BulkPostsAllowOnlyNewForm::Explicit do
38
- it_behaves_like 'a BulkPostsAllowOnlyNewForm'
39
- end
@@ -1,91 +0,0 @@
1
- require "spec_helper"
2
-
3
- shared_examples "a BulkPostsWithFormObjectsForm" do
4
-
5
- it "1. If any of the 'Post's is invalid, no changes should take effect." do
6
- params = {
7
- bulk_post: {
8
- posts_attributes: {
9
- "0" => { title: "new post" },
10
- "1" => { id: 1, title: '' },
11
- "2" => { id: 2, title: 'to be destroyed', _destroy: true }
12
- }
13
- }
14
- }
15
-
16
- Post.create(title: "My title1")
17
- Post.create(title: "My title2")
18
- Post.all.count.should == 2
19
- Post.find_by_id(1).title.should == 'My title1'
20
- Post.find_by_id(2).title.should == 'My title2'
21
-
22
- bulk_posts_form = described_class.new(params[:bulk_post])
23
- bulk_posts_form.save
24
-
25
- bulk_posts_form.posts.first.should have(1).errors_on(:title)
26
- Post.all.count.should == 2
27
- Post.find_by_id(1).title.should == 'My title1'
28
- Post.find_by_id(2).title.should == 'My title2'
29
- end
30
-
31
- it "2. A soon to be deleted 'Post' that is invalid, should not stop all other changes." do
32
- params = {
33
- bulk_post: {
34
- posts_attributes: {
35
- "0" => { title: "new post" },
36
- "1" => { id: 1, title: 'altered post' },
37
- "2" => { id: 2, title: '', _destroy: true }
38
- }
39
- }
40
- }
41
-
42
- Post.create(title: "My title1")
43
- Post.create(title: "My title2")
44
- Post.all.count.should == 2
45
- Post.find_by_id(1).title.should == 'My title1'
46
- Post.find_by_id(2).title.should == 'My title2'
47
-
48
- bulk_posts_form = described_class.new(params[:bulk_post])
49
- bulk_posts_form.save
50
-
51
- bulk_posts_form.posts.second.id.should == 2
52
- bulk_posts_form.posts.second.persisted?.should == false
53
- bulk_posts_form.posts.second.should have(:no).errors_on(:title)
54
- Post.all.count.should == 2
55
- Post.find_by_id(1).title.should == 'altered post'
56
- Post.find_by_id(3).title.should == 'new post'
57
- end
58
-
59
- it "3. 'BulkPostsWithFormObjectsForm' should be importing all of the represented objects errors." do
60
- params = {
61
- bulk_post: {
62
- posts_attributes: {
63
- "0" => { title: "" },
64
- "1" => { id: 1, title: '' },
65
- "2" => { id: 2, title: '', _destroy: true }
66
- }
67
- }
68
- }
69
-
70
- Post.create(title: "My title1")
71
- Post.create(title: "My title2")
72
- Post.all.count.should == 2
73
-
74
- bulk_posts_form = described_class.new(params[:bulk_post])
75
- bulk_posts_form.save
76
-
77
- bulk_posts_form.should have(1).errors_on(:posts)
78
- bulk_posts_form.posts.first.should have(1).errors_on(:title)
79
- bulk_posts_form.posts.second.should have(:no).errors_on(:title)
80
- bulk_posts_form.posts.third.should have(1).errors_on(:title)
81
- end
82
-
83
- end
84
-
85
- describe BulkPostsWithFormObjectsForm::Base do
86
- it_behaves_like 'a BulkPostsWithFormObjectsForm'
87
- end
88
-
89
- describe BulkPostsWithFormObjectsForm::Explicit do
90
- it_behaves_like 'a BulkPostsWithFormObjectsForm'
91
- end
@@ -1,142 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe PostForm::Presenter do
4
-
5
- it "1. Editing a 'Post', through one property and delegating all missing methods to the represented object" do
6
- params = {
7
- post: {
8
- title: 'altered title',
9
- body: 'altered body',
10
- user_id: 666
11
- }
12
- }
13
-
14
- post = Post.create({ title: 'First post', body: 'post body', user_id: 1 })
15
-
16
- post_form = described_class.new(params[:post], post)
17
-
18
- post_form.save.should == true
19
-
20
- Post.all.count.should == 1
21
- post = Post.first
22
- post.title.should == 'altered title'
23
- post.body.should == 'post body'
24
- post.user_id.should == 1
25
-
26
- post_form.title.should == 'altered title'
27
- post_form.body.should == 'post body'
28
- post_form.user_id.should == 1
29
-
30
- post_form.user_id = 665
31
- post_form.user_id.should == 665
32
- end
33
-
34
- end
35
-
36
-
37
- shared_examples "a PostForm with delegated properties" do
38
-
39
- it "1. Creating a 'Post' with 3 params, but only 2 are delegated" do
40
- params = {
41
- post: {
42
- title: 'First post',
43
- body: 'post body',
44
- user_id: 666
45
- }
46
- }
47
-
48
- post_form = described_class.new(params[:post])
49
-
50
- post_form.save.should == true
51
-
52
- Post.all.count.should == 1
53
- post = Post.first
54
- post.title.should == 'First post'
55
- post.body.should == nil
56
- post.user_id.should == 666
57
- end
58
-
59
- end
60
-
61
- describe PostForm::Properties1 do
62
- it_behaves_like 'a PostForm with delegated properties'
63
- end
64
-
65
- describe PostForm::Properties2 do
66
- it_behaves_like 'a PostForm with delegated properties'
67
- end
68
-
69
-
70
- shared_examples "a PostForm with only delegated getters" do
71
-
72
- it "1. Editing a 'Post' with 3 params, no changes should take place but the getters should" do
73
- params = {
74
- post: {
75
- title: 'altered title',
76
- body: 'altered body',
77
- user_id: 666
78
- }
79
- }
80
-
81
- post = Post.create({ title: 'First post', body: 'post body', user_id: 1 })
82
-
83
- post_form = described_class.new(params[:post], post)
84
-
85
- post_form.save.should == true
86
-
87
- Post.all.count.should == 1
88
- post = Post.first
89
- post.title.should == 'First post'
90
- post.body.should == 'post body'
91
- post.user_id.should == 1
92
-
93
- post_form.title.should == 'First post'
94
- post_form.user_id.should == 1
95
- post_form.respond_to?(:body).should == false
96
- end
97
-
98
- end
99
-
100
- describe PostForm::Getters1 do
101
- it_behaves_like 'a PostForm with only delegated getters'
102
- end
103
-
104
- describe PostForm::Getters2 do
105
- it_behaves_like 'a PostForm with only delegated getters'
106
- end
107
-
108
-
109
- shared_examples "a PostForm with only delegated setters" do
110
-
111
- it "1. Editing a 'Post' with 3 params, changes should take place and the getters should not work" do
112
- params = {
113
- post: {
114
- title: 'altered title', body: 'altered body', user_id: 666
115
- }
116
- }
117
-
118
- post = Post.create({ title: 'First post', body: 'post body', user_id: 1 })
119
-
120
- post_form = described_class.new(params[:post], post)
121
-
122
- post_form.save.should == true
123
-
124
- Post.all.count.should == 1
125
- post = Post.first
126
- post.title.should == 'altered title'
127
- post.body.should == 'post body'
128
- post.user_id.should == 666
129
-
130
- post_form.respond_to?(:title).should == false
131
- post_form.respond_to?(:user_id).should == false
132
- end
133
-
134
- end
135
-
136
- describe PostForm::Setters1 do
137
- it_behaves_like 'a PostForm with only delegated setters'
138
- end
139
-
140
- describe PostForm::Setters2 do
141
- it_behaves_like 'a PostForm with only delegated setters'
142
- end
@@ -1,63 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe 'Testing the #reset_nested_objects method' do
4
-
5
- it "1. Creating 'Comments' from 'PostWithCommentForm::Base', adding another, manually and expecting the form_object to not knowing it.", current: true do
6
- params = {
7
- post: {
8
- title: 'new title',
9
- comments_attributes: {
10
- "0" => { body: "body1" },
11
- "1" => { body: "1" }
12
- }
13
- }
14
- }
15
-
16
- post_form = PostWithCommentForm::Base.new(params[:post])
17
-
18
- post_form.save.should == true
19
-
20
- Post.all.count.should == 1
21
- Comment.all.count.should == 2
22
-
23
- post_form.comments.length.should == 2
24
-
25
- post = Post.first
26
- post.comments.create(body: 'new_comment')
27
-
28
- post_form.post.reload
29
- post_form.post.comments.length.should == 3
30
- post_form.comments.length.should == 2
31
- end
32
-
33
- it "1. Creating 'Comments' from 'PostWithCommentForm::Base', adding another, manually and expecting the form_object to know about it.", current: true do
34
- params = {
35
- post: {
36
- title: 'new title',
37
- comments_attributes: {
38
- "0" => { body: "body1" },
39
- "1" => { body: "1" }
40
- }
41
- }
42
- }
43
-
44
- post_form = PostWithCommentForm::Base.new(params[:post])
45
-
46
- post_form.save.should == true
47
-
48
- Post.all.count.should == 1
49
- Comment.all.count.should == 2
50
-
51
- post_form.comments.length.should == 2
52
-
53
- post = Post.first
54
- post.comments.create(body: 'new_comment')
55
-
56
- post_form.post.reload
57
- post_form.post.comments.length.should == 3
58
-
59
- post_form.reset_nested_objects(:comments)
60
- post_form.comments.length.should == 3
61
- end
62
-
63
- end