From e8f5f2c236a648029e47cc89caa58e87826c3726 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Thu, 31 Aug 2017 13:28:13 +0300 Subject: [PATCH 1/2] Fix context not being passed properly in PunditScopedResource --- lib/jsonapi/authorization/pundit_scoped_resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jsonapi/authorization/pundit_scoped_resource.rb b/lib/jsonapi/authorization/pundit_scoped_resource.rb index 18cd49c4..eb5e3a03 100644 --- a/lib/jsonapi/authorization/pundit_scoped_resource.rb +++ b/lib/jsonapi/authorization/pundit_scoped_resource.rb @@ -31,7 +31,7 @@ def records_for(association_name) def fetch_relationship(association_name) relationships = self.class._relationships.select do |_k, v| - v.relation_name({}) == association_name + v.relation_name(context) == association_name end if relationships.empty? nil From 8b4779445946ae4a59bac32aaed4d6452cc40a39 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Thu, 31 Aug 2017 14:01:14 +0300 Subject: [PATCH 2/2] Always pass a hash with :context key to relation_name accessor This seems to be the way jsonapi-resources does it. Although, at some points JR passes way more information down the chain. Unfortunately, that same information is not available in PunditScopedResource so I'll just go with what we can get in all the places. https://github.com/cerebris/jsonapi-resources/blob/5af1f17068b66b0a17b7b69e5db334c640f6f34b/lib/jsonapi/processor.rb#L170-L184 --- lib/jsonapi/authorization/authorizing_processor.rb | 4 ++-- lib/jsonapi/authorization/pundit_scoped_resource.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/jsonapi/authorization/authorizing_processor.rb b/lib/jsonapi/authorization/authorizing_processor.rb index 8df82a96..7b062176 100644 --- a/lib/jsonapi/authorization/authorizing_processor.rb +++ b/lib/jsonapi/authorization/authorizing_processor.rb @@ -347,7 +347,7 @@ def authorize_include_item(resource_klass, source_record, include_item) next_resource_klass = relationship.resource_klass Array.wrap( source_record.public_send( - relationship.relation_name(context) + relationship.relation_name(context: context) ) ).each do |next_source_record| deep.each do |next_include_item| @@ -364,7 +364,7 @@ def authorize_include_item(resource_klass, source_record, include_item) case relationship when JSONAPI::Relationship::ToOne related_record = source_record.public_send( - relationship.relation_name(context) + relationship.relation_name(context: context) ) return if related_record.nil? authorizer.include_has_one_resource(source_record, related_record) diff --git a/lib/jsonapi/authorization/pundit_scoped_resource.rb b/lib/jsonapi/authorization/pundit_scoped_resource.rb index eb5e3a03..5166eadd 100644 --- a/lib/jsonapi/authorization/pundit_scoped_resource.rb +++ b/lib/jsonapi/authorization/pundit_scoped_resource.rb @@ -31,7 +31,7 @@ def records_for(association_name) def fetch_relationship(association_name) relationships = self.class._relationships.select do |_k, v| - v.relation_name(context) == association_name + v.relation_name(context: context) == association_name end if relationships.empty? nil