I combined answers from above and came up with the following:
class ApplicationPolicy
attr_reader :user
def initialize(user)
@user = user
end
def self.permit(roles, options)
return if options[:to].none?
options[:to].each do |action|
define_method("#{action}?") do
return @user.roles? Array.wrap(roles) if options[:when].blank?
send(options[:when]) and @user.roles? Array.wrap(roles)
end
end
end
end
which allows one to use it like this:
class CommentPolicy < ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@record = record
super(user)
end
permit %i[admin member], to: %i[show edit destroy update], when: :created_by_user
def created_by_user
@record.user == @user
end
end
and
permit :admin, to: %i[index update edit]
works as well
my roles method from user
model looks like:
def roles?(user_roles)
user_roles.each do |role|
return true if role?(role)
end
false
end
def role?(role)
roles.any? { |r| r.name.underscore.to_sym == role }
end
before_filter
and call it on all the above methods – Wyatt