Skip to content

Commit

Permalink
Merge pull request ryanb#625 from rogercampos/merging
Browse files Browse the repository at this point in the history
Adding Ability#merge
  • Loading branch information
ryanb committed May 28, 2012
2 parents 0c21831 + 7797b37 commit b3f9ffe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/cancan/ability.rb
Expand Up @@ -228,6 +228,13 @@ def has_raw_sql?(action, subject)
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)
Expand Down
13 changes: 13 additions & 0 deletions spec/cancan/ability_spec.rb
Expand Up @@ -432,4 +432,17 @@ class Container < Hash; end
@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

0 comments on commit b3f9ffe

Please sign in to comment.