object_attorney 2.6.12 → 2.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/object_attorney.rb +2 -0
- data/lib/object_attorney/delegation.rb +26 -4
- data/lib/object_attorney/exposed_data.rb +42 -0
- data/lib/object_attorney/representation.rb +3 -22
- data/lib/object_attorney/version.rb +1 -1
- data/spec/object_attorney/delegation_spec.rb +112 -0
- data/spec/object_attorney/exposed_data_spec.rb +42 -0
- data/spec/support/form_objects/comment_form.rb +1 -1
- data/spec/support/form_objects/post_form.rb +63 -3
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzU5MTQzODQ3Y2NjMTU0MWVkMzRmOGRmOTMwMTZmZjA4Y2Y1MmQxNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZWRlYWQ3MjdkNmU0YjFjZjM4YzQ3MjAyODhlMzg4MGMwMmEzNDRkZQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDBlNjIzZjBjZTIyZTQ2YzhiNTdhM2Y2OTk3ODNiYmEwZDlmNmNlNDZmMmM1
|
10
|
+
NjA2YmY5MTk1ODYyNzk0Y2M3ZTFhNGJjNGY3YjYzMGE1NmViNTVkNWEwMzIw
|
11
|
+
MTZjMTA1ZWE5N2ZlZjE2MTU2NjJkYjYxMDIzOTcyZmZkNTZiNTA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MjhjZDA5N2Q1MzllMTBjMGExOWI2ODQyNzM2ZjEyY2FlOWZiMTY4YmEyNmRl
|
14
|
+
NzJlMjRhYzZiNDAzOGY1MTEzNDg5YWFjMGIxNDAzMjRlMDBhMTFlZDEyNzk5
|
15
|
+
N2EwMTlkYzdkNDM2NTAzMjY3OTIxZTM5OGE3YjY3NDcxZmEzMTg=
|
data/lib/object_attorney.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
|
2
2
|
require "object_attorney/attribute_assignment"
|
3
3
|
require "object_attorney/delegation"
|
4
|
+
require "object_attorney/exposed_data"
|
4
5
|
require "object_attorney/helpers"
|
5
6
|
require "object_attorney/naming"
|
6
7
|
require "object_attorney/reflection"
|
@@ -48,6 +49,7 @@ module ObjectAttorney
|
|
48
49
|
include NestedObjects
|
49
50
|
include Record
|
50
51
|
include Representation
|
52
|
+
include ExposedData
|
51
53
|
|
52
54
|
validate :validate_represented_object
|
53
55
|
end
|
@@ -6,12 +6,34 @@ module ObjectAttorney
|
|
6
6
|
self.superclass.send(method_name, *args) if self.superclass.respond_to?(method_name)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def properties(*_properties)
|
10
|
+
_properties.each { |property| delegate_property(property) }
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
|
13
|
+
def getters(*_getters)
|
14
|
+
_getters.each { |getter| delegate_getter(getter) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def setters(*_setters)
|
18
|
+
_setters.each { |getter| delegate_setter(getter) }
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
protected ##################### PROTECTED #####################
|
23
|
+
|
24
|
+
def delegate_property(property)
|
25
|
+
delegate_getter(property)
|
26
|
+
delegate_setter(property)
|
27
|
+
end
|
28
|
+
|
29
|
+
def delegate_getter(getter)
|
30
|
+
delegate getter, to: :represented_object
|
31
|
+
add_exposed_getters(getter)
|
32
|
+
end
|
33
|
+
|
34
|
+
def delegate_setter(setter)
|
35
|
+
delegate "#{setter}=", to: :represented_object
|
36
|
+
add_exposed_setters(setter)
|
15
37
|
end
|
16
38
|
|
17
39
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ObjectAttorney
|
2
|
+
|
3
|
+
module ExposedData
|
4
|
+
|
5
|
+
def exposed_data
|
6
|
+
self.class.exposed_getters.reduce({}) do |data, getter|
|
7
|
+
data[getter] = send(getter)
|
8
|
+
data
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_json(options = {})
|
13
|
+
exposed_data.to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.included(base)
|
17
|
+
base.extend(ClassMethods)
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
|
22
|
+
def exposed_getters
|
23
|
+
@exposed_getters ||= (zuper_method(:exposed_getters) || [])
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_exposed_getters(*getters)
|
27
|
+
exposed_getters.push(*getters) unless exposed_getters.include?(getters)
|
28
|
+
end
|
29
|
+
|
30
|
+
def exposed_setters
|
31
|
+
@exposed_setters ||= (zuper_method(:exposed_setters) || [])
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_exposed_setters(*setters)
|
35
|
+
exposed_setters.push(*setters) unless exposed_setters.include?(setters)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -59,13 +59,9 @@ module ObjectAttorney
|
|
59
59
|
|
60
60
|
define_method(represented_object_name) { represented_object }
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
initiate_getters(options[:getters], represented_object_name)
|
66
|
-
|
67
|
-
initiate_setters(options[:setter], represented_object_name)
|
68
|
-
initiate_setters(options[:setters], represented_object_name)
|
62
|
+
properties(*options[:properties]) if options.include?(:properties)
|
63
|
+
getters(*options[:getters]) if options.include?(:getters)
|
64
|
+
setters(*options[:setters]) if options.include?(:setters)
|
69
65
|
end
|
70
66
|
|
71
67
|
def represented_object_reflection
|
@@ -82,21 +78,6 @@ module ObjectAttorney
|
|
82
78
|
represented_object_class.reflect_on_association(association)
|
83
79
|
end
|
84
80
|
|
85
|
-
|
86
|
-
private ############### PRIVATE #################
|
87
|
-
|
88
|
-
def initiate_getters(getters, represented_object_name)
|
89
|
-
delegate(*getters, to: represented_object_name) unless getters.nil?
|
90
|
-
end
|
91
|
-
|
92
|
-
def initiate_setters(setters, represented_object_name)
|
93
|
-
return nil if setters.nil?
|
94
|
-
|
95
|
-
setters = [*setters].map { |setter| "#{setter}=" }
|
96
|
-
|
97
|
-
delegate(*setters, to: represented_object_name)
|
98
|
-
end
|
99
|
-
|
100
81
|
end
|
101
82
|
|
102
83
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
shared_examples "a PostForm with delegated properties" do
|
4
|
+
|
5
|
+
it "1. Creating a 'Post' with 3 params, but only 2 are delegated" do
|
6
|
+
params = {
|
7
|
+
post: {
|
8
|
+
title: 'First post',
|
9
|
+
body: 'post body',
|
10
|
+
user_id: 666
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
post_form = described_class.new(params[:post])
|
15
|
+
|
16
|
+
post_form.save.should == true
|
17
|
+
described_class.exposed_getters.should == [:title, :user_id]
|
18
|
+
described_class.exposed_setters.should == [:title, :user_id]
|
19
|
+
|
20
|
+
Post.all.count.should == 1
|
21
|
+
post = Post.first
|
22
|
+
post.title.should == 'First post'
|
23
|
+
post.body.should == nil
|
24
|
+
post.user_id.should == 666
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe PostForm::Properties1 do
|
30
|
+
it_behaves_like 'a PostForm with delegated properties'
|
31
|
+
end
|
32
|
+
|
33
|
+
describe PostForm::Properties2 do
|
34
|
+
it_behaves_like 'a PostForm with delegated properties'
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
shared_examples "a PostForm with only delegated getters" do
|
39
|
+
|
40
|
+
it "1. Editing a 'Post' with 3 params, no changes should take place but the getters should" do
|
41
|
+
params = {
|
42
|
+
post: {
|
43
|
+
title: 'altered title', body: 'altered body', user_id: 666
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
post = Post.create({ title: 'First post', body: 'post body', user_id: 1 })
|
48
|
+
|
49
|
+
post_form = described_class.new(params[:post], post)
|
50
|
+
|
51
|
+
post_form.save.should == true
|
52
|
+
described_class.exposed_getters.should == [:title, :user_id]
|
53
|
+
described_class.exposed_setters.should == []
|
54
|
+
|
55
|
+
Post.all.count.should == 1
|
56
|
+
post = Post.first
|
57
|
+
post.title.should == 'First post'
|
58
|
+
post.body.should == 'post body'
|
59
|
+
post.user_id.should == 1
|
60
|
+
|
61
|
+
post_form.title.should == 'First post'
|
62
|
+
post_form.user_id.should == 1
|
63
|
+
post_form.respond_to?(:body).should == false
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
describe PostForm::Getters1 do
|
69
|
+
it_behaves_like 'a PostForm with only delegated getters'
|
70
|
+
end
|
71
|
+
|
72
|
+
describe PostForm::Getters2 do
|
73
|
+
it_behaves_like 'a PostForm with only delegated getters'
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
shared_examples "a PostForm with only delegated setters" do
|
78
|
+
|
79
|
+
it "1. Editing a 'Post' with 3 params, changes should take place and the getters should not work" do
|
80
|
+
params = {
|
81
|
+
post: {
|
82
|
+
title: 'altered title', body: 'altered body', user_id: 666
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
post = Post.create({ title: 'First post', body: 'post body', user_id: 1 })
|
87
|
+
|
88
|
+
post_form = described_class.new(params[:post], post)
|
89
|
+
|
90
|
+
post_form.save.should == true
|
91
|
+
described_class.exposed_getters.should == []
|
92
|
+
described_class.exposed_setters.should == [:title, :user_id]
|
93
|
+
|
94
|
+
Post.all.count.should == 1
|
95
|
+
post = Post.first
|
96
|
+
post.title.should == 'altered title'
|
97
|
+
post.body.should == 'post body'
|
98
|
+
post.user_id.should == 666
|
99
|
+
|
100
|
+
post_form.respond_to?(:title).should == false
|
101
|
+
post_form.respond_to?(:user_id).should == false
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
describe PostForm::Setters1 do
|
107
|
+
it_behaves_like 'a PostForm with only delegated setters'
|
108
|
+
end
|
109
|
+
|
110
|
+
describe PostForm::Setters2 do
|
111
|
+
it_behaves_like 'a PostForm with only delegated setters'
|
112
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostForm::GrandSon do
|
4
|
+
|
5
|
+
it "1. Editing a 'Post' with 6 params, only 2 should take place, and only 2 getters should be active" do
|
6
|
+
params = {
|
7
|
+
post: {
|
8
|
+
title: 'altered title', body: 'altered body', user_id: 666, author: 'test', email: 'test@gmail.com', date: '20-10-2010'
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
post = Post.create({ title: 'First post', body: 'post body', user_id: 1 })
|
13
|
+
|
14
|
+
post_form = described_class.new(params[:post], post)
|
15
|
+
|
16
|
+
post_form.save.should == true
|
17
|
+
described_class.exposed_getters.should == [:title, :email, :author, :body, :date]
|
18
|
+
described_class.exposed_setters.should == [:title, :user_id]
|
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 == 666
|
25
|
+
post.respond_to?(:author).should == false
|
26
|
+
post.respond_to?(:email).should == false
|
27
|
+
post.respond_to?(:date).should == false
|
28
|
+
|
29
|
+
post_form.title.should == 'altered title'
|
30
|
+
post_form.body.should == 'post body'
|
31
|
+
post_form.author.should == 'test'
|
32
|
+
post_form.email.should == 'test@gmail.com'
|
33
|
+
post_form.date.should == '20-10-2010'
|
34
|
+
post_form.respond_to?(:user_id).should == false
|
35
|
+
|
36
|
+
data = { title: "altered title", email: "test@gmail.com", author: "test", body: "post body", date: "20-10-2010" }
|
37
|
+
|
38
|
+
post_form.exposed_data.should == data
|
39
|
+
post_form.to_json.should == data.to_json
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -4,9 +4,7 @@ module PostForm
|
|
4
4
|
|
5
5
|
include ObjectAttorney
|
6
6
|
|
7
|
-
represents :post
|
8
|
-
|
9
|
-
delegate_properties :title, :body, to: :post
|
7
|
+
represents :post, properties: [:title, :body]
|
10
8
|
|
11
9
|
has_many :comments
|
12
10
|
|
@@ -51,4 +49,66 @@ module PostForm
|
|
51
49
|
|
52
50
|
end
|
53
51
|
|
52
|
+
|
53
|
+
class Properties1
|
54
|
+
include ObjectAttorney
|
55
|
+
represents :post
|
56
|
+
properties :title, :user_id
|
57
|
+
end
|
58
|
+
|
59
|
+
class Properties2
|
60
|
+
include ObjectAttorney
|
61
|
+
represents :post, properties: [:title, :user_id]
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
class Getters1
|
66
|
+
include ObjectAttorney
|
67
|
+
represents :post
|
68
|
+
getters :title, :user_id
|
69
|
+
end
|
70
|
+
|
71
|
+
class Getters2
|
72
|
+
include ObjectAttorney
|
73
|
+
represents :post, getters: [:title, :user_id]
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
class Setters1
|
78
|
+
include ObjectAttorney
|
79
|
+
represents :post
|
80
|
+
setters :title, :user_id
|
81
|
+
end
|
82
|
+
|
83
|
+
class Setters2
|
84
|
+
include ObjectAttorney
|
85
|
+
represents :post, setters: [:title, :user_id]
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
class GrandFather
|
90
|
+
include ObjectAttorney
|
91
|
+
represents :post
|
92
|
+
end
|
93
|
+
|
94
|
+
class Father < GrandFather
|
95
|
+
properties :title
|
96
|
+
|
97
|
+
add_exposed_getters :email, :author
|
98
|
+
|
99
|
+
attr_accessor :email, :author
|
100
|
+
end
|
101
|
+
|
102
|
+
class Son < Father
|
103
|
+
getters :body
|
104
|
+
end
|
105
|
+
|
106
|
+
class GrandSon < Son
|
107
|
+
setters :user_id
|
108
|
+
|
109
|
+
add_exposed_getters :date
|
110
|
+
|
111
|
+
attr_accessor :date
|
112
|
+
end
|
113
|
+
|
54
114
|
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.8.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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/object_attorney/association_reflection.rb
|
77
77
|
- lib/object_attorney/attribute_assignment.rb
|
78
78
|
- lib/object_attorney/delegation.rb
|
79
|
+
- lib/object_attorney/exposed_data.rb
|
79
80
|
- lib/object_attorney/helpers.rb
|
80
81
|
- lib/object_attorney/naming.rb
|
81
82
|
- lib/object_attorney/nested_objects.rb
|
@@ -92,6 +93,8 @@ files:
|
|
92
93
|
- spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb
|
93
94
|
- spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
|
94
95
|
- spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb
|
96
|
+
- spec/object_attorney/delegation_spec.rb
|
97
|
+
- spec/object_attorney/exposed_data_spec.rb
|
95
98
|
- spec/object_attorney/post_form_spec.rb
|
96
99
|
- spec/object_attorney/post_validations_form_spec.rb
|
97
100
|
- spec/object_attorney/post_with_comment_form_spec.rb
|
@@ -153,6 +156,8 @@ test_files:
|
|
153
156
|
- spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb
|
154
157
|
- spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
|
155
158
|
- spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb
|
159
|
+
- spec/object_attorney/delegation_spec.rb
|
160
|
+
- spec/object_attorney/exposed_data_spec.rb
|
156
161
|
- spec/object_attorney/post_form_spec.rb
|
157
162
|
- spec/object_attorney/post_validations_form_spec.rb
|
158
163
|
- spec/object_attorney/post_with_comment_form_spec.rb
|