remote_association 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/remote_association/active_record/relation.rb +5 -4
- data/lib/remote_association/belongs_to_remote.rb +3 -4
- data/lib/remote_association/has_many_remote.rb +33 -9
- data/lib/remote_association/has_one_remote.rb +3 -2
- data/lib/remote_association/version.rb +1 -1
- data/lib/remote_association.rb +6 -4
- data/spec/remote_association/belongs_to_remote_spec.rb +40 -3
- data/spec/remote_association/has_many_remote_spec.rb +43 -3
- data/spec/remote_association/has_one_remote_spec.rb +3 -3
- metadata +4 -4
data/Gemfile.lock
CHANGED
@@ -103,9 +103,10 @@ module ActiveRecord
|
|
103
103
|
keys = @records.uniq.map(&:id)
|
104
104
|
|
105
105
|
remote_objects = fetch_remote_objects(settings.ar_class, keys, settings.ar_accessor)
|
106
|
+
join_key = klass.primary_key
|
106
107
|
|
107
108
|
@records.each do |record|
|
108
|
-
record.send("#{settings.ar_accessor}=", remote_objects.select { |s| s.send(settings.foreign_key) == record.
|
109
|
+
record.send("#{settings.ar_accessor}=", remote_objects.select { |s| s.send(settings.foreign_key) == record.send(join_key) })
|
109
110
|
end
|
110
111
|
end
|
111
112
|
|
@@ -115,9 +116,10 @@ module ActiveRecord
|
|
115
116
|
return if keys.empty?
|
116
117
|
|
117
118
|
remote_objects = fetch_remote_objects(settings.ar_class, keys, settings.ar_accessor)
|
119
|
+
join_key = settings.ar_class.primary_key
|
118
120
|
|
119
121
|
@records.each do |record|
|
120
|
-
record.send("#{settings.ar_accessor}=", remote_objects.select { |s| record.send(settings.foreign_key) == s.
|
122
|
+
record.send("#{settings.ar_accessor}=", remote_objects.select { |s| record.send(settings.foreign_key) == s.send(join_key) })
|
121
123
|
end
|
122
124
|
end
|
123
125
|
|
@@ -129,13 +131,12 @@ module ActiveRecord
|
|
129
131
|
end
|
130
132
|
|
131
133
|
def fetch_remote_objects(ar_class, keys, ar_accessor)
|
132
|
-
params = klass.
|
134
|
+
params = klass.send(:"build_params_hash_for_#{ar_accessor}", keys).deep_merge(remote_conditions[ar_accessor.to_sym])
|
133
135
|
ar_class.find(:all, :params => params )
|
134
136
|
end
|
135
137
|
|
136
138
|
def remote_resources_loaded?
|
137
139
|
!!@remote_resources_loaded
|
138
140
|
end
|
139
|
-
|
140
141
|
end
|
141
142
|
end
|
@@ -48,7 +48,7 @@ module RemoteAssociation
|
|
48
48
|
class_name: remote_rel.to_s.classify,
|
49
49
|
foreign_key: remote_rel.to_s.foreign_key,
|
50
50
|
association_type: :belongs_to_remote,
|
51
|
-
primary_key:
|
51
|
+
primary_key: primary_key
|
52
52
|
}.merge(options.symbolize_keys)
|
53
53
|
|
54
54
|
add_activeresource_relation(remote_rel.to_sym, rel_options)
|
@@ -61,19 +61,18 @@ module RemoteAssociation
|
|
61
61
|
if remote_resources_loaded?
|
62
62
|
@#{remote_rel} ? @#{remote_rel}.first : nil
|
63
63
|
else
|
64
|
-
@#{remote_rel} ||= self.#{rel_options[:foreign_key]}.present? ? #{rel_options[:class_name]}.find(:first, params: self.class.
|
64
|
+
@#{remote_rel} ||= self.#{rel_options[:foreign_key]}.present? ? #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash_for_#{remote_rel}(self.#{rel_options[:foreign_key]})) : nil
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
##
|
69
69
|
# Returns Hash with HTTP parameters to query remote API
|
70
|
-
def self.
|
70
|
+
def self.build_params_hash_for_#{remote_rel}(keys)
|
71
71
|
keys = [keys] unless keys.kind_of?(Array)
|
72
72
|
{"#{rel_options[:primary_key]}" => keys}
|
73
73
|
end
|
74
74
|
|
75
75
|
RUBY
|
76
|
-
|
77
76
|
end
|
78
77
|
end
|
79
78
|
end
|
@@ -50,18 +50,42 @@ module RemoteAssociation
|
|
50
50
|
|
51
51
|
attr_accessor :#{remote_rel}
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
53
|
+
##
|
54
|
+
# Adds a method to call remote relation
|
55
|
+
#
|
56
|
+
# === Example
|
57
|
+
#
|
58
|
+
# def customers
|
59
|
+
# if remote_resources_loaded?
|
60
|
+
# @customers ? @customers : nil
|
61
|
+
# else
|
62
|
+
# join_key = self.class.primary_key
|
63
|
+
# @customers ||= Person.
|
64
|
+
# find(:all, params: self.class.build_params_hash_for_customers(self.send(join_key)))
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
def #{remote_rel}
|
69
|
+
if remote_resources_loaded?
|
70
|
+
@#{remote_rel} ? @#{remote_rel} : []
|
71
|
+
else
|
72
|
+
join_key = self.class.primary_key
|
73
|
+
@#{remote_rel} ||= #{rel_options[:class_name]}.
|
74
|
+
find(:all, params: self.class.build_params_hash_for_#{remote_rel}(self.send(join_key)))
|
75
|
+
end
|
76
|
+
end
|
61
77
|
|
62
78
|
##
|
63
79
|
# Returns Hash with HTTP parameters to query remote API
|
64
|
-
|
80
|
+
#
|
81
|
+
# == Example
|
82
|
+
#
|
83
|
+
# def self.build_params_hash_for_customer(keys)
|
84
|
+
# keys = [keys] unless keys.kind_of?(Array)
|
85
|
+
# {"user_id" => keys}
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
def self.build_params_hash_for_#{remote_rel}(keys)
|
65
89
|
keys = [keys] unless keys.kind_of?(Array)
|
66
90
|
{"#{rel_options[:foreign_key]}" => keys}
|
67
91
|
end
|
@@ -54,13 +54,14 @@ module RemoteAssociation
|
|
54
54
|
if remote_resources_loaded?
|
55
55
|
@#{remote_rel} ? @#{remote_rel}.first : nil
|
56
56
|
else
|
57
|
-
|
57
|
+
join_key = self.class.primary_key
|
58
|
+
@#{remote_rel} ||= #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash_for_#{remote_rel}(self.send(join_key)))
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
61
62
|
##
|
62
63
|
# Returns Hash with HTTP parameters to query remote API
|
63
|
-
def self.
|
64
|
+
def self.build_params_hash_for_#{remote_rel}(keys)
|
64
65
|
keys = [keys] unless keys.kind_of?(Array)
|
65
66
|
{"#{rel_options[:foreign_key]}" => keys}
|
66
67
|
end
|
data/lib/remote_association.rb
CHANGED
@@ -40,16 +40,18 @@ module RemoteAssociation
|
|
40
40
|
#
|
41
41
|
# Author.add_activeresource_relation(:profile, {class_name: "Profile", foreign_key: "author_id"})
|
42
42
|
def add_activeresource_relation(name, options)
|
43
|
-
|
44
|
-
existing_relations[name] = options
|
45
|
-
@activeresource_relations = existing_relations
|
43
|
+
activeresource_relations[name] = options
|
46
44
|
end
|
47
45
|
|
48
46
|
# Returns settings for relations to ActiveResource models.
|
49
47
|
#
|
50
48
|
# === Examples
|
51
49
|
#
|
52
|
-
# Author.activeresource_relations #=>
|
50
|
+
# Author.activeresource_relations #=>
|
51
|
+
# {
|
52
|
+
# profile: {class_name: "Profile", foreign_key: "author_id"},
|
53
|
+
# badges: {class_name: "Badge", foreign_key: "author_id"}
|
54
|
+
# }
|
53
55
|
def activeresource_relations
|
54
56
|
@activeresource_relations ||= {}
|
55
57
|
end
|
@@ -62,7 +62,7 @@ describe RemoteAssociation, "method :belongs_to_remote" do
|
|
62
62
|
profile.user.should be_nil
|
63
63
|
end
|
64
64
|
|
65
|
-
describe "#
|
65
|
+
describe "#build_params_hash_for_relation" do
|
66
66
|
it "returns valid Hash of HTTP query string parameters" do
|
67
67
|
unset_const(:Profile)
|
68
68
|
class Profile < ActiveRecord::Base
|
@@ -70,8 +70,8 @@ describe RemoteAssociation, "method :belongs_to_remote" do
|
|
70
70
|
belongs_to_remote :user
|
71
71
|
end
|
72
72
|
|
73
|
-
Profile.
|
74
|
-
Profile.
|
73
|
+
Profile.build_params_hash_for_user(10).should eq({'id' => [10]})
|
74
|
+
Profile.build_params_hash_for_user([10, 13, 15]).should eq({'id' => [10, 13, 15]})
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
@@ -113,4 +113,41 @@ describe RemoteAssociation, "method :belongs_to_remote" do
|
|
113
113
|
Profile.first.user.name.should eq('User A')
|
114
114
|
end
|
115
115
|
end
|
116
|
+
|
117
|
+
context "safe when using several remotes" do
|
118
|
+
before do
|
119
|
+
unset_const(:User)
|
120
|
+
class User < ActiveRecord::Base
|
121
|
+
include RemoteAssociation::Base
|
122
|
+
belongs_to_remote :foo, foreign_key: 'user_side_foo_id', class_name: "CustomFoo", primary_key: 'foo_id'
|
123
|
+
belongs_to_remote :bar, foreign_key: 'user_side_bar_id', class_name: "CustomBar", primary_key: 'bar_id'
|
124
|
+
|
125
|
+
def user_side_foo_id; self.id; end
|
126
|
+
def user_side_bar_id; self.id; end
|
127
|
+
end
|
128
|
+
class CustomFoo < ActiveResource::Base
|
129
|
+
self.site = REMOTE_HOST
|
130
|
+
self.element_name = "foo"
|
131
|
+
end
|
132
|
+
class CustomBar < ActiveResource::Base
|
133
|
+
self.site = REMOTE_HOST
|
134
|
+
self.element_name = "bar"
|
135
|
+
end
|
136
|
+
|
137
|
+
@foo_body = [{foo: {id: 1, foo_id: 1, value: "F1"}}].to_json
|
138
|
+
@bar_body = [{bar: {id: 1, bar_id: 1, value: "B1"}}].to_json
|
139
|
+
|
140
|
+
FakeWeb.register_uri(:get, "#{REMOTE_HOST}/foos.json?foo_id%5B%5D=1", body: @foo_body)
|
141
|
+
FakeWeb.register_uri(:get, "#{REMOTE_HOST}/bars.json?bar_id%5B%5D=1", body: @bar_body)
|
142
|
+
end
|
143
|
+
|
144
|
+
it "returns remotes respectively by primary and classname" do
|
145
|
+
User.delete_all
|
146
|
+
add_user(1, 'Tester')
|
147
|
+
User.first.foo.id.should == 1
|
148
|
+
User.first.foo.value.should == 'F1'
|
149
|
+
User.first.bar.id.should == 1
|
150
|
+
User.first.bar.value.should == 'B1'
|
151
|
+
end
|
152
|
+
end
|
116
153
|
end
|
@@ -46,10 +46,10 @@ describe RemoteAssociation, 'method :has_many_remote' do
|
|
46
46
|
users.last.profiles.map(&:like).should eq ["letter C"]
|
47
47
|
end
|
48
48
|
|
49
|
-
describe '#
|
49
|
+
describe '#build_params_hash_for_relation' do
|
50
50
|
it 'returns valid Hash of HTTP query string parameters' do
|
51
|
-
User.
|
52
|
-
User.
|
51
|
+
User.build_params_hash_for_profiles(10).should eq({'user_id' => [10]})
|
52
|
+
User.build_params_hash_for_profiles([10, 13, 15]).should eq({'user_id' => [10, 13, 15]})
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -81,4 +81,44 @@ describe RemoteAssociation, 'method :has_many_remote' do
|
|
81
81
|
User.first.profiles.map(&:like).should eq(['letter A', 'letter B'])
|
82
82
|
end
|
83
83
|
end
|
84
|
+
|
85
|
+
context "safe when using several remotes" do
|
86
|
+
before do
|
87
|
+
unset_const(:User)
|
88
|
+
class User < ActiveRecord::Base
|
89
|
+
include RemoteAssociation::Base
|
90
|
+
has_many_remote :foos, foreign_key: 'foo_id', class_name: "CustomFoo"
|
91
|
+
has_many_remote :bars, foreign_key: 'bar_id', class_name: "CustomBar"
|
92
|
+
end
|
93
|
+
class CustomFoo < ActiveResource::Base
|
94
|
+
self.site = REMOTE_HOST
|
95
|
+
self.element_name = "foo"
|
96
|
+
end
|
97
|
+
class CustomBar < ActiveResource::Base
|
98
|
+
self.site = REMOTE_HOST
|
99
|
+
self.element_name = "bar"
|
100
|
+
end
|
101
|
+
|
102
|
+
@foos_body = [
|
103
|
+
{foo: {id: 1, foo_id: 1, value: "F1"}},
|
104
|
+
].to_json
|
105
|
+
|
106
|
+
@bars_body = [
|
107
|
+
{bar: {id: 1, bar_id: 1, value: "B1"}},
|
108
|
+
{bar: {id: 2, bar_id: 1, value: "B2"}},
|
109
|
+
{bar: {id: 3, bar_id: 1, value: "B3"}},
|
110
|
+
].to_json
|
111
|
+
|
112
|
+
FakeWeb.register_uri(:get, "#{REMOTE_HOST}/foos.json?foo_id%5B%5D=1", body: @foos_body)
|
113
|
+
FakeWeb.register_uri(:get, "#{REMOTE_HOST}/bars.json?bar_id%5B%5D=1", body: @bars_body)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "returns remotes respectively by foreign key and classname" do
|
117
|
+
User.delete_all
|
118
|
+
add_user(1, 'Tester')
|
119
|
+
|
120
|
+
User.first.foos.collect {|f| [f.id, f.value] }.should =~ [[1, 'F1']]
|
121
|
+
User.first.bars.collect {|b| [b.id, b.value] }.should =~ [[1, 'B1'], [2, 'B2'], [3, 'B3']]
|
122
|
+
end
|
123
|
+
end
|
84
124
|
end
|
@@ -43,10 +43,10 @@ describe RemoteAssociation, "method :has_one_remote" do
|
|
43
43
|
users.last.profile.like.should eq('letter B')
|
44
44
|
end
|
45
45
|
|
46
|
-
describe "#
|
46
|
+
describe "#build_params_hash_for_relation" do
|
47
47
|
it "returns valid Hash of HTTP query string parameters" do
|
48
|
-
User.
|
49
|
-
User.
|
48
|
+
User.build_params_hash_for_profile(10).should eq({'user_id' => [10]})
|
49
|
+
User.build_params_hash_for_profile([10, 13, 15]).should eq({'user_id' => [10, 13, 15]})
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remote_association
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -166,7 +166,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
166
166
|
version: '0'
|
167
167
|
segments:
|
168
168
|
- 0
|
169
|
-
hash:
|
169
|
+
hash: 2390194235140856121
|
170
170
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
171
|
none: false
|
172
172
|
requirements:
|
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
175
|
version: '0'
|
176
176
|
segments:
|
177
177
|
- 0
|
178
|
-
hash:
|
178
|
+
hash: 2390194235140856121
|
179
179
|
requirements: []
|
180
180
|
rubyforge_project:
|
181
181
|
rubygems_version: 1.8.24
|