restful_resource 0.8.18 → 0.8.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4333040aefc4230aa100dbd68d871a44e2920ba9
4
- data.tar.gz: ac2b15eeac7f8c1a18d200b4745bb6df543994d5
3
+ metadata.gz: ee2cd0cbeb47f73c4698b263fdb12f0fd9f8ba31
4
+ data.tar.gz: c7bee8e65111360e37d50b6e1a930a8ffa0c07ba
5
5
  SHA512:
6
- metadata.gz: 8cf0775238a03d37776b352b1b3c524261e209d76d41ccd7f74ea79b0becc9e2be137ccef78f91cfdabe3b1ad3f9227d6211c86015f0dd9db46b9a0c91178502
7
- data.tar.gz: 1052de7c40ac072b0f3bdc16941b2df243d386971f8327455e8e9c2490b39ab36cbddb3068a64726e46e09bd208f2821339a29e672097ecb6e645563f2b3b768
6
+ metadata.gz: b600cd921813075123dc916f63c0a431e231e8275a7bc0c0763b3a6e2d571535c863267db73d40c9b5318a9d971a1c7540be7025d6232828f947ebdd7dfb9188
7
+ data.tar.gz: 153e70baf50ea3302a9f4bd5ac5031e690167b3752ea276175e5a434b0d3edafd5b27065490ede7c9dd42ca298dbff48105ea9b5f9db3050555439bee4830462
@@ -1,19 +1,25 @@
1
1
  module RestfulResource
2
2
  module Associations
3
3
  def has_many(nested_resource_type)
4
- klass = nested_resource_type.to_s.singularize.camelize.safe_constantize
5
- klass = RestfulResource::OpenObject if (klass.nil? || (klass.superclass != RestfulResource))
4
+ namespace = self.to_s.deconstantize
5
+ klass_name = "#{nested_resource_type.to_s.singularize.camelize}"
6
+ klass_name = "#{namespace}::#{klass_name}" if namespace.present?
6
7
 
7
8
  self.send(:define_method, nested_resource_type) do
9
+ klass = klass_name.safe_constantize
10
+ klass = RestfulResource::OpenObject if (klass.nil? || (klass.superclass != RestfulResource::Base))
8
11
  @inner_object.send(nested_resource_type).map { |obj| klass.new(obj) }
9
12
  end
10
13
  end
11
14
 
12
15
  def has_one(nested_resource_type)
13
- klass = nested_resource_type.to_s.camelize.safe_constantize
14
- klass = RestfulResource::OpenObject if (klass.nil? || klass.superclass != RestfulResource)
16
+ namespace = self.to_s.deconstantize
17
+ klass_name = "#{nested_resource_type.to_s.camelize}"
18
+ klass_name = "#{namespace}::#{klass_name}" if namespace.present?
15
19
 
16
20
  self.send(:define_method, nested_resource_type) do
21
+ klass = klass_name.safe_constantize
22
+ klass = RestfulResource::OpenObject if (klass.nil? || (klass.superclass != RestfulResource::Base))
17
23
  nested_resource = @inner_object.send(nested_resource_type)
18
24
  return nil if nested_resource.nil?
19
25
  klass.new(@inner_object.send(nested_resource_type))
@@ -1,3 +1,3 @@
1
1
  module RestfulResource
2
- VERSION = "0.8.18"
2
+ VERSION = "0.8.19"
3
3
  end
data/spec/fixtures.rb CHANGED
@@ -27,3 +27,23 @@ end
27
27
  class TestB < BaseB
28
28
  self.resource_path "testb"
29
29
  end
30
+
31
+ module ComplicatedModule
32
+ class Parent < RestfulResource::Base
33
+ resource_path "parent"
34
+ has_many :children
35
+
36
+ def is_parent?
37
+ true
38
+ end
39
+ end
40
+
41
+ class Child < RestfulResource::Base
42
+ resource_path 'parents/:parent_id/children'
43
+ has_one :parent
44
+
45
+ def full_name
46
+ "#{self.first_name} #{self.second_name}"
47
+ end
48
+ end
49
+ end
@@ -2,33 +2,43 @@ require_relative '../spec_helper'
2
2
 
3
3
  describe RestfulResource::Associations do
4
4
  describe "#has_many" do
5
- it "should add a method to access nested resource" do
6
- make = Make.new({
7
- name: 'Volkswagen',
8
- models:
5
+ before :each do
6
+ @parent = ComplicatedModule::Parent.new({
7
+ name: 'John Doe',
8
+ children:
9
9
  [
10
- {name: 'Golf', rrp: 1000},
11
- {name: 'Passat', rrp: 3000}
10
+ {first_name: 'David', second_name: 'Doe'},
11
+ {first_name: 'Mary', second_name: 'Doe'}
12
12
  ]
13
13
  })
14
+ end
15
+
16
+ it "should add a method to access nested resource" do
17
+ expect(@parent.children.first.first_name).to eq 'David'
18
+ expect(@parent.children.last.first_name).to eq 'Mary'
19
+ expect(@parent.children.first.to_json).to eq({first_name: 'David', second_name: 'Doe'}.to_json)
20
+ end
14
21
 
15
- expect(make.models.first.name).to eq 'Golf'
16
- expect(make.models.last.name).to eq 'Passat'
17
- expect(make.models.first.rrp).to eq 1000
18
- expect(make.models.last.rrp).to eq 3000
19
- expect(make.models.first.to_json).to eq({name: 'Golf', rrp: 1000}.to_json)
22
+ it "should pick the right class for the instantiation of chilren" do
23
+ expect(@parent.children.first.full_name).to eq 'David Doe'
20
24
  end
21
25
  end
22
26
 
23
27
  describe "#has_one" do
24
- it "should add a method to access nested resource" do
25
- model = Model.new({
26
- name: 'Golf',
27
- make: {name: 'Volkswagen'}
28
+ before :each do
29
+ @child = ComplicatedModule::Child.new({
30
+ first_name: 'David', second_name: 'Smith',
31
+ parent: {name: 'John Smith'}
28
32
  })
33
+ end
34
+
35
+ it "should add a method to access nested resource" do
36
+ expect(@child.parent.name).to eq 'John Smith'
37
+ expect(@child.parent.to_json).to eq({name: 'John Smith'}.to_json)
38
+ end
29
39
 
30
- expect(model.make.name).to eq 'Volkswagen'
31
- expect(model.make.to_json).to eq({name: 'Volkswagen'}.to_json)
40
+ it "should pick the right class for the instantiation of chilren" do
41
+ expect(@child.parent.is_parent?).to be_truthy
32
42
  end
33
43
  end
34
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.18
4
+ version: 0.8.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Santoro
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-27 00:00:00.000000000 Z
12
+ date: 2014-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler