Class: Decidim::Abilities::BaseAbility
- Inherits:
-
Object
- Object
- Decidim::Abilities::BaseAbility
- Includes:
- CanCan::Ability
- Defined in:
- app/models/decidim/abilities/base_ability.rb
Overview
Defines the abilities for a User. Intended to be used with ‘cancancan`.
Instance Method Summary collapse
-
#initialize(user, context = {}) ⇒ BaseAbility
constructor
Initializes the ability class for the given user.
Constructor Details
#initialize(user, context = {}) ⇒ BaseAbility
Initializes the ability class for the given user. Automatically merges injected abilities fmor the configuration. In order to inject more abilities, add this code in the ‘engine.rb` file of your own engine, for example, inside an initializer:
Decidim.configure do |config|
config.abilities << Decidim::MyEngine::Abilities::MyAbility
end
Note that, in development, this will force you to restart the server every time you change things in your ability classes.
user - the User that needs its abilities checked. context - a Hash with some context related to the current request.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/decidim/abilities/base_ability.rb', line 23 def initialize(user, context = {}) Decidim.abilities.each do |ability| merge ability.constantize.new(user, context) end can :create, Authorization do || .user == user && not_already_active?(user, ) end can :update, Authorization do || .user == user && !.granted? end can :manage, Follow do |follow| follow.user == user end can :manage, Notification do |notification| notification.user == user end can :manage, Messaging::Conversation do |conversation| conversation.participants.include?(user) end end |