renaming CanDefinition to Rule
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
require 'cancan/ability'
|
||||
require 'cancan/can_definition'
|
||||
require 'cancan/rule'
|
||||
require 'cancan/controller_resource'
|
||||
require 'cancan/controller_additions'
|
||||
require 'cancan/active_record_additions'
|
||||
|
||||
@@ -54,8 +54,8 @@ module CanCan
|
||||
#
|
||||
# Also see the RSpec Matchers to aid in testing.
|
||||
def can?(action, subject, *extra_args)
|
||||
match = relevant_can_definitions_for_match(action, subject).detect do |can_definition|
|
||||
can_definition.matches_conditions?(action, subject, extra_args)
|
||||
match = relevant_rules_for_match(action, subject).detect do |rule|
|
||||
rule.matches_conditions?(action, subject, extra_args)
|
||||
end
|
||||
match ? match.base_behavior : false
|
||||
end
|
||||
@@ -122,7 +122,7 @@ module CanCan
|
||||
# end
|
||||
#
|
||||
def can(action = nil, subject = nil, conditions = nil, &block)
|
||||
can_definitions << CanDefinition.new(true, action, subject, conditions, block)
|
||||
rules << Rule.new(true, action, subject, conditions, block)
|
||||
end
|
||||
|
||||
# Defines an ability which cannot be done. Accepts the same arguments as "can".
|
||||
@@ -138,7 +138,7 @@ module CanCan
|
||||
# end
|
||||
#
|
||||
def cannot(action = nil, subject = nil, conditions = nil, &block)
|
||||
can_definitions << CanDefinition.new(false, action, subject, conditions, block)
|
||||
rules << Rule.new(false, action, subject, conditions, block)
|
||||
end
|
||||
|
||||
# Alias one or more actions into another one.
|
||||
@@ -187,10 +187,10 @@ module CanCan
|
||||
end
|
||||
|
||||
# Returns a CanCan::Query instance to help generate database queries based on the ability.
|
||||
# If any relevant can definitions use a block then an exception will be raised because an
|
||||
# If any relevant rules use a block then an exception will be raised because an
|
||||
# SQL query cannot be generated from blocks of code.
|
||||
def query(action, subject)
|
||||
Query.new(subject, relevant_can_definitions_for_query(action, subject))
|
||||
Query.new(subject, relevant_rules_for_query(action, subject))
|
||||
end
|
||||
|
||||
# See ControllerAdditions#authorize! for documentation.
|
||||
@@ -215,18 +215,18 @@ module CanCan
|
||||
|
||||
def attributes_for(action, subject)
|
||||
attributes = {}
|
||||
relevant_can_definitions(action, subject).map do |can_definition|
|
||||
attributes.merge!(can_definition.attributes_from_conditions) if can_definition.base_behavior
|
||||
relevant_rules(action, subject).map do |rule|
|
||||
attributes.merge!(rule.attributes_from_conditions) if rule.base_behavior
|
||||
end
|
||||
attributes
|
||||
end
|
||||
|
||||
def has_block?(action, subject)
|
||||
relevant_can_definitions(action, subject).any?(&:only_block?)
|
||||
relevant_rules(action, subject).any?(&:only_block?)
|
||||
end
|
||||
|
||||
def has_raw_sql?(action, subject)
|
||||
relevant_can_definitions(action, subject).any?(&:only_raw_sql?)
|
||||
relevant_rules(action, subject).any?(&:only_raw_sql?)
|
||||
end
|
||||
|
||||
private
|
||||
@@ -259,30 +259,30 @@ module CanCan
|
||||
results
|
||||
end
|
||||
|
||||
def can_definitions
|
||||
@can_definitions ||= []
|
||||
def rules
|
||||
@rules ||= []
|
||||
end
|
||||
|
||||
# Returns an array of CanDefinition instances which match the action and subject
|
||||
# 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_can_definitions(action, subject)
|
||||
can_definitions.reverse.select do |can_definition|
|
||||
can_definition.expanded_actions = expand_actions(can_definition.actions)
|
||||
can_definition.relevant? action, subject
|
||||
def relevant_rules(action, subject)
|
||||
rules.reverse.select do |rule|
|
||||
rule.expanded_actions = expand_actions(rule.actions)
|
||||
rule.relevant? action, subject
|
||||
end
|
||||
end
|
||||
|
||||
def relevant_can_definitions_for_match(action, subject)
|
||||
relevant_can_definitions(action, subject).each do |can_definition|
|
||||
if can_definition.only_raw_sql?
|
||||
def relevant_rules_for_match(action, subject)
|
||||
relevant_rules(action, subject).each do |rule|
|
||||
if rule.only_raw_sql?
|
||||
raise Error, "The can? and cannot? call cannot be used with a raw sql 'can' definition. The checking code cannot be determined for #{action.inspect} #{subject.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def relevant_can_definitions_for_query(action, subject)
|
||||
relevant_can_definitions(action, subject).each do |can_definition|
|
||||
if can_definition.only_block?
|
||||
def relevant_rules_for_query(action, subject)
|
||||
relevant_rules(action, subject).each do |rule|
|
||||
if rule.only_block?
|
||||
raise Error, "The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for #{action.inspect} #{subject.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,21 +12,21 @@ module CanCan
|
||||
end
|
||||
|
||||
def query_with_mongoid_support(action, subject)
|
||||
MongoidQuery.new(subject, relevant_can_definitions_for_query(action, subject))
|
||||
MongoidQuery.new(subject, relevant_rules_for_query(action, subject))
|
||||
end
|
||||
end
|
||||
|
||||
class MongoidQuery
|
||||
def initialize(sanitizer, can_definitions)
|
||||
def initialize(sanitizer, rules)
|
||||
@sanitizer = sanitizer
|
||||
@can_definitions = can_definitions
|
||||
@rules = rules
|
||||
end
|
||||
|
||||
def conditions
|
||||
if @can_definitions.size == 0
|
||||
if @rules.size == 0
|
||||
false_query
|
||||
else
|
||||
@can_definitions.first.instance_variable_get(:@conditions)
|
||||
@rules.first.instance_variable_get(:@conditions)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,7 +45,7 @@ module CanCan
|
||||
# => true
|
||||
# {}.all?{|a| a != 5}
|
||||
# => true
|
||||
class CanDefinition
|
||||
class Rule
|
||||
def matches_conditions_hash_with_mongoid_subject?(subject, conditions = @conditions)
|
||||
if subject.class.include?(Mongoid::Document) && conditions.any?{|k,v| !k.kind_of?(Symbol)}
|
||||
if conditions.empty?
|
||||
|
||||
@@ -3,9 +3,9 @@ module CanCan
|
||||
# Generates the sql conditions and association joins for use in ActiveRecord queries.
|
||||
# Normally you will not use this class directly, but instead through ActiveRecordAdditions#accessible_by.
|
||||
class Query
|
||||
def initialize(sanitizer, can_definitions)
|
||||
def initialize(sanitizer, rules)
|
||||
@sanitizer = sanitizer
|
||||
@can_definitions = can_definitions
|
||||
@rules = rules
|
||||
end
|
||||
|
||||
# Returns conditions intended to be used inside a database query. Normally you will not call this
|
||||
@@ -24,12 +24,12 @@ module CanCan
|
||||
# query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))"
|
||||
#
|
||||
def conditions
|
||||
if @can_definitions.size == 1 && @can_definitions.first.base_behavior
|
||||
if @rules.size == 1 && @rules.first.base_behavior
|
||||
# Return the conditions directly if there's just one definition
|
||||
@can_definitions.first.tableized_conditions
|
||||
@rules.first.tableized_conditions
|
||||
else
|
||||
@can_definitions.reverse.inject(false_sql) do |sql, can_definition|
|
||||
merge_conditions(sql, can_definition.tableized_conditions, can_definition.base_behavior)
|
||||
@rules.reverse.inject(false_sql) do |sql, rule|
|
||||
merge_conditions(sql, rule.tableized_conditions, rule.base_behavior)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -38,8 +38,8 @@ module CanCan
|
||||
# See ActiveRecordAdditions#accessible_by for use in Active Record.
|
||||
def joins
|
||||
joins_hash = {}
|
||||
@can_definitions.each do |can_definition|
|
||||
merge_joins(joins_hash, can_definition.associations_hash)
|
||||
@rules.each do |rule|
|
||||
merge_joins(joins_hash, rule.associations_hash)
|
||||
end
|
||||
clean_joins(joins_hash) unless joins_hash.empty?
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ module CanCan
|
||||
# This class is used internally and should only be called through Ability.
|
||||
# it holds the information about a "can" call made on Ability and provides
|
||||
# helpful methods to determine permission checking and conditions hash generation.
|
||||
class CanDefinition # :nodoc:
|
||||
class Rule # :nodoc:
|
||||
attr_reader :base_behavior, :actions
|
||||
attr_writer :expanded_actions
|
||||
|
||||
Reference in New Issue
Block a user