Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

supporting deeply nested aliases - closes #98

  • Loading branch information...
commit cad425989e31b0e3f6012ac97daaaf172c9f7065 1 parent 1b4377c
@ryanb authored
View
2  CHANGELOG.rdoc
@@ -1,3 +1,5 @@
+* Supporting deeply nested aliases - see issue #98
+
1.2.0 (July 16, 2010)
* Load nested parent resources on collection actions such as "index" (thanks dohzya)
View
11 lib/cancan/ability.rb
@@ -212,6 +212,15 @@ def association_joins(action, subject)
end
private
+
+ # Accepts a hash of aliased actions and returns an array of actions which match.
+ # This should be called before "matches?" and other checking methods since they
+ # rely on the actions to be expanded.
+ def expand_actions(actions)
+ actions.map do |action|
+ aliased_actions[action] ? [action, *expand_actions(aliased_actions[action])] : action
+ end.flatten
+ end
def can_definitions
@can_definitions ||= []
@@ -219,7 +228,7 @@ def can_definitions
def matching_can_definition(action, subject)
can_definitions.reverse.detect do |can_definition|
- can_definition.expand_actions(aliased_actions)
+ can_definition.expanded_actions = expand_actions(can_definition.actions)
can_definition.matches? action, subject
end
end
View
11 lib/cancan/can_definition.rb
@@ -5,6 +5,8 @@ module CanCan
class CanDefinition # :nodoc:
include ActiveSupport::Inflector
attr_reader :block
+ attr_reader :actions
+ attr_writer :expanded_actions
# The first argument when initializing is the base_behavior which is a true/false
# value. True for "can" and false for "cannot". The next two arguments are the action
@@ -18,15 +20,6 @@ def initialize(base_behavior, action, subject, conditions, block)
@block = block
end
- # Accepts a hash of aliased actions and returns an array of actions which match.
- # This should be called before "matches?" and other checking methods since they
- # rely on the actions to be expanded.
- def expand_actions(aliased_actions)
- @expanded_actions = @actions.map do |action|
- aliased_actions[action] ? [action, *aliased_actions[action]] : action
- end.flatten
- end
-
def matches?(action, subject)
matches_action?(action) && matches_subject?(subject)
end
View
7 spec/cancan/ability_spec.rb
@@ -54,6 +54,13 @@
@ability.can?(:destroy, 123).should == :modify_called
end
+ it "should allow deeply nested aliased actions" do
+ @ability.alias_action :increment, :to => :sort
+ @ability.alias_action :sort, :to => :modify
+ @ability.can :modify, :all
+ @ability.can?(:increment, 123).should be_true
+ end
+
it "should return block result for action, object_class, and object for any action" do
@ability.can :manage, :all do |action, object_class, object|
[action, object_class, object]
View
1  spec/spec.opts
@@ -0,0 +1 @@
+--color
Please sign in to comment.
Something went wrong with that request. Please try again.