Skip to content

Commit

Permalink
Merge pull request #136 from richmolj/master
Browse files Browse the repository at this point in the history
Fix remote sideload belongs_to links
  • Loading branch information
richmolj authored May 8, 2019
2 parents ad73757 + 896349e commit 8ade48c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/graphiti/util/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def on_demand_links(url)

def params
@params ||= {}.tap do |params|
if @sideload.type != :belongs_to
if @sideload.type != :belongs_to || @sideload.remote?
params[:filter] = @sideload.base_filter([@model])
end

Expand All @@ -70,7 +70,7 @@ def params
def path
@path ||=
path = @sideload.resource.endpoint[:url].to_s
if @sideload.type == :belongs_to
if @sideload.type == :belongs_to && !@sideload.remote?
path = "#{path}/#{@model.send(@sideload.foreign_key)}"
end
path
Expand Down
22 changes: 20 additions & 2 deletions spec/serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ def define_relationship
render
credit_card = json["data"][0]["relationships"]["credit_card"]
expect(credit_card["links"]["related"])
.to eq("http://foo.com/mastercards/789")
.to eq("http://foo.com/mastercards?filter[id]=789")
end
end

Expand Down Expand Up @@ -1351,7 +1351,25 @@ def classification
remote: "http://foo.com/classifications"
render
expect(classification["links"]["related"])
.to eq("http://foo.com/classifications/789")
.to eq("http://foo.com/classifications?filter[id]=789")
end

# Special case because we hit index with a filter
context 'and params are customized' do
before do
resource.belongs_to :classification,
remote: "http://foo.com/classifications" do
params do |hash|
hash[:filter][:foo] = 'bar'
end
end
end

it 'links correctly' do
render
expect(classification["links"]["related"])
.to eq("http://foo.com/classifications?filter[foo]=bar&filter[id]=789")
end
end
end
end
Expand Down

0 comments on commit 8ade48c

Please sign in to comment.