Class: Ability
- Inherits:
-
Object
- Object
- Ability
- Includes:
- CanCan::Ability
- Defined in:
- app/models/ability.rb
Overview
Implementation class for Cancan gem. Instead of overriding this class, consider adding new permissions using the special register_ability
method which allows extensions to add their own abilities.
See github.com/ryanb/cancan for more details on cancan.
Class Method Summary collapse
-
.register_ability(ability) ⇒ Object
Allows us to go beyond the standard cancan initialize method which makes it difficult for engines to modify the default
Ability
of an application.
Instance Method Summary collapse
-
#initialize(user) ⇒ Ability
constructor
A new instance of Ability.
Constructor Details
#initialize(user) ⇒ Ability
Returns a new instance of Ability.
19 20 21 22 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/ability.rb', line 19 def initialize(user) self.clear_aliased_actions # override cancan default aliasing (we don't want to differentiate between read and index) alias_action :edit, :to => :update alias_action :new, :to => :create alias_action :new_action, :to => :create alias_action :show, :to => :read user ||= User.new if user.has_role? 'admin' can :manage, :all else ############################# can :read, User do |resource| resource == user end can :update, User do |resource| resource == user end can :create, User ############################# can :read, Order do |order, token| order.user == user || order.token && token == order.token end can :update, Order do |order, token| order.user == user || order.token && token == order.token end can :create, Order ############################# can :read, Product can :index, Product ############################# can :read, Taxon can :index, Taxon ############################# end #include any abilities registered by extensions, etc. Ability.abilities.each do |clazz| ability = clazz.send(:new, user) @rules = rules + ability.send(:rules) end end |
Class Method Details
.register_ability(ability) ⇒ Object
Allows us to go beyond the standard cancan initialize method which makes it difficult for engines to modify the default Ability
of an application. The ability
argument must be a class that includes the CanCan::Ability
module. The registered ability should behave properly as a stand-alone class and therefore should be easy to test in isolation.
15 16 17 |
# File 'app/models/ability.rb', line 15 def self.register_ability(ability) self.abilities.add(ability) end |