Method: CanCan::Ability#merge

Defined in:
lib/cancan/ability.rb

#merge(ability) ⇒ Object

Copies all rules and aliased actions of the given CanCan::Ability and adds them to self.

class ReadAbility
  include CanCan::Ability

  def initialize
    can :read, User
    alias_action :show, :index, to: :see
  end
end

class WritingAbility
  include CanCan::Ability

  def initialize
    can :edit, User
    alias_action :create, :update, to: :modify
  end
end

read_ability = ReadAbility.new
read_ability.can? :edit, User.new #=> false
read_ability.merge(WritingAbility.new)
read_ability.can? :edit, User.new #=> true
read_ability.aliased_actions #=> [:see => [:show, :index], :modify => [:create, :update]]

If there are collisions when merging the aliased_actions, the actions on self will be overwritten.

class ReadAbility

include CanCan::Ability

def initialize
  alias_action :show, :index, to: :see
end

end

class ShowAbility

include CanCan::Ability

def initialize
  alias_action :show, to: :see
end

end

read_ability = ReadAbility.new read_ability.merge(ShowAbility) read_ability.aliased_actions #=> [:see => [:show]]



248
249
250
251
252
253
254
# File 'lib/cancan/ability.rb', line 248

def merge(ability)
  ability.rules.each do |rule|
    add_rule(rule.dup)
  end
  @aliased_actions = aliased_actions.merge(ability.aliased_actions)
  self
end