Skip to content

Instantly share code, notes, and snippets.

@pavel-sazonov
Created August 5, 2018 16:32
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 pavel-sazonov/afe9cb6f86132a5c59406fe5dfb76145 to your computer and use it in GitHub Desktop.
Save pavel-sazonov/afe9cb6f86132a5c59406fe5dfb76145 to your computer and use it in GitHub Desktop.
class Ability
include CanCan::Ability
def initialize(user)
if user
can :create, [Question, Answer, Attachment, Comment]
can %i[update destroy], [Question, Answer], user_id: user.id
can :destroy, Comment, user_id: user.id
can :destroy, Attachment, attachable: { user_id: user.id }
can :set_best, Answer, question: { user_id: user.id }
can %i[vote_up vote_down], Vote, votable: { user_id: user.id }
# can %i[vote_up vote_down], [Question, Answer] do |votable|
# !user.author_of?(votable)# && votable.voted_by(user).empty?
# votable.user_id != user.id
# end
can :cancel_vote, [Question, Answer], user_id: user.id
end
can :read, :all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment