Skip to content

Instantly share code, notes, and snippets.

@midhunkrishna
Created October 8, 2015 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midhunkrishna/f21f93ccab7a01423e68 to your computer and use it in GitHub Desktop.
Save midhunkrishna/f21f93ccab7a01423e68 to your computer and use it in GitHub Desktop.
filter_criterion.md
  class FileUploadPolicy < ApplicationPolicy
  def create?
    can_create = false
    FileUploadPolicy.creator_roles.each do |role|
      can_create = can_create || @user.send("#{role}?".to_sym)
    end
    can_create
  end

  def self.creator_roles
    [User::SUPER_ADMIN_ROLE, User::CATEGORY_MANAGER_ROLE, User::CATALOGUE_MANAGER_ROLE]
  end
end
class User < AB
  
def self.uploaders
    uploader_roles= FileUploadPolicy.creator_roles

    matching_role_query = uploader_roles.map do |role|
      "(roles like '%#{role}%')"
    end.join(' or ')
    
    self.where(matching_role_query)
  end  
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment