Skip to content

Commit

Permalink
Merge alias actions when merging abilities (#538)
Browse files Browse the repository at this point in the history
Closes #468
Closes #280

This changes the behaviour of `Ability#merge` to also include defined
`alias_actions`.

If a collision between the defined aliases occurs the actions on +self+
will be overwritten with those on the passed object.

The inline documentation has been updated in order to reflect those
changes.
  • Loading branch information
Jcambass authored and coorasse committed Jan 26, 2019
1 parent b3752fb commit 4eecb08
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/cancan/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,13 @@ def has_raw_sql?(action, subject)
relevant_rules(action, subject).any?(&:only_raw_sql?)
end

# Copies all rules of the given +CanCan::Ability+ and adds them to +self+.
# 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
#
Expand All @@ -216,18 +217,43 @@ def has_raw_sql?(action, subject)
#
# 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]]
def merge(ability)
ability.rules.each do |rule|
add_rule(rule.dup)
end
@aliased_actions = aliased_actions.merge(ability.aliased_actions)
self
end

Expand Down
29 changes: 29 additions & 0 deletions spec/cancan/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,35 @@ class Container < Hash
expect(@ability.send(:rules).size).to eq(2)
end

it 'adds the aliased actions from the given ability' do
@ability.alias_action :show, to: :see
(another_ability = double).extend(CanCan::Ability)
another_ability.alias_action :create, :update, to: :manage

@ability.merge(another_ability)
expect(@ability.aliased_actions).to eq(
read: %i[index show],
create: %i[new],
update: %i[edit],
manage: %i[create update],
see: %i[show]
)
end

it 'overwrittes the aliased actions with the value from the given ability' do
@ability.alias_action :show, :index, to: :see
(another_ability = double).extend(CanCan::Ability)
another_ability.alias_action :show, to: :see

@ability.merge(another_ability)
expect(@ability.aliased_actions).to eq(
read: %i[index show],
create: %i[new],
update: %i[edit],
see: %i[show]
)
end

it 'can add an empty ability' do
(another_ability = double).extend(CanCan::Ability)

Expand Down

0 comments on commit 4eecb08

Please sign in to comment.