active_remote 1.2.1 → 1.3.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.
- data/.gitignore +1 -0
- data/Gemfile +0 -2
- data/LICENSE +1 -1
- data/active_remote.gemspec +0 -1
- data/lib/active_remote/association.rb +22 -39
- data/lib/active_remote/version.rb +1 -1
- data/spec/lib/active_remote/association_spec.rb +48 -14
- data/spec/support/models/author.rb +1 -0
- data/spec/support/models/post.rb +2 -0
- metadata +4 -21
- data/.rvmrc +0 -1
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/LICENSE
CHANGED
data/active_remote.gemspec
CHANGED
@@ -37,20 +37,9 @@ module ActiveRemote
|
|
37
37
|
# end
|
38
38
|
# end
|
39
39
|
#
|
40
|
-
def belongs_to(
|
41
|
-
|
42
|
-
|
43
|
-
define_method(klass_name) do
|
44
|
-
value = instance_variable_get(:"@#{klass_name}")
|
45
|
-
|
46
|
-
unless value
|
47
|
-
klass = klass_name.to_s.classify.constantize
|
48
|
-
value = klass.search(:guid => read_attribute(:"#{klass_name}_guid")).first
|
49
|
-
instance_variable_set(:"@#{klass_name}", value)
|
50
|
-
end
|
51
|
-
|
52
|
-
return value
|
53
|
-
end
|
40
|
+
def belongs_to(belongs_to_klass, options={})
|
41
|
+
perform_association( belongs_to_klass, options ) do |klass, obj|
|
42
|
+
klass.search(:guid => obj.read_attribute(:"#{belongs_to_klass}_guid")).first
|
54
43
|
end
|
55
44
|
end
|
56
45
|
|
@@ -84,21 +73,9 @@ module ActiveRemote
|
|
84
73
|
# end
|
85
74
|
# end
|
86
75
|
#
|
87
|
-
def has_many(
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
define_method(plural_klass_name) do
|
92
|
-
values = instance_variable_get(:"@#{plural_klass_name}")
|
93
|
-
|
94
|
-
unless values
|
95
|
-
klass = plural_klass_name.to_s.classify.constantize
|
96
|
-
values = klass.search(:"#{self.class.name.demodulize.underscore}_guid" => self.guid)
|
97
|
-
instance_variable_set(:"@#{plural_klass_name}", values)
|
98
|
-
end
|
99
|
-
|
100
|
-
return values
|
101
|
-
end
|
76
|
+
def has_many(has_many_class, options={})
|
77
|
+
perform_association( has_many_class, options ) do |klass, obj|
|
78
|
+
klass.search(:"#{obj.class.name.demodulize.underscore}_guid" => obj.guid)
|
102
79
|
end
|
103
80
|
end
|
104
81
|
|
@@ -131,20 +108,26 @@ module ActiveRemote
|
|
131
108
|
# end
|
132
109
|
# end
|
133
110
|
#
|
134
|
-
def has_one(
|
135
|
-
|
111
|
+
def has_one(has_one_klass, options={})
|
112
|
+
perform_association( has_one_klass, options ) do |klass, obj|
|
113
|
+
klass.search(:"#{obj.class.name.demodulize.underscore}_guid" => obj.guid).first
|
114
|
+
end
|
115
|
+
end
|
136
116
|
|
137
|
-
|
138
|
-
value = instance_variable_get(:"@#{klass_name}")
|
117
|
+
private
|
139
118
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
instance_variable_set(:"@#{klass_name}", value)
|
144
|
-
end
|
119
|
+
def perform_association(associated_klass, optionz={})
|
120
|
+
define_method(associated_klass) do
|
121
|
+
value = instance_variable_get(:"@#{associated_klass}")
|
145
122
|
|
146
|
-
|
123
|
+
unless value
|
124
|
+
klass_name = optionz.fetch(:class_name){ associated_klass }
|
125
|
+
klass = klass_name.to_s.classify.constantize
|
126
|
+
value = yield( klass, self )
|
127
|
+
instance_variable_set(:"@#{associated_klass}", value)
|
147
128
|
end
|
129
|
+
|
130
|
+
return value
|
148
131
|
end
|
149
132
|
end
|
150
133
|
end
|
@@ -5,27 +5,43 @@ describe ActiveRemote::Association do
|
|
5
5
|
let(:records) { [ record ] }
|
6
6
|
|
7
7
|
describe '.belongs_to' do
|
8
|
-
let(:author_guid) { 'AUT-123' }
|
9
8
|
|
10
|
-
|
11
|
-
|
9
|
+
context 'simple association' do
|
10
|
+
let(:author_guid) { 'AUT-123' }
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
subject.author.should eq record
|
16
|
-
end
|
12
|
+
subject { Post.new(:author_guid => author_guid) }
|
13
|
+
it { should respond_to(:author) }
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
it 'searches the associated model for a single record' do
|
16
|
+
Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records)
|
17
|
+
subject.author.should eq record
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'memorizes the result record' do
|
21
|
+
Author.should_receive(:search).once.with(:guid => subject.author_guid).and_return(records)
|
22
|
+
3.times { subject.author.should eq record }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when the search is empty' do
|
26
|
+
it 'returns a nil' do
|
27
|
+
Author.should_receive(:search).with(:guid => subject.author_guid).and_return([])
|
28
|
+
subject.author.should be_nil
|
29
|
+
end
|
30
|
+
end
|
21
31
|
end
|
22
32
|
|
23
|
-
context '
|
24
|
-
|
25
|
-
|
26
|
-
|
33
|
+
context 'specific association with class name' do
|
34
|
+
let(:author_guid) { 'AUT-456' }
|
35
|
+
|
36
|
+
subject { Post.new(:author_guid => author_guid) }
|
37
|
+
it { should respond_to(:coauthor) }
|
38
|
+
|
39
|
+
it 'searches the associated model for a single record' do
|
40
|
+
Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records)
|
41
|
+
subject.author.should eq record
|
27
42
|
end
|
28
43
|
end
|
44
|
+
|
29
45
|
end
|
30
46
|
|
31
47
|
describe '.has_many' do
|
@@ -51,6 +67,15 @@ describe ActiveRemote::Association do
|
|
51
67
|
subject.posts.should be_empty
|
52
68
|
end
|
53
69
|
end
|
70
|
+
|
71
|
+
context 'specific association with class name' do
|
72
|
+
it { should respond_to(:flagged_posts) }
|
73
|
+
|
74
|
+
it 'searches the associated model for a single record' do
|
75
|
+
Post.should_receive(:search).with(:author_guid => subject.guid).and_return([])
|
76
|
+
subject.flagged_posts.should be_empty
|
77
|
+
end
|
78
|
+
end
|
54
79
|
end
|
55
80
|
|
56
81
|
describe '.has_one' do
|
@@ -75,6 +100,15 @@ describe ActiveRemote::Association do
|
|
75
100
|
subject.category.should be_nil
|
76
101
|
end
|
77
102
|
end
|
103
|
+
|
104
|
+
context 'specific association with class name' do
|
105
|
+
it { should respond_to(:main_category) }
|
106
|
+
|
107
|
+
it 'searches the associated model for a single record' do
|
108
|
+
Category.should_receive(:search).with(:post_guid => subject.guid).and_return(records)
|
109
|
+
subject.main_category.should eq record
|
110
|
+
end
|
111
|
+
end
|
78
112
|
end
|
79
113
|
|
80
114
|
end
|
data/spec/support/models/post.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_remote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: activemodel
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
name: active_attr
|
32
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -166,7 +150,6 @@ extra_rdoc_files: []
|
|
166
150
|
files:
|
167
151
|
- .gitignore
|
168
152
|
- .rspec
|
169
|
-
- .rvmrc
|
170
153
|
- Gemfile
|
171
154
|
- LICENSE
|
172
155
|
- README.md
|
@@ -235,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
235
218
|
version: '0'
|
236
219
|
segments:
|
237
220
|
- 0
|
238
|
-
hash:
|
221
|
+
hash: 1289251465950014823
|
239
222
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
240
223
|
none: false
|
241
224
|
requirements:
|
@@ -244,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
244
227
|
version: '0'
|
245
228
|
segments:
|
246
229
|
- 0
|
247
|
-
hash:
|
230
|
+
hash: 1289251465950014823
|
248
231
|
requirements: []
|
249
232
|
rubyforge_project:
|
250
233
|
rubygems_version: 1.8.24
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use 1.9.3@active_remote --create
|