Skip to content

Use plural of resource type for controller#988

Open
hidde-jan wants to merge 2 commits intoJSONAPI-Resources:masterfrom
hidde-jan:patch-1
Open

Use plural of resource type for controller#988
hidde-jan wants to merge 2 commits intoJSONAPI-Resources:masterfrom
hidde-jan:patch-1

Conversation

@hidde-jan
Copy link
Copy Markdown
Contributor

Currently, doing jsonapi_resource :current_user will use the CurrentUserController, however, any relationships on the current_user resource will instead look for the CurrentUsersController. By default, rails uses the plural name controller for any singular resource. Keeping this the same for JA:R seems like a good idea.

This is currently not picked up by the tests currently, since Preferences is already a plural.

Currently, doing `jsonapi_resource :current_user` will use the `CurrentUserController`, however, any relationships on the `current_user` resource will instead look for the `CurrentUsersController`. By default, rails uses the plural name controller for any singular resource. Keeping this the same for JA:R seems like a good idea.
@hidde-jan
Copy link
Copy Markdown
Contributor Author

hidde-jan commented Feb 22, 2017

Actually, relationships for singular resources also seem broken (at least in 0.8.3). If I GET a related resource of a current_user (say, events), then I get the following error:

{
    "errors": [
        {
            "code": "PARAM_MISSING",
            "detail": "The required parameter, current_user_id, is missing.",
            "status": "400",
            "title": "Missing Parameter"
        }
    ]
}

Edit: I did some more checking. The only way this can currently be worked around is to trick the RequestParser into thinking a valid key was passed, i.e. by overriding CurrentUserResource._as_parent_key and returning :source, then overriding verify_key:

class CurrentUserResource < JSONAPI::Resource
  def self._as_parent_key
    :source
   end

  def self.verify_key(key, context = {})
    if key == "current_users" && context[:current_user]
      context[:current_user].id
    else
      super
    end
  end
end

Edit 2: :source also doesn't work, because it is not used for show_relationship. The only way I can make it work is by setting _as_parent_key to :action and then just always returning the id of the current_user in the context.

@amiel
Copy link
Copy Markdown

amiel commented May 27, 2017

I'm running against this issue as well. Thanks for the details :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants