Releases: davydovanton/kan
Releases · davydovanton/kan
v0.4.0
v0.3.0
- Allow to use callable objects as a role objects (@davydovanton) #20
- Allow to use classes as a role objects (@davydovanton) #18
- Add
#permit
matcher for rspec specs (@berniechiu) #15
v0.2.0
We're happy to announce 0.2
version of kan.
Role system
Now kan support to write simple role based system. For this you need to define role block in each abilities classes:
module Post
class AnonymousAbilities
include Kan::Abilities
role :anonymous do |user, _|
user.id.nil?
end
register(:read, :edit, :delete) { false }
end
class BaseAbilities
include Kan::Abilities
role :all do |_, _|
true
end
register(:read) { |_, _| true }
register(:edit, :delete) { |user, post| false }
end
class AuthorAbilities
include Kan::Abilities
role :author do |user, post|
user.id == post.author_id
end
register(:read, :edit) { |_, _| true }
register(:delete) { |_, _| false }
end
class AdminAbilities
include Kan::Abilities
role :admin do |user, _|
user.admin?
end
register :read, :edit, :delete { |_, _| true }
end
end
After that initialize Kan application object and call it with payload:
abilities = Kan::Application.new(
post: [Post::AnonymousAbilities.new, Post::BaseAbilities.new, Post::AuthorAbilities.new, Post::AdminAbilities.new],
comment: Comments::Abilities.new
)
abilities['post.read'].call(anonymous, post) # => false
abilities['post.read'].call(regular, post) # => true
abilities['post.read'].call(author, post) # => true
abilities['post.read'].call(admin, post) # => true
abilities['post.edit'].call(anonymous, post) # => false
abilities['post.edit'].call(regular, post) # => false
abilities['post.edit'].call(author, post) # => true
abilities['post.edit'].call(admin, post) # => true
abilities['post.delete'].call(anonymous, post) # => false
abilities['post.delete'].call(regular, post) # => false
abilities['post.delete'].call(author, post) # => false
abilities['post.delete'].call(admin, post) # => true
Logger support
Now you can use any logger instances in your abilities and roles:
class AnonymousAbilities
include Kan::Abilities
register(:read, :edit, :delete) do
logger.info 'Anonymous ability checked'
false
end
end
And initialize your abilities instances with custom logger:
abilities = Kan::Application.new(
comment: Comments::Abilities.new(logger: MyCustomLogger.new)
)
Credits
Thanks all developers why was helped with new release:
- @apotonick
- @etxzay
- @HolyWalley
- @RomanSerikov
- @valikos
❤️