I'm seeing this kind of error:
Invalid #<GroupPolicy::Scope> constructor is called
but it's very strange because removing the or() method seems to bypass the error - but I need this method to find Groups that belong to a particular user AND find Groups that a particular person can edit, and return that set to the index page. The user in this case is a PORO populated with employee data from an AREST service. I don't think I'm using the method incorrectly because the error thrown is related to Pundit.
Application Policy:
class ApplicationPolicy
attr_reader :context, :record
def initialize(context, record)
@context = context
@record = record
end
delegate :user, to: :context
delegate :cookies, to: :context
...
class Scope
attr_reader :context, :scope
def initialize(context, scope)
@context = context
@scope = scope
end
delegate :user, to: :context
delegate :cookies, to: :context
def resolve
scope.all
end
end
end
Group Policy:
class GroupPolicy < ApplicationPolicy
...
class Scope < Scope
def resolve
if user.admin?
super
else
scope.joins(:group_editors).where(group_editors: { editor_id: user.id })
# the above line works, but it doesn't work when I try:
# scope.joins(:group_editors).where(group_editors: { editor_id: user.id }).or(scope.where(owner_id: user.id))
# both queries succeed individually if I print them each on a new line
end
end
end
end
Group Model:
class Group < ApplicationRecord
has_many :roles
has_many :group_editors
has_many :contents
end
Editors Model:
class GroupEditor < ApplicationRecord
belongs_to :group
end
Groups Controller (index):
def index
@groups = policy_scope(Group)
redirect_to group_path @groups.first if @groups.count == 1
end
Pundit User (uses additional context because we read cookies when doing auth sometimes):
def pundit_user
UserContext.new(current_user, cookies)
end
class UserContext
attr_reader :user, :cookies
def initialize(user, cookies)
@user = user
@cookies = cookies
end
end
Edit #1
This seems to work - but why?
scope.joins(:group_editors).where('owner_id = ? OR group_editors.editor_id = ?', user.id, user.id)
Edit #2
The real error is
Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
which is unfortunate. Pundit throws a misleading error.