Permalink
Browse files

Support for multiparameter attributes

Multiparameter attributes, like those used with datetime_select and
time_select are understood, and the parenthesized portion is stripped
when checking permissions.

For example:
  @ability.can :update, :article, :date
will allow
  @ability.can? :update, :article, 'date(1i)'
  @ability.can? :update, :article, 'date(2i)'
  @ability.can? :update, :article, 'date(3i)'
  • Loading branch information...
1 parent aed9f26 commit 1728080452c65e1ae6fbbf7ad72b0857c6d497bd @jhawthorn jhawthorn committed Aug 12, 2012
Showing with 17 additions and 0 deletions.
  1. +3 −0 lib/cancan/ability.rb
  2. +14 −0 spec/cancan/ability_spec.rb
View
@@ -306,6 +306,9 @@ def rules
# Returns an array of Rule instances which match the action and subject
# This does not take into consideration any hash conditions or block statements
def relevant_rules(action, subject, attribute = nil)
+ if attribute
+ attribute = attribute.to_s.split('(').first.to_sym
+ end
specificity = 0
rules.reverse.each_with_object([]) do |rule, relevant_rules|
rule.expanded_actions = expand_aliases(:actions, rule.actions)
@@ -224,6 +224,20 @@
@ability.can?(:update, :users, "name").should be_true
end
+ it "understands multiparameter attributes as strings" do
+ @ability.can :update, :users, :date
+ @ability.can?(:update, :users, "date(i1)").should be_true
+ @ability.can?(:update, :users, "date(i2)").should be_true
+ @ability.can?(:update, :users, "date(i3)").should be_true
+ end
+
+ it "understands multiparameter attributes as symbols" do
+ @ability.can :update, :users, :date
+ @ability.can?(:update, :users, :"date(i1)").should be_true
+ @ability.can?(:update, :users, :"date(i2)").should be_true
+ @ability.can?(:update, :users, :"date(i3)").should be_true
+ end
+
it "combines attribute check with conditions hash" do
@ability.can :update, :ranges, :begin => 1
@ability.can :update, :ranges, :name, :begin => 2

0 comments on commit 1728080

Please sign in to comment.