Skip to content

Commit c160242

Browse files
committed
WIP Update for jsonapi-resources v0.10
Note this will break with earlier versions of JR
1 parent 0433692 commit c160242

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
source 'https://rubygems.org'
22

3+
# ToDo: This is only for testing purposes
4+
gem 'jsonapi-resources', :git => 'https://github.com/cerebris/jsonapi-resources.git', :branch => 'track_join_options'
5+
36
gemspec

jsonapi-authorization.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
1717
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
1818
spec.require_paths = ["lib"]
1919

20-
spec.add_dependency "jsonapi-resources", "~> 0.9.0"
20+
spec.add_dependency "jsonapi-resources", "~> 0.10.0"
2121
spec.add_dependency "pundit", ">= 1.0.0", "< 3.0.0"
2222

2323
spec.add_development_dependency "appraisal"

lib/jsonapi/authorization/authorizing_processor.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def authorize_show_relationship
7272
related_resource =
7373
case relationship
7474
when JSONAPI::Relationship::ToOne
75-
parent_resource.public_send(params[:relationship_type].to_sym)
75+
resources_from_relationship(source_klass, source_id, relationship.type, context).first
7676
when JSONAPI::Relationship::ToMany
7777
# Do nothing — already covered by policy scopes
7878
else
@@ -91,7 +91,7 @@ def authorize_show_related_resource
9191

9292
source_resource = source_klass.find_by_key(source_id, context: context)
9393

94-
related_resource = source_resource.public_send(relationship_type)
94+
related_resource = resources_from_relationship(source_klass, source_id, relationship_type, context).first
9595

9696
source_record = source_resource._model
9797
related_record = related_resource._model unless related_resource.nil?
@@ -282,6 +282,14 @@ def authorizer
282282
@authorizer ||= ::JSONAPI::Authorization.configuration.authorizer.new(context: context)
283283
end
284284

285+
def resources_from_relationship(source_klass, source_id, relationship_type, context)
286+
rid = source_klass.find_related_fragments([JSONAPI::ResourceIdentity.new(source_klass, source_id)],
287+
relationship_type,
288+
context: context).keys.first
289+
290+
rid.resource_klass.find_to_populate_by_keys(rid.id)
291+
end
292+
285293
# TODO: Communicate with upstream to fix this nasty hack
286294
def operation_resource_id
287295
case operation_type

lib/jsonapi/authorization/pundit_scoped_resource.rb

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,19 @@ module PunditScopedResource
88
module ClassMethods
99
def records(options = {})
1010
user_context = JSONAPI::Authorization.configuration.user_context(options[:context])
11-
::Pundit.policy_scope!(user_context, _model_class)
11+
::Pundit.policy_scope!(user_context, super)
1212
end
13-
end
14-
15-
def records_for(association_name)
16-
record_or_records = @model.public_send(association_name)
17-
relationship = fetch_relationship(association_name)
1813

19-
case relationship
20-
when JSONAPI::Relationship::ToOne
21-
record_or_records
22-
when JSONAPI::Relationship::ToMany
23-
user_context = JSONAPI::Authorization.configuration.user_context(context)
24-
::Pundit.policy_scope!(user_context, record_or_records)
25-
else
26-
raise "Unknown relationship type #{relationship.inspect}"
27-
end
28-
end
29-
30-
private
31-
32-
def fetch_relationship(association_name)
33-
relationships = self.class._relationships.select do |_k, v|
34-
v.relation_name(context: context) == association_name
35-
end
36-
if relationships.empty?
37-
nil
38-
else
39-
relationships.values.first
14+
def apply_joins(records, join_manager, options)
15+
records = super
16+
join_manager.join_details.each do |k, v|
17+
next if k == '' || v[:join_type] == :root
18+
v[:join_options][:relationship_details][:resource_klasses].each_key do |klass|
19+
next unless klass.included_modules.include?(PunditScopedResource)
20+
records = records.where(v[:alias] => { klass._primary_key => klass.records(options)})
21+
end
22+
end
23+
records
4024
end
4125
end
4226
end

0 commit comments

Comments
 (0)