-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathrouting_ext.rb
269 lines (224 loc) · 10.5 KB
/
routing_ext.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
module ActionDispatch
module Routing
class Mapper
Resource.class_eval do
def unformat_route(route)
JSONAPI.configuration.route_formatter.unformat(route.to_s)
end
def nested_param
:"#{unformat_route(singular)}_#{param}"
end
end
Resources.class_eval do
def format_route(route)
JSONAPI.configuration.route_formatter.format(route.to_s)
end
def jsonapi_resource(*resources, &_block)
@resource_type = resources.first
res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))
options = resources.extract_options!.dup
options[:controller] ||= @resource_type
options.merge!(res.routing_resource_options)
options[:path] = format_route(@resource_type)
if options[:except]
options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new')
options[:except] << :edit unless options[:except].include?(:edit) || options[:except].include?('edit')
else
options[:except] = [:new, :edit]
end
if res._immutable
options[:except] << :create unless options[:except].include?(:create) || options[:except].include?('create')
options[:except] << :update unless options[:except].include?(:update) || options[:except].include?('update')
options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy')
end
resource @resource_type, options do
# :nocov:
if @scope.respond_to? :[]=
# Rails 4
@scope[:jsonapi_resource] = @resource_type
if block_given?
yield
else
jsonapi_relationships
end
else
# Rails 5
jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
if block_given?
yield
else
jsonapi_relationships
end
end
end
# :nocov:
end
end
def jsonapi_relationships(options = {})
res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))
res._relationships.each do |relationship_name, relationship|
if relationship.is_a?(JSONAPI::Relationship::ToMany)
jsonapi_links(relationship_name, options)
jsonapi_related_resources(relationship_name, options)
else
jsonapi_link(relationship_name, options)
jsonapi_related_resource(relationship_name, options)
end
end
end
def jsonapi_resources(*resources, &_block)
@resource_type = resources.first
res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))
options = resources.extract_options!.dup
options[:controller] ||= @resource_type
options.merge!(res.routing_resource_options)
options[:param] = :id
options[:path] = format_route(@resource_type)
if res.resource_key_type == :uuid
options[:constraints] ||= {}
options[:constraints][:id] ||= /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/
end
if options[:except]
options[:except] = Array(options[:except])
options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new')
options[:except] << :edit unless options[:except].include?(:edit) || options[:except].include?('edit')
else
options[:except] = [:new, :edit]
end
if res._immutable
options[:except] << :create unless options[:except].include?(:create) || options[:except].include?('create')
options[:except] << :update unless options[:except].include?(:update) || options[:except].include?('update')
options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy')
end
resources @resource_type, options do
# :nocov:
if @scope.respond_to? :[]=
# Rails 4
@scope[:jsonapi_resource] = @resource_type
if block_given?
yield
else
jsonapi_relationships
end
else
# Rails 5
jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
if block_given?
yield
else
jsonapi_relationships
end
end
end
# :nocov:
end
end
def links_methods(options)
default_methods = [:show, :create, :destroy, :update]
if only = options[:only]
Array(only).map(&:to_sym)
elsif except = options[:except]
default_methods - Array(except)
else
default_methods
end
end
def jsonapi_link(*links)
link_type = links.first
formatted_relationship_name = format_route(link_type)
options = links.extract_options!.dup
res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix)
options[:controller] ||= res._type.to_s
methods = links_methods(options)
if methods.include?(:show)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'show_relationship', relationship: link_type.to_s, via: [:get]
end
if res.mutable?
if methods.include?(:update)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'update_relationship', relationship: link_type.to_s, via: [:put, :patch]
end
if methods.include?(:destroy)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'destroy_relationship', relationship: link_type.to_s, via: [:delete]
end
end
end
def jsonapi_links(*links)
link_type = links.first
formatted_relationship_name = format_route(link_type)
options = links.extract_options!.dup
res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix)
options[:controller] ||= res._type.to_s
methods = links_methods(options)
if methods.include?(:show)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'show_relationship', relationship: link_type.to_s, via: [:get]
end
if res.mutable?
if methods.include?(:create)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'create_relationship', relationship: link_type.to_s, via: [:post]
end
if methods.include?(:update)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'update_relationship', relationship: link_type.to_s, via: [:put, :patch]
end
if methods.include?(:destroy)
match "relationships/#{formatted_relationship_name}", controller: options[:controller],
action: 'destroy_relationship', relationship: link_type.to_s, via: [:delete]
end
end
end
def jsonapi_related_resource(*relationship)
source = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix)
options = relationship.extract_options!.dup
relationship_name = relationship.first
relationship = source._relationships[relationship_name]
formatted_relationship_name = format_route(relationship.name)
if relationship.polymorphic?
options[:controller] ||= relationship.class_name.underscore.pluralize
else
related_resource = JSONAPI::Resource.resource_klass_for(related_resource_type_with_module_prefix(relationship.class_name.underscore.pluralize))
options[:controller] ||= related_resource._type.to_s
end
match formatted_relationship_name, controller: options[:controller],
relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
action: 'show_related_resource', via: [:get]
end
def jsonapi_related_resources(*relationship)
source = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix)
options = relationship.extract_options!.dup
relationship_name = relationship.first
relationship = source._relationships[relationship_name]
formatted_relationship_name = format_route(relationship.name)
related_resource = JSONAPI::Resource.resource_klass_for(related_resource_type_with_module_prefix(relationship.class_name.underscore))
options[:controller] ||= related_resource._type.to_s
match formatted_relationship_name,
controller: options[:controller],
relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
action: 'index_related_resources', via: [:get]
end
protected
# :nocov:
def jsonapi_resource_scope(resource, resource_type) #:nodoc:
@scope = @scope.new(scope_level_resource: resource, jsonapi_resource: resource_type)
controller(resource.resource_scope) { yield }
ensure
@scope = @scope.parent
end
# :nocov:
private
def resource_type_with_module_prefix(resource = nil)
resource_name = resource || @scope[:jsonapi_resource]
[@scope[:module], resource_name].compact.collect(&:to_s).join('/')
end
def related_resource_type_with_module_prefix(resource = nil)
resource_name = resource || @scope[:jsonapi_resource]
[JSONAPI::Resource.root_path_for_path(@scope[:module]), resource_name].compact.collect(&:to_s).join('/')
end
end
end
end
end