Skip to content

Commit 9f31073

Browse files
committed
fixing Query::Builder#find with parameters
1 parent 6bef918 commit 9f31073

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/json_api_client/query/builder.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Builder
77

88
def initialize(klass, opts = {})
99
@klass = klass
10-
@primary_key = nil
10+
@primary_key = opts.fetch( :primary_key, nil )
1111
@pagination_params = opts.fetch( :pagination_params, {} )
1212
@path_params = opts.fetch( :path_params, {} )
1313
@additional_params = opts.fetch( :additional_params, {} )
@@ -87,12 +87,12 @@ def to_a
8787
def find(args = {})
8888
case args
8989
when Hash
90-
where(args)
90+
scope = where(args)
9191
else
92-
@primary_key = args
92+
scope = _new_scope( primary_key: args )
9393
end
9494

95-
klass.requestor.get(params)
95+
klass.requestor.get(scope.params)
9696
end
9797

9898
def method_missing(method_name, *args, &block)
@@ -103,6 +103,7 @@ def method_missing(method_name, *args, &block)
103103

104104
def _new_scope( opts = {} )
105105
self.class.new( @klass,
106+
primary_key: opts.fetch( :primary_key, @primary_key ),
106107
pagination_params: @pagination_params.merge( opts.fetch( :pagination_params, {} ) ),
107108
path_params: @path_params.merge( opts.fetch( :path_params, {} ) ),
108109
additional_params: @additional_params.merge( opts.fetch( :additional_params, {} ) ),

test/unit/query_builder_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,17 @@ def test_find_with_args
243243
all_stub = stub_request(:get, "http://example.com/articles")
244244
.to_return(headers: {content_type: "application/vnd.api+json"}, body: { data: [] }.to_json)
245245

246+
find_stub = stub_request(:get, "http://example.com/articles/6")
247+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: { data: [] }.to_json)
248+
246249
scope = Article.where()
247250

248251
scope.find( "author.id" => "foo" )
252+
scope.find(6)
249253
scope.all
250254

251255
assert_requested first_stub, times: 1
252256
assert_requested all_stub, times: 1
257+
assert_requested find_stub, times: 1
253258
end
254259
end

0 commit comments

Comments
 (0)