diff --git a/README.md b/README.md index 656b819ac..8179a63c7 100644 --- a/README.md +++ b/README.md @@ -781,7 +781,15 @@ edit_contact GET /contacts/:id/edit(.:format) contacts#edit ``` To manually add in the nested routes you can use the `jsonapi_links`, `jsonapi_related_resources` and -`jsonapi_related_resource` inside the block. +`jsonapi_related_resource` inside the block. Or, you can add the default set of nested routes using the `jsonapi_relationships` method. For example: + +```ruby +Rails.application.routes.draw do + jsonapi_resources :contacts do + jsonapi_relationships + end +end +``` ###### `jsonapi_links` diff --git a/lib/jsonapi/request.rb b/lib/jsonapi/request.rb index db9989c3c..2de491f0b 100644 --- a/lib/jsonapi/request.rb +++ b/lib/jsonapi/request.rb @@ -400,7 +400,7 @@ def parse_single_replace_operation(data, keys) end type = data[:type] - if type.nil? || type != format_key(@resource_klass._type).to_s + if type.nil? || type != @resource_klass._type.to_s raise JSONAPI::Exceptions::ParameterMissing.new(:type) end diff --git a/lib/jsonapi/routing_ext.rb b/lib/jsonapi/routing_ext.rb index 09e7ce391..f7941cbd3 100644 --- a/lib/jsonapi/routing_ext.rb +++ b/lib/jsonapi/routing_ext.rb @@ -18,59 +18,58 @@ def format_route(route) end def jsonapi_resource(*resources, &block) - resource_type = resources.first - res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(resources.first)) + @resource_type = resources.first + res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type)) options = resources.extract_options!.dup - options[:controller] ||= resource_type + options[:controller] ||= @resource_type options.merge!(res.routing_resource_options) - options[:path] = format_route(resource_type) + options[:path] = format_route(@resource_type) - resource resource_type, options do - @scope[:jsonapi_resource] = resource_type + resource @resource_type, options do + @scope[:jsonapi_resource] = @resource_type if block_given? yield else - res._associations.each do |association_name, association| - if association.is_a?(JSONAPI::Association::HasMany) - jsonapi_links(association_name) - else - jsonapi_link(association_name) - end - end + jsonapi_relationships + end + end + end + + def jsonapi_relationships + res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type)) + res._associations.each do |association_name, association| + if association.is_a?(JSONAPI::Association::HasMany) + jsonapi_links(association_name) + jsonapi_related_resources(association_name) + else + jsonapi_link(association_name) + jsonapi_related_resource(association_name) end end end def jsonapi_resources(*resources, &block) - resource_type = resources.first - res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(resources.first)) + @resource_type = resources.first + res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type)) options = resources.extract_options!.dup - options[:controller] ||= resource_type + options[:controller] ||= @resource_type options.merge!(res.routing_resource_options) # Route using the primary_key. Can be overridden using routing_resource_options options[:param] ||= res._primary_key - options[:path] = format_route(resource_type) + options[:path] = format_route(@resource_type) - resources resource_type, options do - @scope[:jsonapi_resource] = resource_type + resources @resource_type, options do + @scope[:jsonapi_resource] = @resource_type if block_given? yield else - res._associations.each do |association_name, association| - if association.is_a?(JSONAPI::Association::HasMany) - jsonapi_links(association_name) - jsonapi_related_resources(association_name) - else - jsonapi_link(association_name) - jsonapi_related_resource(association_name) - end - end + jsonapi_relationships end end end diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 697b57ccb..d2b4af2eb 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -755,4 +755,4 @@ class BadlyNamedAttributesResource < JSONAPI::Resource betax = Planet.create(name: 'Beta X', description: 'Newly discovered Planet X', planet_type_id: unknown.id) betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y', planet_type_id: unknown.id) betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id) -betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W') \ No newline at end of file +betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W') diff --git a/test/test_helper.rb b/test/test_helper.rb index b2ca32e53..b84abb03f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -66,7 +66,9 @@ def as_json(options = nil) jsonapi_resources :people jsonapi_resources :comments jsonapi_resources :tags - jsonapi_resources :posts + jsonapi_resources :posts do + jsonapi_relationships + end jsonapi_resources :sections jsonapi_resources :iso_currencies jsonapi_resources :expense_entries