Class: Samurai::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/samurai/ability.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Ability

Returns a new instance of Ability.



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
64
65
# File 'app/models/samurai/ability.rb', line 24

def initialize(user)
  Rails.logger.info self.abilities.inspect

  if user.admin?
    can :manage, :all
  else
    can :read, :dashboard
  end

  # Include any abilities registered by extensions, etc.
  Ability.abilities.each do |klass|
    ability = klass.send(:new, user)
    @rules = rules + ability.send(:rules)
  end

  # Define abilities for the passed in user here. For example:
  #
  #   user ||= User.new # guest user (not logged in)
  #   if user.admin?
  #     can :manage, :all
  #   else
  #     can :read, :all
  #   end
  #
  # The first argument to `can` is the action you are giving the user
  # permission to do.
  # If you pass :manage it will apply to every action. Other common actions
  # here are :read, :create, :update and :destroy.
  #
  # The second argument is the resource the user can perform the action on.
  # If you pass :all it will apply to every resource. Otherwise pass a Ruby
  # class of the resource.
  #
  # The third argument is an optional hash of conditions to further filter the
  # objects.
  # For example, here the user can only update published articles.
  #
  #   can :update, Article, :published => true
  #
  # See the wiki for details:
  # https://github.com/ryanb/cancan/wiki/Defining-Abilities
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 Samurai::Ability of an application. The registered ability should behave properly as a stand-alone class and therefore should be easy to test in isolation.

Parameters:

  • ability (Ability)

    a class that includes the CanCan::Ability module.



14
15
16
# File 'app/models/samurai/ability.rb', line 14

def self.register_ability(ability)
  self.abilities.add(ability)
end

.remove_ability(ability) ⇒ Object

Remove a registered ability.

Parameters:

  • ability (Ability)

    a class that includes the CanCan::Ability module.



20
21
22
# File 'app/models/samurai/ability.rb', line 20

def self.remove_ability(ability)
 self.abilities.delete(ability)
end