lib/jsonapi/routing_ext.rb in jsonapi-resources-0.9.6 vs lib/jsonapi/routing_ext.rb in jsonapi-resources-0.9.7
- old
+ new
@@ -18,10 +18,14 @@
def jsonapi_resource(*resources, &_block)
@resource_type = resources.first
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))
+ unless res.singleton?
+ warn "Singleton routes created for non singleton resource #{res}. Links may not be generated correctly."
+ end
+
options = resources.extract_options!.dup
options[:controller] ||= @resource_type
options.merge!(res.routing_resource_options)
options[:path] = format_route(@resource_type)
@@ -78,10 +82,14 @@
def jsonapi_resources(*resources, &_block)
@resource_type = resources.first
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))
+ if res.singleton?
+ warn "Singleton resource #{res} should use `jsonapi_resource` instead."
+ end
+
options = resources.extract_options!.dup
options[:controller] ||= @resource_type
options.merge!(res.routing_resource_options)
options[:param] = :id
@@ -151,23 +159,27 @@
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]
+ match "relationships/#{formatted_relationship_name}",
+ controller: options[:controller],
+ action: 'show_relationship', relationship: link_type.to_s, via: [:get],
+ as: "relationships/#{link_type}"
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]
+ 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]
+ 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)
@@ -180,27 +192,28 @@
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]
+ action: 'show_relationship', relationship: link_type.to_s, via: [:get],
+ as: "relationships/#{link_type}"
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]
+ 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]
+ 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]
+ action: 'destroy_relationship', relationship: link_type.to_s, via: [:delete]
end
end
end
def jsonapi_related_resource(*relationship)
@@ -217,13 +230,14 @@
else
related_resource = JSONAPI::Resource.resource_for(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: 'get_related_resource', via: [:get]
+ match formatted_relationship_name, controller: options[:controller],
+ relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
+ action: 'get_related_resource', via: [:get],
+ as: relationship_name
end
def jsonapi_related_resources(*relationship)
source = JSONAPI::Resource.resource_for(resource_type_with_module_prefix)
options = relationship.extract_options!.dup
@@ -233,12 +247,14 @@
formatted_relationship_name = format_route(relationship.name)
related_resource = JSONAPI::Resource.resource_for(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: 'get_related_resources', via: [:get]
+ match formatted_relationship_name,
+ controller: options[:controller],
+ relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
+ action: 'get_related_resources', via: [:get],
+ as: relationship_name
end
protected
# :nocov:
def jsonapi_resource_scope(resource, resource_type) #:nodoc: