Skip to content

How to authorize relationships when creating a new resource? #56

@valscion

Description

@valscion

In the new resource case, we don't yet have a record available in the Pundit policies as all we've got is the class.

user_1 = User.new(id: 'user-1').save!
comment_1 = Comment.new(id: 'c-1', content: 'Is this the real life?').save!
comment_2 = Comment.new(id: 'c-2', content: 'Is this just fantasy?').save!

POST /articles

{
  "type": "articles",
  "relationships": {
    "author": {
      "data": {
        "type": "users",
        "id": "user-1"
      }
    },
    "comments": {
      "data": [
        { "type": "comments", "id": "c-1" },
        { "type": "comments", "id": "c-2" }
      ]
    }
  }
}

This will need to be addressed somehow... I'm afraid that we don't actually have any methods we could use for this case, though, as all of the relationship operation methods expect that we already have a record instance! Damn.

What should we call these methods in this case? (NOTE: The names here are arbitrary, they are just meant for a conversion starter)

  1. ArticlePolicy#create?
  2. ArticlePolicy#create_with_user?(user_1)
  3. ArticlePolicy#create_with_comments?([comment_1, comment_2])

This discussion was splitted off from #51

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions