Browse files

renaming noun to subject internally

  • Loading branch information...
1 parent 240c281 commit d9f3c8b0ae41512aaabd81d462970b40c2fc7e89 @ryanb committed Apr 16, 2010
Showing with 26 additions and 26 deletions.
  1. +4 −4 lib/cancan.rb
  2. +22 −22 lib/cancan/ability.rb
View
8 lib/cancan.rb
@@ -1,10 +1,10 @@
module CanCan
- # This error is raised when a user isn't allowed to access a given
- # controller action. See ControllerAdditions#unauthorized! for details.
- class AccessDenied < StandardError; end
-
# A general CanCan exception
class Error < StandardError; end
+
+ # This error is raised when a user isn't allowed to access a given
+ # controller action. See ControllerAdditions#unauthorized! for details.
+ class AccessDenied < Error; end
end
require 'cancan/ability'
View
44 lib/cancan/ability.rb
@@ -49,9 +49,9 @@ module Ability
# assert ability.cannot?(:destroy, Project.new)
# end
#
- def can?(action, noun, *extra_args)
- matching_can_definition(action, noun) do |base_behavior, defined_actions, defined_nouns, defined_conditions, defined_block|
- result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_conditions, defined_block, extra_args)
+ def can?(action, subject, *extra_args)
+ matching_can_definition(action, subject) do |base_behavior, defined_actions, defined_subjects, defined_conditions, defined_block|
+ result = can_perform_action?(action, subject, defined_actions, defined_subjects, defined_conditions, defined_block, extra_args)
return base_behavior ? result : !result
end
false
@@ -118,9 +118,9 @@ def cannot?(*args)
# can :read, :stats
# can? :read, :stats # => true
#
- def can(action, noun, conditions = nil, &block)
+ def can(action, subject, conditions = nil, &block)
@can_definitions ||= []
- @can_definitions << [true, action, noun, conditions, block]
+ @can_definitions << [true, action, subject, conditions, block]
end
# Define an ability which cannot be done. Accepts the same arguments as "can".
@@ -135,9 +135,9 @@ def can(action, noun, conditions = nil, &block)
# product.invisible?
# end
#
- def cannot(action, noun, conditions = nil, &block)
+ def cannot(action, subject, conditions = nil, &block)
@can_definitions ||= []
- @can_definitions << [false, action, noun, conditions, block]
+ @can_definitions << [false, action, subject, conditions, block]
end
# Alias one or more actions into another one.
@@ -196,22 +196,22 @@ def clear_aliased_actions
# If the ability is not defined then false is returned so be sure to take that into consideration.
# If the ability is defined using a block then this will raise an exception since a hash of conditions cannot be
# determined from that.
- def conditions(action, noun)
- matching_can_definition(action, noun) do |base_behavior, defined_actions, defined_nouns, defined_conditions, defined_block|
- raise Error, "Cannot determine ability conditions from block for #{action.inspect} #{noun.inspect}" if defined_block
+ def conditions(action, subject)
+ matching_can_definition(action, subject) do |base_behavior, defined_actions, defined_subjects, defined_conditions, defined_block|
+ raise Error, "Cannot determine ability conditions from block for #{action.inspect} #{subject.inspect}" if defined_block
return defined_conditions || {}
end
false
end
private
- def matching_can_definition(action, noun, &block)
- (@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_conditions, defined_block|
+ def matching_can_definition(action, subject, &block)
+ (@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_subject, defined_conditions, defined_block|
defined_actions = expand_actions(defined_action)
- defined_nouns = [defined_noun].flatten
- if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun)
- return block.call(base_behavior, defined_actions, defined_nouns, defined_conditions, defined_block)
+ defined_subjects = [defined_subject].flatten
+ if includes_action?(defined_actions, action) && includes_subject?(defined_subjects, subject)
+ return block.call(base_behavior, defined_actions, defined_subjects, defined_conditions, defined_block)
end
end
end
@@ -234,18 +234,18 @@ def expand_actions(actions)
end.flatten
end
- def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_conditions, defined_block, extra_args)
+ def can_perform_action?(action, subject, defined_actions, defined_subjects, defined_conditions, defined_block, extra_args)
if defined_block
block_args = []
block_args << action if defined_actions.include?(:manage)
- block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all)
- block_args << (noun.class == Class ? nil : noun)
+ block_args << (subject.class == Class ? subject : subject.class) if defined_subjects.include?(:all)
+ block_args << (subject.class == Class ? nil : subject)
block_args += extra_args
defined_block.call(*block_args)
elsif defined_conditions
- if noun.class != Class
+ if subject.class != Class
defined_conditions.all? do |name, value|
- noun.send(name) == value
+ subject.send(name) == value
end
end
else
@@ -257,8 +257,8 @@ def includes_action?(actions, action)
actions.include?(:manage) || actions.include?(action)
end
- def includes_noun?(nouns, noun)
- nouns.include?(:all) || nouns.include?(noun) || nouns.any? { |c| c.kind_of?(Class) && noun.kind_of?(c) }
+ def includes_subject?(subjects, subject)
+ subjects.include?(:all) || subjects.include?(subject) || subjects.any? { |c| c.kind_of?(Class) && subject.kind_of?(c) }
end
end
end

0 comments on commit d9f3c8b

Please sign in to comment.