Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:denyago/remote_association

Conflicts:
	lib/remote_association/has_many_remote.rb
	lib/remote_association/has_one_remote.rb
  • Loading branch information...
commit 5915e5d152b4874477a375c775c1e98fbf9c09a7 2 parents cc371aa + 7a38ef1
@denyago authored
View
10 lib/remote_association.rb
@@ -40,16 +40,18 @@ module ClassMethods
#
# Author.add_activeresource_relation(:profile, {class_name: "Profile", foreign_key: "author_id"})
def add_activeresource_relation(name, options)
- existing_relations = activeresource_relations
- existing_relations[name] = options
- @activeresource_relations = existing_relations
+ activeresource_relations[name] = options
end
# Returns settings for relations to ActiveResource models.
#
# === Examples
#
- # Author.activeresource_relations #=> [{profile: {class_name: "Profile", foreign_key: "author_id"}}]
+ # Author.activeresource_relations #=>
+ # {
+ # profile: {class_name: "Profile", foreign_key: "author_id"},
+ # badges: {class_name: "Badge", foreign_key: "author_id"}
+ # }
def activeresource_relations
@activeresource_relations ||= {}
end
View
3  lib/remote_association/active_record/relation.rb
@@ -131,13 +131,12 @@ def set_remote_resources_loaded
end
def fetch_remote_objects(ar_class, keys, ar_accessor)
- params = klass.build_params_hash(keys).deep_merge(remote_conditions[ar_accessor.to_sym])
+ params = klass.send(:"build_params_hash_for_#{ar_accessor}", keys).deep_merge(remote_conditions[ar_accessor.to_sym])
ar_class.find(:all, :params => params )
end
def remote_resources_loaded?
!!@remote_resources_loaded
end
-
end
end
View
5 lib/remote_association/belongs_to_remote.rb
@@ -61,19 +61,18 @@ def #{remote_rel}
if remote_resources_loaded?
@#{remote_rel} ? @#{remote_rel}.first : nil
else
- @#{remote_rel} ||= self.#{rel_options[:foreign_key]}.present? ? #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash(self.#{rel_options[:foreign_key]})) : nil
+ @#{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
end
end
##
# Returns Hash with HTTP parameters to query remote API
- def self.build_params_hash(keys)
+ def self.build_params_hash_for_#{remote_rel}(keys)
keys = [keys] unless keys.kind_of?(Array)
{"#{rel_options[:primary_key]}" => keys}
end
RUBY
-
end
end
end
View
44 lib/remote_association/has_many_remote.rb
@@ -50,20 +50,42 @@ def has_many_remote(remote_rel, options ={})
attr_accessor :#{remote_rel}
- def #{remote_rel} # def customers
- if remote_resources_loaded? # if remote_resources_loaded?
- @#{remote_rel} ? @#{remote_rel} : [] # @customers ? @customers : []
- else # else
- join_key = self.class.primary_key # join_key = self.class.primary_key
- @#{remote_rel} ||= #{rel_options[:class_name]}. # @customers ||= Person.
- find(:all, params: self.class. # find(:all, params: self.class.
- build_params_hash(self.send(join_key))) # build_params_hash(self.send(join_key)))
- end # end
- end # end
+ ##
+ # Adds a method to call remote relation
+ #
+ # === Example
+ #
+ # def customers
+ # if remote_resources_loaded?
+ # @customers ? @customers : nil
+ # else
+ # join_key = self.class.primary_key
+ # @customers ||= Person.
+ # find(:all, params: self.class.build_params_hash_for_customers(self.send(join_key)))
+ # end
+ # end
+ #
+ def #{remote_rel}
+ if remote_resources_loaded?
+ @#{remote_rel} ? @#{remote_rel} : []
+ else
+ join_key = self.class.primary_key
+ @#{remote_rel} ||= #{rel_options[:class_name]}.
+ find(:all, params: self.class.build_params_hash_for_#{remote_rel}(self.send(join_key)))
+ end
+ end
##
# Returns Hash with HTTP parameters to query remote API
- def self.build_params_hash(keys)
+ #
+ # == Example
+ #
+ # def self.build_params_hash_for_customer(keys)
+ # keys = [keys] unless keys.kind_of?(Array)
+ # {"user_id" => keys}
+ # end
+ #
+ def self.build_params_hash_for_#{remote_rel}(keys)
keys = [keys] unless keys.kind_of?(Array)
{"#{rel_options[:foreign_key]}" => keys}
end
View
4 lib/remote_association/has_one_remote.rb
@@ -55,13 +55,13 @@ def #{remote_rel}
@#{remote_rel} ? @#{remote_rel}.first : nil
else
join_key = self.class.primary_key
- @#{remote_rel} ||= #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash(self.send(join_key)))
+ @#{remote_rel} ||= #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash_for_#{remote_rel}(self.send(join_key)))
end
end
##
# Returns Hash with HTTP parameters to query remote API
- def self.build_params_hash(keys)
+ def self.build_params_hash_for_#{remote_rel}(keys)
keys = [keys] unless keys.kind_of?(Array)
{"#{rel_options[:foreign_key]}" => keys}
end
View
43 spec/remote_association/belongs_to_remote_spec.rb
@@ -62,7 +62,7 @@ class Profile < ActiveRecord::Base
profile.user.should be_nil
end
- describe "#build_params_hash" do
+ describe "#build_params_hash_for_relation" do
it "returns valid Hash of HTTP query string parameters" do
unset_const(:Profile)
class Profile < ActiveRecord::Base
@@ -70,8 +70,8 @@ class Profile < ActiveRecord::Base
belongs_to_remote :user
end
- Profile.build_params_hash(10).should eq({'id' => [10]})
- Profile.build_params_hash([10, 13, 15]).should eq({'id' => [10, 13, 15]})
+ Profile.build_params_hash_for_user(10).should eq({'id' => [10]})
+ Profile.build_params_hash_for_user([10, 13, 15]).should eq({'id' => [10, 13, 15]})
end
end
@@ -113,4 +113,41 @@ class Profile < ActiveRecord::Base
Profile.first.user.name.should eq('User A')
end
end
+
+ context "safe when using several remotes" do
+ before do
+ unset_const(:User)
+ class User < ActiveRecord::Base
+ include RemoteAssociation::Base
+ belongs_to_remote :foo, foreign_key: 'user_side_foo_id', class_name: "CustomFoo", primary_key: 'foo_id'
+ belongs_to_remote :bar, foreign_key: 'user_side_bar_id', class_name: "CustomBar", primary_key: 'bar_id'
+
+ def user_side_foo_id; self.id; end
+ def user_side_bar_id; self.id; end
+ end
+ class CustomFoo < ActiveResource::Base
+ self.site = REMOTE_HOST
+ self.element_name = "foo"
+ end
+ class CustomBar < ActiveResource::Base
+ self.site = REMOTE_HOST
+ self.element_name = "bar"
+ end
+
+ @foo_body = [{foo: {id: 1, foo_id: 1, value: "F1"}}].to_json
+ @bar_body = [{bar: {id: 1, bar_id: 1, value: "B1"}}].to_json
+
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/foos.json?foo_id%5B%5D=1", body: @foo_body)
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/bars.json?bar_id%5B%5D=1", body: @bar_body)
+ end
+
+ it "returns remotes respectively by primary and classname" do
+ User.delete_all
+ add_user(1, 'Tester')
+ User.first.foo.id.should == 1
+ User.first.foo.value.should == 'F1'
+ User.first.bar.id.should == 1
+ User.first.bar.value.should == 'B1'
+ end
+ end
end
View
46 spec/remote_association/has_many_remote_spec.rb
@@ -46,10 +46,10 @@ class Profile < ActiveResource::Base
users.last.profiles.map(&:like).should eq ["letter C"]
end
- describe '#build_params_hash' do
+ describe '#build_params_hash_for_relation' do
it 'returns valid Hash of HTTP query string parameters' do
- User.build_params_hash(10).should eq({'user_id' => [10]})
- User.build_params_hash([10, 13, 15]).should eq({'user_id' => [10, 13, 15]})
+ User.build_params_hash_for_profiles(10).should eq({'user_id' => [10]})
+ User.build_params_hash_for_profiles([10, 13, 15]).should eq({'user_id' => [10, 13, 15]})
end
end
@@ -81,4 +81,44 @@ class User < ActiveRecord::Base
User.first.profiles.map(&:like).should eq(['letter A', 'letter B'])
end
end
+
+ context "safe when using several remotes" do
+ before do
+ unset_const(:User)
+ class User < ActiveRecord::Base
+ include RemoteAssociation::Base
+ has_many_remote :foos, foreign_key: 'foo_id', class_name: "CustomFoo"
+ has_many_remote :bars, foreign_key: 'bar_id', class_name: "CustomBar"
+ end
+ class CustomFoo < ActiveResource::Base
+ self.site = REMOTE_HOST
+ self.element_name = "foo"
+ end
+ class CustomBar < ActiveResource::Base
+ self.site = REMOTE_HOST
+ self.element_name = "bar"
+ end
+
+ @foos_body = [
+ {foo: {id: 1, foo_id: 1, value: "F1"}},
+ ].to_json
+
+ @bars_body = [
+ {bar: {id: 1, bar_id: 1, value: "B1"}},
+ {bar: {id: 2, bar_id: 1, value: "B2"}},
+ {bar: {id: 3, bar_id: 1, value: "B3"}},
+ ].to_json
+
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/foos.json?foo_id%5B%5D=1", body: @foos_body)
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/bars.json?bar_id%5B%5D=1", body: @bars_body)
+ end
+
+ it "returns remotes respectively by foreign key and classname" do
+ User.delete_all
+ add_user(1, 'Tester')
+
+ User.first.foos.collect {|f| [f.id, f.value] }.should =~ [[1, 'F1']]
+ User.first.bars.collect {|b| [b.id, b.value] }.should =~ [[1, 'B1'], [2, 'B2'], [3, 'B3']]
+ end
+ end
end
View
6 spec/remote_association/has_one_remote_spec.rb
@@ -43,10 +43,10 @@ class Profile < ActiveResource::Base
users.last.profile.like.should eq('letter B')
end
- describe "#build_params_hash" do
+ describe "#build_params_hash_for_relation" do
it "returns valid Hash of HTTP query string parameters" do
- User.build_params_hash(10).should eq({'user_id' => [10]})
- User.build_params_hash([10, 13, 15]).should eq({'user_id' => [10, 13, 15]})
+ User.build_params_hash_for_profile(10).should eq({'user_id' => [10]})
+ User.build_params_hash_for_profile([10, 13, 15]).should eq({'user_id' => [10, 13, 15]})
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.