From 7797b37c7e70a4299c144a0da5dbc3b347416715 Mon Sep 17 00:00:00 2001 From: Roger Campos Date: Mon, 31 Oct 2011 14:08:50 +0100 Subject: [PATCH] Adding Ability#merge --- lib/cancan/ability.rb | 7 +++++++ spec/cancan/ability_spec.rb | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index 5f86877..2b597e1 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -228,6 +228,13 @@ module CanCan relevant_rules(action, subject).any?(&:only_raw_sql?) end + def merge(ability) + ability.send(:rules).each do |rule| + rules << rule.dup + end + self + end + private def unauthorized_message_keys(action, subject) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 48b6782..3551cce 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -416,4 +416,17 @@ describe CanCan::Ability do @ability.unauthorized_message(:edit, 1..3).should == "edit range" end end + + describe "#merge" do + it "should add the rules from the given ability" do + @ability.can :use, :tools + another_ability = Object.new + another_ability.extend(CanCan::Ability) + another_ability.can :use, :search + + @ability.merge(another_ability) + @ability.can?(:use, :search).should be_true + @ability.send(:rules).size.should == 2 + end + end end