Class: Spree::Ability

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, options = {}) ⇒ Ability

Returns a new instance of Ability.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/spree/ability.rb', line 41

def initialize(user, options = {})
  alias_cancan_delete_action

  @user = user || Spree.user_class.new
  @store = options[:store] || Spree::Current.store

  apply_permissions_from_sets

  # Include any abilities registered by extensions, etc.
  # this is legacy behaviour and should be removed in Spree 5.0
  Ability.abilities.merge(abilities_to_register).each do |clazz|
    merge clazz.new(@user)
  end
end

Instance Attribute Details

#storeSpree::Store? (readonly)

Returns the current store.

Returns:



27
28
29
# File 'app/models/spree/ability.rb', line 27

def store
  @store
end

#userObject (readonly)

Returns the current user.

Returns:

  • (Object)

    the current user



24
25
26
# File 'app/models/spree/ability.rb', line 24

def user
  @user
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.



33
34
35
# File 'app/models/spree/ability.rb', line 33

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

.remove_ability(ability) ⇒ Object



37
38
39
# File 'app/models/spree/ability.rb', line 37

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