api_resource 0.6.5 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,9 @@ module ApiResource
24
24
 
25
25
  id_method_name = self.foreign_key_name(assoc_name)
26
26
  associated_class = opts[:class_name] || assoc_name.to_s.classify
27
+
28
+ # pass this along
29
+ opts[:name] = assoc_name
27
30
 
28
31
  klass.api_resource_generated_methods.module_eval <<-EOE, __FILE__, __LINE__ + 1
29
32
  def #{assoc_name}
@@ -33,13 +33,34 @@ module ApiResource
33
33
  # if we don't have a remote path and we do have and id,
34
34
  # we set it before we call the internal object
35
35
  # this lets us dynamically generate the correct path
36
- if self.remote_path.blank? && self.owner.try(:id).present?
37
- self.remote_path = self.klass.collection_path(
38
- self.owner.class.to_s.foreign_key => self.owner.id
36
+ if self.remote_path.blank?
37
+ # first try for a set of ids e.g. /objects.json?ids[]=1
38
+ associated_ids = self.owner.read_attribute(
39
+ self.association_id_method
39
40
  )
41
+ if associated_ids.is_a?(Array) && associated_ids.present?
42
+ self.remote_path = self.klass.collection_path(
43
+ :ids => associated_ids
44
+ )
45
+ # next try for a foreign key e.g. /objects.json?owner_id=1
46
+ elsif self.owner.try(:id).present?
47
+ self.remote_path = self.klass.collection_path(
48
+ self.owner.class.to_s.foreign_key => self.owner.id
49
+ )
50
+ end
40
51
  end
41
52
  super
42
53
  end
54
+
55
+ protected
56
+
57
+ # The method by which we get ids for the association
58
+ # e.g. object_ids
59
+ def association_id_method
60
+ self.class.foreign_key_name(@options["name"])
61
+ end
62
+
63
+
43
64
  end
44
65
  end
45
66
  end
@@ -6,14 +6,16 @@ module ApiResource
6
6
 
7
7
  include Enumerable
8
8
 
9
- attr_reader :conditions, :klass, :included_objects, :internal_object, :association,
10
- :remote_path
9
+ attr_reader :conditions, :klass, :included_objects,
10
+ :internal_object, :association, :remote_path
11
11
 
12
12
  # TODO: add the other load forcing methods here for collections
13
- delegate :[], :[]=, :<<, :first, :second, :last, :blank?, :nil?, :include?, :push, :pop,
14
- :+, :concat, :flatten, :flatten!, :compact, :compact!, :empty?, :fetch, :map,
15
- :reject, :reject!, :reverse, :select, :select!, :size, :sort, :sort!, :uniq, :uniq!,
16
- :to_a, :sample, :slice, :slice!, :count, :present?, :to => :internal_object
13
+ delegate :[], :[]=, :<<, :first, :second, :last, :blank?, :nil?,
14
+ :include?, :push, :pop, :+, :concat, :flatten, :flatten!, :compact,
15
+ :compact!, :empty?, :fetch, :map, :reject, :reject!, :reverse,
16
+ :select, :select!, :size, :sort, :sort!, :uniq, :uniq!, :to_a,
17
+ :sample, :slice, :slice!, :count, :present?, :delete_if,
18
+ :to => :internal_object
17
19
 
18
20
  # need to figure out what to do with args in the subclass,
19
21
  # parent is the set of scopes we have right now
@@ -1,3 +1,3 @@
1
1
  module ApiResource
2
- VERSION = "0.6.5"
2
+ VERSION = "0.6.6"
3
3
  end
@@ -20,6 +20,23 @@ module ApiResource
20
20
 
21
21
  end
22
22
 
23
+ context "#load" do
24
+
25
+ it "does a find based on its set of ids if present" do
26
+ tr = TestResource.new
27
+ tr.has_many_object_ids = [1,2]
28
+
29
+ HasManyObject.connection.expects(:get)
30
+ .with("/has_many_objects.json?ids%5B%5D=1&ids%5B%5D=2")
31
+ .returns([{"name" => "Test"}])
32
+
33
+ tr.has_many_objects.length.should be 1
34
+ tr.has_many_objects.first.name.should eql("Test")
35
+
36
+ end
37
+
38
+ end
39
+
23
40
  end
24
41
  end
25
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5
4
+ version: 0.6.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-04-16 00:00:00.000000000 Z
14
+ date: 2013-04-17 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake