Skip to content
Browse files

Remove params bound to path segments from the query string

Otherwise, these parameters would be included twice :(
  • Loading branch information...
1 parent 609f9b7 commit d87dc7bde053e3d0f3979a26f732ff4cbfa69604 @thomsbg thomsbg committed with Aug 16, 2012
Showing with 29 additions and 21 deletions.
  1. +1 −1 lib/her/model/introspection.rb
  2. +20 −20 lib/her/model/paths.rb
  3. +8 −0 spec/model/paths_spec.rb
View
2 lib/her/model/introspection.rb
@@ -38,7 +38,7 @@ def containing_module # {{{
# @user = User.find(1)
# p @user # => #<User(/users/1) id=1 name="Tobias Fünke">
def inspect # {{{
- "#<#{self.class}(#{self.class.build_request_path(@data)}) #{@data.inject([]) { |memo, item| key, value = item; memo << "#{key}=#{attribute_for_inspect(value)}"}.join(" ")}>"
+ "#<#{self.class}(#{request_path}) #{@data.inject([]) { |memo, item| key, value = item; memo << "#{key}=#{attribute_for_inspect(value)}"}.join(" ")}>"
end # }}}
private
View
40 lib/her/model/paths.rb
@@ -11,7 +11,7 @@ module Paths
#
# User.find(1) # Fetched via GET /utilisateurs/1
def request_path # {{{
- self.class.build_request_path(@data)
+ self.class.build_request_path(to_params)
end # }}}
module ClassMethods
@@ -40,25 +40,25 @@ def resource_path(path=nil) # {{{
@her_resource_path = path
end # }}}
- # Return a custom path based on the collection path and variable parameters
- #
- # @example
- # class User
- # include Her::Model
- # collection_path "/utilisateurs"
- # end
- #
- # User.all # Fetched via GET /utilisateurs
- def build_request_path(path=nil, parameters={}) # {{{
- unless path.is_a?(String)
- parameters = path || {}
- path = parameters.include?(:id) ? @her_resource_path : @her_collection_path
- end
- path.gsub(/:([\w_]+)/) do
- # Look for :key or :_key, otherwise raise an exception
- parameters[$1.to_sym] || parameters["_#{$1}".to_sym] || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
- end
- end # }}}
+ # Return a custom path based on the collection path and variable parameters
+ #
+ # @example
+ # class User
+ # include Her::Model
+ # collection_path "/utilisateurs"
+ # end
+ #
+ # User.all # Fetched via GET /utilisateurs
+ def build_request_path(path=nil, parameters={}) # {{{
+ unless path.is_a?(String)
+ parameters = path || {}
+ path = parameters.include?(:id) ? @her_resource_path : @her_collection_path
+ end
+ path.gsub(/:([\w_]+)/) do
+ # Look for :key or :_key, otherwise raise an exception
+ parameters.delete($1.to_sym) || parameters.delete("_#{$1}".to_sym) || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
+ end
+ end # }}}
end
end
end
View
8 spec/model/paths_spec.rb
@@ -28,14 +28,22 @@
it "builds paths with custom collection path with multiple variables" do # {{{
Foo::User.collection_path "/organizations/:organization_id/utilisateurs"
+
Foo::User.build_request_path(:id => "foo", :_organization_id => "acme").should == "/organizations/acme/utilisateurs/foo"
Foo::User.build_request_path(:_organization_id => "acme").should == "/organizations/acme/utilisateurs"
+
+ Foo::User.build_request_path(:id => "foo", :organization_id => "acme").should == "/organizations/acme/utilisateurs/foo"
+ Foo::User.build_request_path(:organization_id => "acme").should == "/organizations/acme/utilisateurs"
end # }}}
it "builds paths with custom relative collection path with multiple variables" do # {{{
Foo::User.collection_path "organizations/:organization_id/utilisateurs"
+
Foo::User.build_request_path(:id => "foo", :_organization_id => "acme").should == "organizations/acme/utilisateurs/foo"
Foo::User.build_request_path(:_organization_id => "acme").should == "organizations/acme/utilisateurs"
+
+ Foo::User.build_request_path(:id => "foo", :organization_id => "acme").should == "organizations/acme/utilisateurs/foo"
+ Foo::User.build_request_path(:organization_id => "acme").should == "organizations/acme/utilisateurs"
end # }}}
it "builds paths with custom item path" do # {{{

0 comments on commit d87dc7b

Please sign in to comment.
Something went wrong with that request. Please try again.