Permalink
Browse files

replace Rule conditions attr_reader with a getter that execute the va…

…lues that are a callable Object
  • Loading branch information...
timoschilling committed Nov 5, 2012
1 parent 6768035 commit d3c73c4a98001061a080773725acb213273af982
Showing with 17 additions and 1 deletion.
  1. +17 −1 lib/cancan/rule.rb
View
@@ -3,7 +3,7 @@ module CanCan
# it holds the information about a "can" call made on Ability and provides
# helpful methods to determine permission checking and conditions hash generation.
class Rule # :nodoc:
- attr_reader :base_behavior, :subjects, :actions, :conditions
+ attr_reader :base_behavior, :subjects, :actions
attr_writer :expanded_actions
# The first argument when initializing is the base_behavior which is a true/false
@@ -20,6 +20,22 @@ def initialize(base_behavior, action, subject, conditions, block)
@block = block
end
+ # Process the conditions that are a Proc or a callable Object
+ def conditions
+ case @conditions
+ when Array
+ @conditions.map do |condition|
+ condition.respond_to?(:call) ? condition.call : condition
+ end
+ when Hash
+ @conditions.each_with_object({}) do |(key, condition), processed_conditions|
+ processed_conditions[key] = condition.respond_to?(:call) ? condition.call : condition
+ end
+ else
+ @conditions
+ end
+ end
+
# Matches both the subject and action, not necessarily the conditions
def relevant?(action, subject)
subject = subject.values.first if subject.class == Hash

0 comments on commit d3c73c4

Please sign in to comment.