caprese 0.3.24 → 0.3.25

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: 5ab60011a9ebcc4df7f6db15aa5fddaf4d45e17e
4
- data.tar.gz: 1fc4b7e07dd201f6630cf313c0a8b509b02ee2ed
3
+ metadata.gz: 34252b10961eccda9b87897794f97016566a8e2d
4
+ data.tar.gz: f8b6e715a0cbbdbd9b71a81ef864872c09ab8aaa
5
5
  SHA512:
6
- metadata.gz: b9c5bd9541636bc733733ae8d1f06be612e73f37b665a784d2ed69b0c9640c134e60c1629a867711e8ce104306348c2c289bc3baffe5b1e25a493e3b16ee2960
7
- data.tar.gz: b7cf51285324c5647cfd5827e8089cc56e647362feccd61f0a0f36fcfd410a6d9beabb1f42aff54f0176a225ce7663568b3c97b662eebf1da48c5ad66fc7227b
6
+ metadata.gz: 289224faf57d9e726b555d8b10bf4114127f40d8469157f5320f7a30b6e5c01a7fdfeaf9802e61739beb4f6c2d966ead2fc7881c1ea99797c8e97ab2df2aecea
7
+ data.tar.gz: 525fb2eff4783c7f1c584db0ee9d4fb7ca2f0bd133661538d8ef28fb1f670898c631a965940c27b83190e86e0c29d081f4947afaa51f758de77b56cd82a33286
data/CHANGELOG.md CHANGED
@@ -136,4 +136,8 @@
136
136
 
137
137
  ## 0.3.24
138
138
 
139
- * Add `Caprese.config.isolated_namespace` to enable Caprese to work with isolate_namespace Engines
139
+ * Add `Caprese.config.isolated_namespace` to enable Caprese to work with isolate_namespace Engines by trimming the `isolated_namespace` off the front of the result of calls to `version_module`, `version_path`, `version_name`, etc.
140
+
141
+ ## 0.3.25
142
+
143
+ * Add `namespaced_module` and `unnamespace` helper to Versioning that provides full module namespace when `config.isolate_namespace` is present (replaces the old functionality of `version_module` and `unversion`)
@@ -0,0 +1,20 @@
1
+ require 'active_support/concern'
2
+
3
+ module Caprese
4
+ module UrlHelpers
5
+ extend ActiveSupport::Concern
6
+
7
+ # Returns the proper route URL helpers based on whether or not Caprese is being used in an engine's
8
+ # isolated_namespace or just a regular application
9
+ def url_helpers
10
+ mod =
11
+ if namespace = Caprese.config.isolated_namespace
12
+ namespace::Engine
13
+ else
14
+ Rails.application
15
+ end
16
+
17
+ mod.routes.url_helpers
18
+ end
19
+ end
20
+ end
@@ -4,7 +4,74 @@ module Caprese
4
4
  module Versioning
5
5
  extend ActiveSupport::Concern
6
6
 
7
+ # Get full namespaced module name of object (class)
8
+ #
9
+ # @example
10
+ # namespaced_module('OrdersController') => 'API::V1::OrdersController'
11
+ #
12
+ # @param [String] suffix the string to append, hypothetically a module in
13
+ # this namespace's module space
14
+ # @return [String] the namespaced modulized name of the suffix passed in
15
+ def namespaced_module(suffix = nil)
16
+ mod = (self.is_a?(Class) ? self : self.class).name.deconstantize
17
+
18
+ name = suffix || mod
19
+ name = name.prepend("#{mod}::") unless suffix.nil? || (/^#{mod}::\.*/).match(suffix)
20
+
21
+ name
22
+ end
23
+
24
+ # Get full namespaced path (url)
25
+ #
26
+ # @example
27
+ # namespaced_path('orders') => 'api/v1/orders'
28
+ #
29
+ # @param [String] suffix the string to append, hypothetically an extension in
30
+ # this namespace's path space
31
+ # @return [String] the full namespaced path name of the suffix passed in
32
+ def namespaced_path(suffix = nil)
33
+ namespaced_module.downcase.split('::').push(suffix).reject(&:nil?).join('/')
34
+ end
35
+
36
+ # Get full namespaced dot path (translations)
37
+ #
38
+ # @example
39
+ # namespaced_dot_path('orders') => 'api/v1/orders'
40
+ #
41
+ # @param [String] suffix the string to append, hypothetically an extension in
42
+ # this namespace's dot space
43
+ # @return [String] the full namespaced dot path of the suffix passed in
44
+ def namespaced_dot_path(suffix = nil)
45
+ namespaced_module.downcase.split('::').push(suffix).reject(&:nil?).join('.')
46
+ end
47
+
48
+ # Get full namespaced underscore name (routes)
49
+ #
50
+ # @example
51
+ # namespaced_name('orders') => 'api_v1_orders'
52
+ #
53
+ # @param [String] suffix the string to append, hypothetically a name that
54
+ # needs to be namespaced
55
+ # @return [String] the namespaced name of the suffix passed in
56
+ def namespaced_name(suffix = nil)
57
+ namespaced_module.downcase.split('::').push(suffix).reject(&:nil?).join('_')
58
+ end
59
+
60
+ # Strips string to raw unnamespaced format regardless of input format
61
+ #
62
+ # @param [String] str the string to unversion
63
+ # @return [String] the stripped, unnamespaced name of the string passed in
64
+ def unnamespace(str)
65
+ str
66
+ .remove(namespaced_module(''))
67
+ .remove(namespaced_path(''))
68
+ .remove(namespaced_name(''))
69
+ .remove(namespaced_dot_path(''))
70
+ end
71
+
7
72
  # Get versioned module name of object (class)
73
+ # @note The difference between namespaced and versioned module names is that if an isolated_namespace
74
+ # is present, version_module will return a module name without the isolated namespace
8
75
  #
9
76
  # @example
10
77
  # version_module('OrdersController') => 'API::V1::OrdersController'
@@ -13,11 +80,11 @@ module Caprese
13
80
  # this version's module space
14
81
  # @return [String] the versioned modulized name of the suffix passed in
15
82
  def version_module(suffix = nil)
16
- name = (self.is_a?(Class) ? self : self.class).name.deconstantize
83
+ name = namespaced_module(suffix)
17
84
 
18
85
  name = name.gsub("#{Caprese.config.isolated_namespace}::", '') if Caprese.config.isolated_namespace
19
86
 
20
- name + (suffix ? "::#{suffix}" : '')
87
+ name
21
88
  end
22
89
 
23
90
  # Get versioned path (url)
@@ -45,6 +112,8 @@ module Caprese
45
112
  end
46
113
 
47
114
  # Get versioned underscore name (routes)
115
+ # @note The difference between namespaced and versioned module names is that if an isolated_namespace
116
+ # is present, version_module will return a module name without the isolated namespace
48
117
  #
49
118
  # @example
50
119
  # version_name('orders') => 'api_v1_orders'
@@ -28,7 +28,7 @@ module Caprese
28
28
  # @param [Hash] t the interpolation params to be passed into I18n.t
29
29
  def error(field: nil, code: :invalid, t: {})
30
30
  Error.new(
31
- controller: unversion(params[:controller]),
31
+ controller: unnamespace(params[:controller]),
32
32
  action: params[:action],
33
33
  field: field,
34
34
  code: code,
@@ -169,7 +169,7 @@ module Caprese
169
169
  # @return [Relation] the record scope of the queried controller
170
170
  def queried_record_scope
171
171
  unless @queried_record_scope
172
- scope = record_scope(unversion(params[:controller]).to_sym)
172
+ scope = record_scope(unnamespace(params[:controller]).to_sym)
173
173
 
174
174
  if scope.any? && query_params[:filter].try(:any?)
175
175
  if (valid_filters = query_params[:filter].select { |k, _| scope.column_names.include? actual_field(k).to_s }).present?
@@ -58,11 +58,10 @@ module Caprese
58
58
  links = { self: request.original_url }
59
59
 
60
60
  if !target.respond_to?(:to_ary) &&
61
- Rails.application.routes.url_helpers
62
- .respond_to?(related_url = version_name("#{params[:relationship].singularize}_url"))
61
+ url_helpers.respond_to?(related_url = version_name("#{params[:relationship].singularize}_url"))
63
62
 
64
63
  links[:related] =
65
- Rails.application.routes.url_helpers.send(
64
+ url_helpers.send(
66
65
  related_url,
67
66
  target.read_attribute(self.config.resource_primary_key),
68
67
  host: caprese_default_url_options_host
@@ -117,10 +116,10 @@ module Caprese
117
116
  links = { self: request.original_url }
118
117
 
119
118
  # Add related link for this relationship if it exists
120
- if Rails.application.routes.url_helpers
121
- .respond_to?(related_path = "relationship_data_#{version_name(unversion(params[:controller]).singularize)}_url")
119
+ if url_helpers
120
+ .respond_to?(related_path = "relationship_data_#{version_name(unnamespace(params[:controller]).singularize)}_url")
122
121
 
123
- links[:related] = Rails.application.routes.url_helpers.send(
122
+ links[:related] = url_helpers.send(
124
123
  related_path,
125
124
  params[:id],
126
125
  params[:relationship],
@@ -66,7 +66,7 @@ module Caprese
66
66
  # @return [Serializer] the serializer for the class
67
67
  def get_serializer_for(klass)
68
68
  begin
69
- version_module("#{klass.name}Serializer").constantize
69
+ namespaced_module("#{klass.name}Serializer").constantize
70
70
  rescue NameError => e
71
71
  get_serializer_for(klass.superclass) if klass.superclass
72
72
  end
@@ -31,7 +31,7 @@ module Caprese
31
31
  #
32
32
  # @return [Class] the record class for the current controller
33
33
  def controller_record_class
34
- record_class(unversion(params[:controller]))
34
+ record_class(unnamespace(params[:controller]))
35
35
  end
36
36
 
37
37
  # Checks if a given type mismatches the controller type
@@ -1,5 +1,6 @@
1
1
  require 'action_controller'
2
2
  require 'active_support/configurable'
3
+ require 'caprese/concerns/url_helpers'
3
4
  require 'caprese/concerns/versioning'
4
5
  require 'caprese/controller/concerns/aliasing'
5
6
  require 'caprese/controller/concerns/callbacks'
@@ -24,7 +25,9 @@ module Caprese
24
25
  include Relationships
25
26
  include Rendering
26
27
  include Typing
28
+ include UrlHelpers
27
29
  include Versioning
30
+ extend UrlHelpers
28
31
  extend Versioning
29
32
  end
30
33
  end
@@ -13,7 +13,7 @@ module Caprese
13
13
  # links = { self: '/api/v1/orders/asd27hß' }
14
14
  link :self do
15
15
  if(url = serializer.class.route_for(object))
16
- Rails.application.routes.url_helpers.send(
16
+ serializer.url_helpers.send(
17
17
  url,
18
18
  object.read_attribute(Caprese.config.resource_primary_key),
19
19
  host: serializer.class.send(:caprese_default_url_options_host)
@@ -84,9 +84,9 @@ module Caprese
84
84
 
85
85
  Proc.new do |serializer|
86
86
  link :self do
87
- url = "relationship_definition_#{serializer.version_name("#{object.class.name.underscore}_url")}"
88
- if Rails.application.routes.url_helpers.respond_to? url
89
- Rails.application.routes.url_helpers.send(
87
+ url = "relationship_definition_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
88
+ if serializer.url_helpers.respond_to? url
89
+ serializer.url_helpers.send(
90
90
  url,
91
91
  id: object.read_attribute(primary_key),
92
92
  relationship: reflection_name,
@@ -96,9 +96,9 @@ module Caprese
96
96
  end
97
97
 
98
98
  link :related do
99
- url = "relationship_data_#{serializer.version_name("#{object.class.name.underscore}_url")}"
100
- if Rails.application.routes.url_helpers.respond_to? url
101
- Rails.application.routes.url_helpers.send(
99
+ url = "relationship_data_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
100
+ if serializer.url_helpers.respond_to? url
101
+ serializer.url_helpers.send(
102
102
  url,
103
103
  id: object.read_attribute(primary_key),
104
104
  relationship: reflection_name,
@@ -54,7 +54,7 @@ module Caprese
54
54
  # @return [Serializer] the serializer for the class
55
55
  def get_serializer_for(klass)
56
56
  begin
57
- version_module("#{klass.name}Serializer").constantize
57
+ namespaced_module("#{klass.name}Serializer").constantize
58
58
  rescue NameError => e
59
59
  get_serializer_for(klass.superclass) if klass.superclass
60
60
  end
@@ -68,7 +68,7 @@ module Caprese
68
68
  def get_route_for(klass)
69
69
  output = nil
70
70
  while klass.superclass do
71
- if Rails.application.routes.url_helpers.respond_to?(url = version_name("#{klass.name.underscore}_url"))
71
+ if url_helpers.respond_to?(url = version_name("#{unnamespace(klass.name).underscore}_url"))
72
72
  output = url
73
73
  break
74
74
  end
@@ -1,4 +1,5 @@
1
1
  require 'active_model_serializers'
2
+ require 'caprese/concerns/url_helpers'
2
3
  require 'caprese/concerns/versioning'
3
4
  require 'caprese/serializer/concerns/aliasing'
4
5
  require 'caprese/serializer/concerns/links'
@@ -7,7 +8,9 @@ require 'caprese/serializer/concerns/relationships'
7
8
 
8
9
  module Caprese
9
10
  class Serializer < ActiveModel::Serializer
11
+ extend UrlHelpers
10
12
  extend Versioning
13
+ include UrlHelpers
11
14
  include Versioning
12
15
  include Aliasing
13
16
  include Links
@@ -1,3 +1,3 @@
1
1
  module Caprese
2
- VERSION = '0.3.24'
2
+ VERSION = '0.3.25'
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.24
4
+ version: 0.3.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Landgrebe
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2017-05-29 00:00:00.000000000 Z
13
+ date: 2017-05-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: active_model_serializers
@@ -222,6 +222,7 @@ files:
222
222
  - lib/caprese/adapter/json_api/pagination_links.rb
223
223
  - lib/caprese/adapter/json_api/relationship.rb
224
224
  - lib/caprese/adapter/json_api/resource_identifier.rb
225
+ - lib/caprese/concerns/url_helpers.rb
225
226
  - lib/caprese/concerns/versioning.rb
226
227
  - lib/caprese/controller.rb
227
228
  - lib/caprese/controller/concerns/aliasing.rb