caprese 0.3.17 → 0.3.18

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7d3ec5ca008e336953160f8bb5c6963e22b8dc3
4
- data.tar.gz: 422237c49d75ac0a80636da14932e3c0bbb4a166
3
+ metadata.gz: e196ca099f2b8170091ceede2c96fe511d119013
4
+ data.tar.gz: 3122835a8dfc2d64de36f3407fcc2f71ee8f527e
5
5
  SHA512:
6
- metadata.gz: 791201a9973930c32d47f7bd86e7a0b267c574073318fe28d3739fa24ae3a66ceebf583269df590efbeba1131bbd679ee8f85f4043f74f5fa4b248a8d9ed6a27
7
- data.tar.gz: ff6240cccd8715607851fbc7a6147186d70f53e43d464f010b8a33763ad176e1bd2935af2b4d2a98628e112dbce26380cbb9cb80d3bd9379ad3c9b86f021856c
6
+ metadata.gz: 9f0d0d402f302b4dae304c02b9f0b5825839de58057f7941113370d4408597de0310b0630a9c3cbf485fab67963e9d7d21ec277722b98629547b2897948260de
7
+ data.tar.gz: 379c742b2ef7dc13e313781df63db218c2613e1ba5886975139a0066069d1ea90c013b3c8c585fd5e73d18b1c91bd1ba77fd026e7c3afc9350843e8c421d057c
data/CHANGELOG.md CHANGED
@@ -91,3 +91,7 @@
91
91
  ## 0.3.17
92
92
 
93
93
  * Update rendering concern serializer_for to resemble Serializer.serializer_for
94
+
95
+ ## 0.3.18
96
+
97
+ * Allow serialized data links to use parent class links
@@ -12,19 +12,25 @@ module Caprese
12
12
  # object = Order<@token='asd27h'>
13
13
  # links = { self: '/api/v1/orders/asd27hß' }
14
14
  link :self do
15
- if Rails.application.routes.url_helpers
16
- .respond_to?(url = serializer.version_name("#{object.class.name.underscore}_url"))
17
-
15
+ if(url = serializer.class.route_for(object))
18
16
  Rails.application.routes.url_helpers.send(
19
17
  url,
20
18
  object.read_attribute(Caprese.config.resource_primary_key),
21
- host: serializer.send(:caprese_default_url_options_host)
19
+ host: serializer.class.send(:caprese_default_url_options_host)
22
20
  )
23
21
  end
24
22
  end
25
23
  end
26
24
 
27
25
  module ClassMethods
26
+ # Fetches the host from Caprese.config.default_url_options or fails if it is not set
27
+ # @note default_url_options[:host] is used to render the host in links that are serialized in the response
28
+ def caprese_default_url_options_host
29
+ Caprese.config.default_url_options.fetch(:host) do
30
+ fail 'Caprese requires that config.default_url_options[:host] be set when rendering links.'
31
+ end
32
+ end
33
+
28
34
  # Overridden so we can define relationship links without any code in a specific
29
35
  # serializer
30
36
  def has_many(name, options = {}, &block)
@@ -82,7 +88,7 @@ module Caprese
82
88
  url,
83
89
  id: object.read_attribute(primary_key),
84
90
  relationship: reflection_name,
85
- host: serializer.send(:caprese_default_url_options_host)
91
+ host: serializer.class.send(:caprese_default_url_options_host)
86
92
  )
87
93
  end
88
94
  end
@@ -94,7 +100,7 @@ module Caprese
94
100
  url,
95
101
  id: object.read_attribute(primary_key),
96
102
  relationship: reflection_name,
97
- host: serializer.send(:caprese_default_url_options_host)
103
+ host: serializer.class.send(:caprese_default_url_options_host)
98
104
  )
99
105
  end
100
106
  end
@@ -103,16 +109,6 @@ module Caprese
103
109
  end
104
110
  end
105
111
  end
106
-
107
- private
108
-
109
- # Fetches the host from Caprese.config.default_url_options or fails if it is not set
110
- # @note default_url_options[:host] is used to render the host in links that are serialized in the response
111
- def caprese_default_url_options_host
112
- Caprese.config.default_url_options.fetch(:host) do
113
- fail 'Caprese requires that config.default_url_options[:host] be set when rendering links.'
114
- end
115
- end
116
112
  end
117
113
  end
118
114
  end
@@ -8,7 +8,7 @@ module Caprese
8
8
  module ClassMethods
9
9
  # Gets a versioned serializer for a given record
10
10
  #
11
- # @note Overrides the default since the default does not do namespaced lookup
11
+ # @note Overrides the AMS default since the default does not do namespaced lookup
12
12
  #
13
13
  # @param [ActiveRecord::Base] record the record to get a serializer for
14
14
  # @param [Hash] options options for `super` to use when getting the serializer
@@ -33,6 +33,18 @@ module Caprese
33
33
  true
34
34
  end
35
35
 
36
+ # Gets a versioned route for a given record
37
+ #
38
+ # @param [ActiveRecord::Base] record the record to get a route for
39
+ # @return [String,Nil] the route for the given record
40
+ def route_for(record)
41
+ return nil unless record
42
+
43
+ get_route_for(record.class)
44
+ end
45
+
46
+ # TODO: Add route_for_relationship
47
+
36
48
  private
37
49
 
38
50
  # Gets a serializer for a klass, either as the serializer explicitly defined
@@ -47,6 +59,23 @@ module Caprese
47
59
  get_serializer_for(klass.superclass) if klass.superclass
48
60
  end
49
61
  end
62
+
63
+ # Gets a route for a klass, either as the serializer explicitly defined
64
+ # for this class, or as a route defined for one of the klass's parents
65
+ #
66
+ # @param [Class] klass the klass to get the serializer for
67
+ # @return [String] the route for the class
68
+ def get_route_for(klass)
69
+ output = nil
70
+ while klass.superclass do
71
+ if Rails.application.routes.url_helpers.respond_to?(url = version_name("#{klass.name.underscore}_url"))
72
+ output = url
73
+ break
74
+ end
75
+ klass = klass.superclass
76
+ end
77
+ output
78
+ end
50
79
  end
51
80
  end
52
81
  end
@@ -1,3 +1,3 @@
1
1
  module Caprese
2
- VERSION = '0.3.17'
2
+ VERSION = '0.3.18'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caprese
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.17
4
+ version: 0.3.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Landgrebe