spyke 1.8.5 → 1.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/spyke/associations.rb +11 -5
- data/lib/spyke/associations/association.rb +2 -2
- data/lib/spyke/associations/builder.rb +35 -0
- data/lib/spyke/attribute_assignment.rb +1 -2
- data/lib/spyke/relation.rb +2 -2
- data/lib/spyke/version.rb +1 -1
- data/test/associations_test.rb +19 -0
- data/test/relation_test.rb +6 -8
- data/test/support/fixtures.rb +5 -0
- data/test/test_helper.rb +4 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21babce7ddc3d045a984d9efc4501ac4b811f497
|
4
|
+
data.tar.gz: bb6a369931ec88d716aed6fe2831928ac9f42e60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c298b3f6569a9a0367129b54b76d3b1ccc042b3ddc0f829c1f0ef279666aaa84e576b3427fc63006cc5cf542ebda1e38a4ce890f6c177a031b4d0a63c3664d3e
|
7
|
+
data.tar.gz: bfd3b3b44721671a22be2c9732917b495d710a4504d4eccd892c88b81790f1370a4e6f73092f6e3afe9a89ae5a692af2dd50b1ee8dadab3a3d8af9b5e4caddf8
|
data/lib/spyke/associations.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spyke/associations/association'
|
2
|
+
require 'spyke/associations/builder'
|
2
3
|
require 'spyke/associations/has_many'
|
3
4
|
require 'spyke/associations/has_one'
|
4
5
|
require 'spyke/associations/belongs_to'
|
@@ -14,7 +15,7 @@ module Spyke
|
|
14
15
|
|
15
16
|
module ClassMethods
|
16
17
|
def has_many(name, options = {})
|
17
|
-
|
18
|
+
create_association(name, HasMany, options)
|
18
19
|
|
19
20
|
define_method "#{name.to_s.singularize}_ids=" do |ids|
|
20
21
|
attributes[name] = []
|
@@ -27,7 +28,7 @@ module Spyke
|
|
27
28
|
end
|
28
29
|
|
29
30
|
def has_one(name, options = {})
|
30
|
-
|
31
|
+
create_association(name, HasOne, options)
|
31
32
|
|
32
33
|
define_method "build_#{name}" do |attributes = nil|
|
33
34
|
association(name).build(attributes)
|
@@ -35,7 +36,7 @@ module Spyke
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def belongs_to(name, options = {})
|
38
|
-
|
39
|
+
create_association(name, BelongsTo, options)
|
39
40
|
|
40
41
|
define_method "build_#{name}" do |attributes = nil|
|
41
42
|
association(name).build(attributes)
|
@@ -54,9 +55,14 @@ module Spyke
|
|
54
55
|
|
55
56
|
def reflect_on_association(name)
|
56
57
|
# Just enough to support nested_form gem
|
57
|
-
|
58
|
-
Association.new(nil, name, assoc)
|
58
|
+
associations[name] || associations[name.to_s.pluralize.to_sym]
|
59
59
|
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def create_association(name, type, options)
|
64
|
+
self.associations = associations.merge(name => Builder.new(self, name, type, options))
|
65
|
+
end
|
60
66
|
end
|
61
67
|
end
|
62
68
|
end
|
@@ -6,8 +6,8 @@ module Spyke
|
|
6
6
|
class Association < Relation
|
7
7
|
attr_reader :parent, :name
|
8
8
|
|
9
|
-
def initialize(parent, name, options = {})
|
10
|
-
super
|
9
|
+
def initialize(klass, parent, name, options = {})
|
10
|
+
super(klass, options)
|
11
11
|
@parent, @name = parent, name
|
12
12
|
end
|
13
13
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Spyke
|
2
|
+
module Associations
|
3
|
+
class Builder
|
4
|
+
def initialize(parent_class, name, type, options = {})
|
5
|
+
@parent_class, @name, @type, @options = parent_class, name, type, options
|
6
|
+
end
|
7
|
+
|
8
|
+
def build(parent)
|
9
|
+
@type.new(klass, parent, @name, @options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def klass
|
13
|
+
@klass ||= compute_class(@options[:class_name] || @name)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# https://github.com/rails/rails/blob/70ac072976c8cc6f013f0df3777e54ccae3f4f8c/activerecord/lib/active_record/inheritance.rb#L132-L150
|
19
|
+
def compute_class(type_name)
|
20
|
+
parent_name = @parent_class.to_s
|
21
|
+
type_name = type_name.to_s.classify
|
22
|
+
|
23
|
+
candidates = []
|
24
|
+
parent_name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
|
25
|
+
candidates << type_name
|
26
|
+
|
27
|
+
candidates.each do |candidate|
|
28
|
+
constant = ActiveSupport::Dependencies.safe_constantize(candidate)
|
29
|
+
return constant if candidate == constant.to_s
|
30
|
+
end
|
31
|
+
raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/spyke/relation.rb
CHANGED
@@ -4,8 +4,8 @@ module Spyke
|
|
4
4
|
class Relation
|
5
5
|
include Enumerable
|
6
6
|
|
7
|
-
attr_reader :klass
|
8
|
-
|
7
|
+
attr_reader :klass
|
8
|
+
attr_accessor :params
|
9
9
|
delegate :to_ary, :[], :any?, :empty?, :last, :size, :metadata, to: :find_some
|
10
10
|
|
11
11
|
def initialize(klass, options = {})
|
data/lib/spyke/version.rb
CHANGED
data/test/associations_test.rb
CHANGED
@@ -370,5 +370,24 @@ module Spyke
|
|
370
370
|
assert_equal %w{ Condiments Tools }, recipe.groups.map(&:name)
|
371
371
|
assert_equal %w{ Salt Spoon }, recipe.ingredients.map(&:name)
|
372
372
|
end
|
373
|
+
|
374
|
+
def test_namespaced_model
|
375
|
+
tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
|
376
|
+
likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
|
377
|
+
Cookbook::Tip.find(1).likes.first
|
378
|
+
assert_requested tip_endpoint
|
379
|
+
assert_requested likes_endpoint
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_namespaced_association_class_auto_detect
|
383
|
+
favorite = Cookbook::Tip.new.favorites.build
|
384
|
+
assert_equal Cookbook::Favorite, favorite.class
|
385
|
+
end
|
386
|
+
|
387
|
+
def test_raising_exception_if_class_not_found
|
388
|
+
assert_raises NameError do
|
389
|
+
Cookbook::Tip.new.votes
|
390
|
+
end
|
391
|
+
end
|
373
392
|
end
|
374
393
|
end
|
data/test/relation_test.rb
CHANGED
@@ -124,18 +124,16 @@ module Spyke
|
|
124
124
|
assert_requested endpoint_2, times: 1
|
125
125
|
end
|
126
126
|
|
127
|
-
def test_namespaced_model
|
128
|
-
tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
|
129
|
-
likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
|
130
|
-
Cookbook::Tip.find(1).likes.first
|
131
|
-
assert_requested tip_endpoint
|
132
|
-
assert_requested likes_endpoint
|
133
|
-
end
|
134
|
-
|
135
127
|
def test_path_validation
|
136
128
|
assert_raises Spyke::InvalidPathError do
|
137
129
|
Recipe.new.groups.to_a
|
138
130
|
end
|
139
131
|
end
|
132
|
+
|
133
|
+
def test_raise_no_method_error
|
134
|
+
assert_raises NoMethodError do
|
135
|
+
Recipe.new.groups.unknown_method
|
136
|
+
end
|
137
|
+
end
|
140
138
|
end
|
141
139
|
end
|
data/test/support/fixtures.rb
CHANGED
@@ -94,8 +94,13 @@ module Cookbook
|
|
94
94
|
class Tip < Spyke::Base
|
95
95
|
uri '/tips/(:id)'
|
96
96
|
has_many :likes, class_name: 'Cookbook::Like', uri: '/tips/:cookbook_tip_id/likes/(:id)'
|
97
|
+
has_many :favorites
|
98
|
+
has_many :votes
|
97
99
|
end
|
98
100
|
|
99
101
|
class Like < Spyke::Base
|
100
102
|
end
|
103
|
+
|
104
|
+
class Favorite < Spyke::Base
|
105
|
+
end
|
101
106
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spyke
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Balvig
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -268,6 +268,7 @@ files:
|
|
268
268
|
- lib/spyke/associations.rb
|
269
269
|
- lib/spyke/associations/association.rb
|
270
270
|
- lib/spyke/associations/belongs_to.rb
|
271
|
+
- lib/spyke/associations/builder.rb
|
271
272
|
- lib/spyke/associations/has_many.rb
|
272
273
|
- lib/spyke/associations/has_one.rb
|
273
274
|
- lib/spyke/attribute_assignment.rb
|