Module: JSONAPI::Authorization::PunditScopedResource

Extended by:
ActiveSupport::Concern
Defined in:
lib/jsonapi/authorization/pundit_scoped_resource.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#records_for(association_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jsonapi/authorization/pundit_scoped_resource.rb', line 15

def records_for(association_name)
  record_or_records = @model.public_send(association_name)
  relationship = fetch_relationship(association_name)

  case relationship
  when JSONAPI::Relationship::ToOne
    record_or_records
  when JSONAPI::Relationship::ToMany
    user_context = JSONAPI::Authorization.configuration.user_context(context)
    ::Pundit.policy_scope!(user_context, record_or_records)
  else
    raise "Unknown relationship type #{relationship.inspect}"
  end
end