Pundit: || is not recognized as replacement for "or" operator.

Created on 13 Mar 2018  路  4Comments  路  Source: varvet/pundit

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.

Most helpful comment

@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.

All 4 comments

@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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabrielecirulli picture gabrielecirulli  路  4Comments

epinault picture epinault  路  6Comments

vmcilwain picture vmcilwain  路  5Comments

chevinbrown picture chevinbrown  路  4Comments

dowi picture dowi  路  4Comments