object_attorney 2.8.1 → 2.9.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 +8 -8
- data/lib/object_attorney/delegation.rb +9 -2
- data/lib/object_attorney/exposed_data.rb +9 -17
- data/lib/object_attorney/representation.rb +2 -0
- data/lib/object_attorney/version.rb +1 -1
- data/spec/object_attorney/delegation_spec.rb +37 -1
- data/spec/object_attorney/exposed_data_spec.rb +2 -3
- data/spec/spec_helper.rb +0 -1
- data/spec/support/form_objects/post_form.rb +8 -2
- metadata +2 -6
- data/spec/object_attorney/user_form_with_ruby_errors_spec.rb +0 -15
- data/spec/support/form_objects/user_form_with_ruby_errors.rb +0 -15
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTk2ZjBiODdlOGJlOGRjZDRkYTZkMjViZjllOThhYjgxMTBjYzYyMQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjdhYzlhNjJiZjYyZTkzZWI3MjkzMjViYzVjZGRkNDVmYTkxN2JlMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NWE0YTk0N2FhNDZjOWJiYmU5ZDE0Mjg0ZWQwZWZkNDQ1ZTE5ZWQyMDRhM2E0
|
10
|
+
M2VhMWM2ODU2ZjI2ZWY3MTkxOWE3OWViOGNiM2ViNmQ4YTY5OTYyNjBmNDRj
|
11
|
+
NTQzYmNhMTcxOGY2YjZlNDc4ZDI5M2U1N2JiNTQwNmRhNmMwMDM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTNlZDEzMTUwMmViMjE4OTY3YzU3MjBjYTliM2Q1N2VjM2I3MTVmMzNiN2Fj
|
14
|
+
NTQwZDY2N2NiMjcwYmJlMDY1Y2JhNWJlOTk2ZDIxOThiYjY3MzIzZjJiMWQy
|
15
|
+
ZGJmZGVjNmVlMWYxMTkxNTUyMGY2YjhhN2M5M2IwYTU5NTBmMGE=
|
@@ -2,6 +2,14 @@ module ObjectAttorney
|
|
2
2
|
|
3
3
|
module Delegation
|
4
4
|
|
5
|
+
module MissingMethods
|
6
|
+
|
7
|
+
def method_missing(method, *args, &block)
|
8
|
+
represented_object.send(method, *args, &block)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
5
13
|
def zuper_method(method_name, *args)
|
6
14
|
self.superclass.send(method_name, *args) if self.superclass.respond_to?(method_name)
|
7
15
|
end
|
@@ -28,12 +36,11 @@ module ObjectAttorney
|
|
28
36
|
|
29
37
|
def delegate_getter(getter)
|
30
38
|
delegate getter, to: :represented_object
|
31
|
-
|
39
|
+
add_exposed_data getter
|
32
40
|
end
|
33
41
|
|
34
42
|
def delegate_setter(setter)
|
35
43
|
delegate "#{setter}=", to: :represented_object
|
36
|
-
add_exposed_setters(setter)
|
37
44
|
end
|
38
45
|
|
39
46
|
end
|
@@ -2,15 +2,15 @@ module ObjectAttorney
|
|
2
2
|
|
3
3
|
module ExposedData
|
4
4
|
|
5
|
-
def
|
6
|
-
self.class.
|
5
|
+
def to_hash
|
6
|
+
self.class.exposed_data.reduce({}) do |data, getter|
|
7
7
|
data[getter] = send(getter)
|
8
8
|
data
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_json(options = {})
|
13
|
-
|
13
|
+
to_hash.to_json
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.included(base)
|
@@ -19,24 +19,16 @@ module ObjectAttorney
|
|
19
19
|
|
20
20
|
module ClassMethods
|
21
21
|
|
22
|
-
def
|
23
|
-
return @
|
22
|
+
def exposed_data
|
23
|
+
return @exposed_data if defined?(@exposed_data)
|
24
24
|
|
25
|
-
@
|
25
|
+
@exposed_data = zuper_method(:exposed_data)
|
26
26
|
|
27
|
-
@
|
27
|
+
@exposed_data ||= represented_object_class.present? && represented_object_class.method_defined?(:id) ? [:id] : []
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
def exposed_setters
|
35
|
-
@exposed_setters ||= (zuper_method(:exposed_setters) || [])
|
36
|
-
end
|
37
|
-
|
38
|
-
def add_exposed_setters(*setters)
|
39
|
-
exposed_setters.push(*setters) unless exposed_setters.include?(setters)
|
30
|
+
def add_exposed_data(*getters)
|
31
|
+
exposed_data.push(*getters) unless exposed_data.include?(getters)
|
40
32
|
end
|
41
33
|
|
42
34
|
end
|
@@ -62,6 +62,8 @@ module ObjectAttorney
|
|
62
62
|
properties(*options[:properties]) if options.include?(:properties)
|
63
63
|
getters(*options[:getters]) if options.include?(:getters)
|
64
64
|
setters(*options[:setters]) if options.include?(:setters)
|
65
|
+
|
66
|
+
class_eval { include Delegation::MissingMethods } if options[:delegate_missing_methods]
|
65
67
|
end
|
66
68
|
|
67
69
|
def represented_object_reflection
|
@@ -1,5 +1,39 @@
|
|
1
1
|
require "spec_helper"
|
2
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", current: true 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
|
+
|
3
37
|
shared_examples "a PostForm with delegated properties" do
|
4
38
|
|
5
39
|
it "1. Creating a 'Post' with 3 params, but only 2 are delegated" do
|
@@ -38,7 +72,9 @@ shared_examples "a PostForm with only delegated getters" do
|
|
38
72
|
it "1. Editing a 'Post' with 3 params, no changes should take place but the getters should" do
|
39
73
|
params = {
|
40
74
|
post: {
|
41
|
-
title: 'altered title',
|
75
|
+
title: 'altered title',
|
76
|
+
body: 'altered body',
|
77
|
+
user_id: 666
|
42
78
|
}
|
43
79
|
}
|
44
80
|
|
@@ -14,8 +14,7 @@ describe PostForm::GrandSon do
|
|
14
14
|
post_form = described_class.new(params[:post], post)
|
15
15
|
|
16
16
|
post_form.save.should == true
|
17
|
-
described_class.
|
18
|
-
described_class.exposed_setters.should == [:title, :user_id]
|
17
|
+
described_class.exposed_data.should == [:id, :title, :email, :author, :body, :date]
|
19
18
|
|
20
19
|
Post.all.count.should == 1
|
21
20
|
post = Post.first
|
@@ -35,7 +34,7 @@ describe PostForm::GrandSon do
|
|
35
34
|
|
36
35
|
data = { id:1, title: "altered title", email: "test@gmail.com", author: "test", body: "post body", date: "20-10-2010" }
|
37
36
|
|
38
|
-
post_form.
|
37
|
+
post_form.to_hash.should == data
|
39
38
|
post_form.to_json.should == data.to_json
|
40
39
|
end
|
41
40
|
|
data/spec/spec_helper.rb
CHANGED
@@ -30,7 +30,6 @@ require 'support/form_objects/bulk_posts_allow_only_existing_form'
|
|
30
30
|
require 'support/form_objects/bulk_posts_allow_only_new_form'
|
31
31
|
require 'support/form_objects/bulk_posts_with_form_objects_form'
|
32
32
|
require 'support/form_objects/user_form'
|
33
|
-
require 'support/form_objects/user_form_with_ruby_errors'
|
34
33
|
|
35
34
|
RSpec.configure do |config|
|
36
35
|
#config.treat_symbols_as_metadata_keys_with_true_values = true
|
@@ -50,6 +50,12 @@ module PostForm
|
|
50
50
|
end
|
51
51
|
|
52
52
|
|
53
|
+
class Presenter
|
54
|
+
include ObjectAttorney
|
55
|
+
represents :post, delegate_missing_methods: true, properties: [:title]
|
56
|
+
end
|
57
|
+
|
58
|
+
|
53
59
|
class Properties1
|
54
60
|
include ObjectAttorney
|
55
61
|
represents :post
|
@@ -94,7 +100,7 @@ module PostForm
|
|
94
100
|
class Father < GrandFather
|
95
101
|
properties :title
|
96
102
|
|
97
|
-
|
103
|
+
add_exposed_data :email, :author
|
98
104
|
|
99
105
|
attr_accessor :email, :author
|
100
106
|
end
|
@@ -106,7 +112,7 @@ module PostForm
|
|
106
112
|
class GrandSon < Son
|
107
113
|
setters :user_id
|
108
114
|
|
109
|
-
|
115
|
+
add_exposed_data :date
|
110
116
|
|
111
117
|
attr_accessor :date
|
112
118
|
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: 2.
|
4
|
+
version: 2.9.0
|
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: 2014-02-
|
11
|
+
date: 2014-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -103,7 +103,6 @@ files:
|
|
103
103
|
- spec/object_attorney/post_with_only_existing_comments_form_spec.rb
|
104
104
|
- spec/object_attorney/post_with_only_new_comments_form_spec.rb
|
105
105
|
- spec/object_attorney/user_form_spec.rb
|
106
|
-
- spec/object_attorney/user_form_with_ruby_errors_spec.rb
|
107
106
|
- spec/spec_helper.rb
|
108
107
|
- spec/support/active_model/validations.rb
|
109
108
|
- spec/support/database_setup.rb
|
@@ -120,7 +119,6 @@ files:
|
|
120
119
|
- spec/support/form_objects/post_with_only_existing_comments_form.rb
|
121
120
|
- spec/support/form_objects/post_with_only_new_comments_form.rb
|
122
121
|
- spec/support/form_objects/user_form.rb
|
123
|
-
- spec/support/form_objects/user_form_with_ruby_errors.rb
|
124
122
|
- spec/support/models/address.rb
|
125
123
|
- spec/support/models/comment.rb
|
126
124
|
- spec/support/models/post.rb
|
@@ -166,7 +164,6 @@ test_files:
|
|
166
164
|
- spec/object_attorney/post_with_only_existing_comments_form_spec.rb
|
167
165
|
- spec/object_attorney/post_with_only_new_comments_form_spec.rb
|
168
166
|
- spec/object_attorney/user_form_spec.rb
|
169
|
-
- spec/object_attorney/user_form_with_ruby_errors_spec.rb
|
170
167
|
- spec/spec_helper.rb
|
171
168
|
- spec/support/active_model/validations.rb
|
172
169
|
- spec/support/database_setup.rb
|
@@ -183,7 +180,6 @@ test_files:
|
|
183
180
|
- spec/support/form_objects/post_with_only_existing_comments_form.rb
|
184
181
|
- spec/support/form_objects/post_with_only_new_comments_form.rb
|
185
182
|
- spec/support/form_objects/user_form.rb
|
186
|
-
- spec/support/form_objects/user_form_with_ruby_errors.rb
|
187
183
|
- spec/support/models/address.rb
|
188
184
|
- spec/support/models/comment.rb
|
189
185
|
- spec/support/models/post.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe UserFormWithRubyErrors do
|
4
|
-
|
5
|
-
xit "1. 'UserFormWithRubyErrors' when saving should through an error!" do
|
6
|
-
params = { user: { email: 'email@gmail.com', terms_of_service: true } }
|
7
|
-
|
8
|
-
user_form = UserFormWithRubyErrors.new(params[:user])
|
9
|
-
save_result = user_form.save
|
10
|
-
|
11
|
-
save_result.should == true
|
12
|
-
User.all.count.should == 1
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
class UserFormWithRubyErrors
|
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
|
-
def submit
|
12
|
-
fake_var == true
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|