Skip to content

JR lamda relation_name gets broken  #80

@Justin-Maxwell

Description

@Justin-Maxwell

The ability to use context-driven relation names seems to be broken, because the context argument isn't passed as required to JSONAPI::Relationship.relation_name when invoked from within jsonapi-authorization.

relation_name - the name of the relation to use on the model. A lambda may be provided which allows conditional selection of the relation based on the context.

When JR invokes the lambda, the received argument looks something like this:
{:context=>{:current_user=>#<User id: 14, ... ...>, :demo_mode=>true, :action=>"show"}, :include_directives=>#<JSONAPI::IncludeDirectives:0x007f6b5d043290 @resource_klass=API::V1::SomeResource, @force_eager_load=false, @include_directives_hash={:include_related=>{:some_included=>{:include=>true, :include_related=>{}, :include_in_join=>true}}}>, :fields=>{}}

But when invoked from JSONAPI::Authorization::AuthorizingProcessor.authorize_include_item at authorize_include_item(resource_klass, source_record, rel_name), rather than passing the full context object, the lambda receives:
{:current_user=>#<Tibbing::User id: 14, ...>, :demo_mode=>true, :action=>"show"}

Which, since the context is at least present, could be cludged around, but unfortunately, it is then invoked from JSONAPI::Authorization::PunditScopedResource.fetch_relationship via v.relation_name({}) so we get {}, there is no way for the lamda to return the correct relation_name, and it errors out, disabling this functionality.

In our context, the demo_mode context boolean needs to 'pick' the appropriate result of a to_one relationship. I'm going to have another crack at over-riding records_for_ in the resource, but it would be good to ensure that the JR context is always provided to .relation_name, and test that lambda relation_names work 'as advertised' by JR.

I just wish I had the time to work on tweaking the gem and send through a pull-request, rather than just adding problems ... :-(

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions