Created
April 19, 2018 13:59
-
-
Save davydovanton/de13e221b8d12ff58be35a7b8a8dc84b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Post | |
class AnonymousAbilities | |
include Kan::Abilities | |
role(:anonymous) do |user, _| | |
user.id.nil? | |
end | |
register(:read) { [] } | |
end | |
class BaseAbilities | |
include Kan::Abilities | |
role(:all) do |_, _| | |
!!user.id | |
end | |
register(:read) { |user, repo| repo.all_with_limit(user) } | |
end | |
class PayedUserAbilities | |
include Kan::Abilities | |
role(:author) do |user, post| | |
!!user.id and user.payed | |
end | |
register(:read) { |user, repo| repo.all(user) } | |
end | |
end | |
abilities = Kan::Application.new( | |
post: [Post::AnonymousAbilities.new, Post::BaseAbilities.new, Post::PayedUserAbilities.new] | |
) | |
abilities['post.read'].call(anonymous, post) # => [] | |
abilities['post.read'].call(regular, post) # => [5 items] | |
abilities['post.read'].call(payed, post) # => [all items] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment