Skip to content

Instantly share code, notes, and snippets.

@fresh5447
Created May 5, 2014 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fresh5447/0521ed10d30fc3ce632f to your computer and use it in GitHub Desktop.
Save fresh5447/0521ed10d30fc3ce632f to your computer and use it in GitHub Desktop.
WikiPolicy
class WikiPolicy < ApplicationPolicy
class Scope < Struct.new(:user, :scope)
    def resolve
      if user.admin?
        scope.all
      else
        scope.where(user: user)
      end
    end
  end
def index?
    if user.role?(:admin)
        true
    elsif user.role?(:premium)
        wiki.user == user || wiki.collaborators.map{|collab| collab.user}.include?(user) || !wiki.private?
        true
    else
       :private => false
    end
  end
  
  def update?
    index?
  end
  def edit?
    index?
  end
  def create?
    user.present?
  end
  def show?
    wiki.private? ? update? : true
  end
  def destroy?
    update?
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment