Skip to content

Instantly share code, notes, and snippets.

@davydovanton
Created April 19, 2018 13:59
Show Gist options
  • Save davydovanton/de13e221b8d12ff58be35a7b8a8dc84b to your computer and use it in GitHub Desktop.
Save davydovanton/de13e221b8d12ff58be35a7b8a8dc84b to your computer and use it in GitHub Desktop.
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