Skip to content

Conversation

@jpalumickas
Copy link
Contributor

This PR extends #54 with added tests.

@valscion valscion self-assigned this May 29, 2017
@valscion
Copy link
Member

Ok, now I see what's happening. Thanks for the PR!

Do you think it would be better if it were somehow possible to tell the authorizer that the relationship is about to get emptied? What if the expected authorizer call would look like this instead?

allow_operation(
  'replace_fields',
  article,
  [{
    relation_name: :author,
    relation_type: :to_one,
    records: nil
  }]
)

These kind of relationship specs currently reside in a slightly badly named file spec/requests/tricky_operations_spec.rb. Do you think you could move the added spec there, and maybe structure it like so?

describe 'PATCH /articles/:id (nullifying to-one relationship)' do
  # spec code that is similar to the
  # 'PATCH /articles/:id (mass-modifying relationships)' spec code
  # ...
end

Would you also be open to change the behavior to match that?

Thanks for your contribution! If you want me to open up something more, feel free to ask me anything ☺️

@jpalumickas
Copy link
Contributor Author

@valscion
Moved tests to tricky operations.

Do you think it would be better if it were somehow possible to tell the authorizer that the relationship is about to get emptied? What if the expected authorizer call would look like this instead?

For this one I had error, so I left as it is for now:

#<InstanceDouble(JSONAPI::Authorization::DefaultPunditAuthorizer) (anonymous)> received :replace_fields with unexpected arguments
         expected: (#<Article id: 1012824740, external_id: "3", author_id: 676971205, blank_value: nil>, [{:relation_name=>:author, :relation_type=>:to_one, :records=>nil}])
              got: (#<Article id: 1012824740, external_id: "3", author_id: 676971205, blank_value: nil>, [])

@valscion
Copy link
Member

valscion commented May 30, 2017

For this one I had error, so I left as it is for now:

Ah, but actually that error highlights the behavior that we would actually want to have. We would want to allow the authorizer to have a way of handling this situation.

However, in the DefaultPunditAuthorizer, we might want to actually call remove_to_one_relationship when the records is nil so that the policy method that would end up being called would be "remove_#{relationship_type}?".

Do you want to go ahead and do these changes? That would mean these changes:

Does this sound reasonable to you?

data[rel_type].flat_map do |assoc_name, assoc_value|
case assoc_value
when nil
next
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to change the def related_models method at all. Your bug fix should only be necessary to touch the related_models_with_context method.


before do
allow_any_instance_of(UserPolicy::Scope).to receive(:resolve)
.and_return(user_policy_scope)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec would actually pass even if we didn't handle UserPolicy::Scope at all, so I guess these user_policy_scope things could be removed.

@jpalumickas
Copy link
Contributor Author

@valscion Fixed 😉

@valscion
Copy link
Member

Wow, looking great! Thanks for the fast turn-around 💞

Are you OK with me adding you to the list of contributors in the README?

Copy link
Member

@valscion valscion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jpalumickas
Copy link
Contributor Author

Sure, cheers ! 😉

@valscion valscion merged commit 95c9693 into venuu:master May 30, 2017
@valscion
Copy link
Member

I'll release alpha4 version after I've had lunch. Thanks for your contributions!

@jpalumickas jpalumickas deleted the fix/nullify-relationship-from-resource branch May 30, 2017 08:23
@jpalumickas
Copy link
Contributor Author

Thanks!

@valscion
Copy link
Member

Released v1.0.0.alpha4 with this change :)

@jpalumickas
Copy link
Contributor Author

Perfect! thanks for the release 🚀

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants