diff --git a/lib/jsonapi/routing_ext.rb b/lib/jsonapi/routing_ext.rb index 5aae3537e..13ff8a86f 100644 --- a/lib/jsonapi/routing_ext.rb +++ b/lib/jsonapi/routing_ext.rb @@ -44,7 +44,7 @@ def jsonapi_resource(*resources, &_block) options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy') end - resource @resource_type, options do + resource @resource_type, **options do # :nocov: if @scope.respond_to? :[]= # Rails 4 @@ -57,7 +57,7 @@ def jsonapi_resource(*resources, &_block) end else # Rails 5 - jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do + jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], **options), @resource_type) do if block_given? yield else @@ -119,7 +119,7 @@ def jsonapi_resources(*resources, &_block) options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy') end - resources @resource_type, options do + resources @resource_type, **options do # :nocov: if @scope.respond_to? :[]= # Rails 4 @@ -131,7 +131,7 @@ def jsonapi_resources(*resources, &_block) end else # Rails 5 - jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do + jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], **options), @resource_type) do if block_given? yield else diff --git a/test/unit/routing_compatibility_test.rb b/test/unit/routing_compatibility_test.rb new file mode 100644 index 000000000..6ba466fd5 --- /dev/null +++ b/test/unit/routing_compatibility_test.rb @@ -0,0 +1,38 @@ +require 'logger' +require 'minitest/autorun' +require 'active_support/all' +require 'rails' +require 'action_controller' +require 'jsonapi-resources' + +class RoutingCompatibilityResource < JSONAPI::Resource +end + +class RoutingProfileResource < JSONAPI::Resource + singleton singleton_key: -> (context) { context[:current_user_id] } +end + +class RoutingCompatibilityTest < Minitest::Test + def test_plural_routes_preserve_keyword_options + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + jsonapi_resources :routing_compatibilities, controller: 'examples', only: [:index, :show] + end + + assert_equal '/routing-compatibilities', routes.url_helpers.routing_compatibilities_path + assert_equal '/routing-compatibilities/42', routes.url_helpers.routing_compatibility_path(42) + assert_equal ['examples'], routes.routes.map { |route| route.defaults[:controller] }.uniq + refute_includes routes.routes.map(&:verb), 'DELETE' + end + + def test_singleton_routes_preserve_keyword_options + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + jsonapi_resource :routing_profile, controller: 'profiles', only: [:show] + end + + assert_equal '/routing-profile', routes.url_helpers.routing_profile_path + assert_equal ['profiles'], routes.routes.map { |route| route.defaults[:controller] }.uniq + refute_includes routes.routes.map(&:verb), 'DELETE' + end +end