Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow arguments in relationship methods

  • Loading branch information...
commit 7ce6a8db0eeac005cc6e7bc65550ceab0404f298 1 parent e33489b
@remiprev authored
Showing with 39 additions and 9 deletions.
  1. +21 −6 lib/her/model/relationships.rb
  2. +18 −3 spec/model/relationships_spec.rb
View
27 lib/her/model/relationships.rb
@@ -62,9 +62,14 @@ def has_many(name, attrs={}) # {{{
}.merge(attrs)
(relationships[:has_many] ||= []) << attrs
- define_method(name) do
+ define_method(name) do |*method_attrs|
+ method_attrs = method_attrs[0] || {}
klass = self.class.nearby_class(attrs[:class_name])
- @data[name] ||= klass.get_collection("#{self.class.build_request_path(:id => id)}#{attrs[:path]}")
+ if method_attrs.any?
+ klass.get_collection("#{self.class.build_request_path(method_attrs.merge(:id => id))}#{attrs[:path]}")
+ else
+ @data[name] ||= klass.get_collection("#{self.class.build_request_path(:id => id)}#{attrs[:path]}")
+ end
end
end # }}}
@@ -94,9 +99,14 @@ def has_one(name, attrs={}) # {{{
}.merge(attrs)
(relationships[:has_one] ||= []) << attrs
- define_method(name) do
+ define_method(name) do |*method_attrs|
+ method_attrs = method_attrs[0] || {}
klass = self.class.nearby_class(attrs[:class_name])
- @data[name] ||= klass.get_resource("#{self.class.build_request_path(:id => id)}#{attrs[:path]}")
+ if method_attrs.any?
+ klass.get_resource("#{self.class.build_request_path(method_attrs.merge(:id => id))}#{attrs[:path]}")
+ else
+ @data[name] ||= klass.get_resource("#{self.class.build_request_path(:id => id)}#{attrs[:path]}")
+ end
end
end # }}}
@@ -127,9 +137,14 @@ def belongs_to(name, attrs={}) # {{{
}.merge(attrs)
(relationships[:belongs_to] ||= []) << attrs
- define_method(name) do
+ define_method(name) do |*method_attrs|
+ method_attrs = method_attrs[0] || {}
klass = self.class.nearby_class(attrs[:class_name])
- @data[name] ||= klass.get_resource("#{klass.build_request_path(:id => @data[attrs[:foreign_key].to_sym])}")
+ if method_attrs.any?
+ klass.get_resource("#{klass.build_request_path(method_attrs.merge(:id => @data[attrs[:foreign_key].to_sym]))}")
+ else
+ @data[name] ||= klass.get_resource("#{klass.build_request_path(:id => @data[attrs[:foreign_key].to_sym])}")
+ end
end
end # }}}
View
21 spec/model/relationships_spec.rb
@@ -79,10 +79,13 @@
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke", :comments => [{ :id => 2, :body => "Tobias, you blow hard!" }, { :id => 3, :body => "I wouldn't mind kissing that man between the cheeks, so to speak" }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 1, :name => "Bluth Company" }, :organization_id => 1 }.to_json] }
- stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization_id => 1 }.to_json] }
+ stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization_id => 2 }.to_json] }
+ stub.get("/users/1/comments") { |env| [200, {}, [{ :id => 4, :body => "They're having a FIRESALE?" }].to_json] }
stub.get("/users/2/comments") { |env| [200, {}, [{ :id => 4, :body => "They're having a FIRESALE?" }, { :id => 5, :body => "Is this the tiny town from Footloose?" }].to_json] }
stub.get("/users/2/role") { |env| [200, {}, { :id => 2, :body => "User" }.to_json] }
- stub.get("/organizations/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company" }.to_json] }
+ stub.get("/users/1/role") { |env| [200, {}, { :id => 3, :body => "User" }.to_json] }
+ stub.get("/organizations/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company Foo" }.to_json] }
+ stub.get("/organizations/2") { |env| [200, {}, { :id => 2, :name => "Bluth Company" }.to_json] }
end
end
@@ -114,6 +117,10 @@
@user_without_included_data.comments.first.body.should == "They're having a FIRESALE?"
end # }}}
+ it "fetches has_many data even if it was included, only if called with parameters" do # {{{
+ @user_with_included_data.comments(:foo_id => 1).length.should == 1
+ end # }}}
+
it "maps an array of included data through has_one" do # {{{
@user_with_included_data.role.should be_a(Foo::Role)
@user_with_included_data.role.id.should == 1
@@ -126,6 +133,10 @@
@user_without_included_data.role.body.should == "User"
end # }}}
+ it "fetches has_one data even if it was included, only if called with parameters" do # {{{
+ @user_with_included_data.role(:foo_id => 2).id.should == 3
+ end # }}}
+
it "maps an array of included data through belongs_to" do # {{{
@user_with_included_data.organization.should be_a(Foo::Organization)
@user_with_included_data.organization.id.should == 1
@@ -134,9 +145,13 @@
it "fetches data that was not included through belongs_to" do # {{{
@user_without_included_data.organization.should be_a(Foo::Organization)
- @user_without_included_data.organization.id.should == 1
+ @user_without_included_data.organization.id.should == 2
@user_without_included_data.organization.name.should == "Bluth Company"
end # }}}
+
+ it "fetches belongs_to data even if it was included, only if called with parameters" do # {{{
+ @user_with_included_data.organization(:foo_id => 1).name.should == "Bluth Company Foo"
+ end # }}}
end
context "handling relationships with details" do
Please sign in to comment.
Something went wrong with that request. Please try again.