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 +4 -4
- data/lib/restful_resource/associations.rb +10 -4
- data/lib/restful_resource/version.rb +1 -1
- data/spec/fixtures.rb +20 -0
- data/spec/restful_resource/associations_spec.rb +27 -17
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee2cd0cbeb47f73c4698b263fdb12f0fd9f8ba31
|
4
|
+
data.tar.gz: c7bee8e65111360e37d50b6e1a930a8ffa0c07ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
5
|
-
|
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
|
-
|
14
|
-
|
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))
|
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
|
-
|
6
|
-
|
7
|
-
name: '
|
8
|
-
|
5
|
+
before :each do
|
6
|
+
@parent = ComplicatedModule::Parent.new({
|
7
|
+
name: 'John Doe',
|
8
|
+
children:
|
9
9
|
[
|
10
|
-
{
|
11
|
-
{
|
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
|
-
|
16
|
-
expect(
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
31
|
-
expect(
|
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.
|
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-
|
12
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|