Skip to content

Commit f7936b1

Browse files
committed
fix association passing from collections for resources except Tag - zendesk#108
1 parent 64d0c44 commit f7936b1

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

lib/zendesk_api/collection.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def <<(item)
126126
raise "this collection is for #{@resource_class}"
127127
end
128128
else
129-
@resources << wrap_resource(item)
129+
@resources << wrap_resource(item, true)
130130
end
131131
end
132132

@@ -363,14 +363,17 @@ def handle_response(body)
363363
end
364364

365365
# Simplified Associations#wrap_resource
366-
def wrap_resource(res)
366+
def wrap_resource(res, with_association = @resource_class == Tag)
367367
case res
368368
when Array
369369
wrap_resource(Hash[*res])
370370
when Hash
371-
@resource_class.new(@client, res.merge(:association => association))
371+
res = res.merge(:association => @association) if with_association
372+
@resource_class.new(@client, res)
372373
else
373-
@resource_class.new(@client, :id => res, :association => association)
374+
res = { :id => res }
375+
res.merge!(:association => @association) if with_association
376+
@resource_class.new(@client, res)
374377
end
375378
end
376379

lib/zendesk_api/resource.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def loaded_associations
9292
end
9393

9494
# Returns the path to the resource
95-
def path(*args)
96-
@association.generate_path(self, *args)
95+
def path(options = {})
96+
@association.generate_path(self, options)
9797
end
9898

9999
# Passes #to_json to the underlying attributes hash

spec/core/collection_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,5 +703,21 @@ def to_proc
703703
subject.create
704704
end
705705
end
706+
707+
context "resources" do
708+
before(:each) do
709+
stub_json_request(:get, %r{test_resources/active},
710+
json(:test_resources => [{ :id => 1 }]))
711+
712+
subject.fetch
713+
714+
stub_json_request(:put, %r{test_resources/1})
715+
end
716+
717+
it "should not save using the collection path" do
718+
resource = subject.first
719+
resource.save
720+
end
721+
end
706722
end
707723
end

0 commit comments

Comments
 (0)