With Devise, Rolify and Pundit on Rails 5.2, I am running into the issue where when I use the || operator instead of using or it does not validate the second part of the or statement.
Example (show method):
class UserPolicy
attr_reader :current_user, :model
def initialize(current_user, model)
@current_user = current_user
@user = model
end
def index?
@current_user.has_role? :admin
end
def show?
@current_user.has_role? :admin || @current_user == @user
end
def update?
@current_user.has_role? :admin or @current_user == @user
end
...
end
In this example, the update method works, but the show method does not.
@richjdsmith I don't think this is related to the gem:
Check:
@pablocrivella is right.
The line you wrote
@current_user.has_role? :admin || @current_user == @user
is interpreted by ruby as
@current_user.has_role?(:admin || @current_user == @user)
That's why it looks like the second part of the condition is never called.
@richjdsmith Could you close this issue since this indeed is not related to the gem?
Sorry about that, I missed this in my notifications!
I will close this as that makes perfect sense.
Cheers all for your help!
Most helpful comment
@pablocrivella is right.
The line you wrote
is interpreted by ruby as
That's why it looks like the second part of the condition is never called.