From 37c149182c5566c0928e8d4d0f6e767951e59c9b Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 21 Dec 2010 10:41:55 -0800 Subject: [PATCH] renaming CanDefinition to Rule --- lib/cancan.rb | 2 +- lib/cancan/ability.rb | 46 +++++++++---------- lib/cancan/mongoid_additions.rb | 12 ++--- lib/cancan/query.rb | 16 +++---- lib/cancan/{can_definition.rb => rule.rb} | 2 +- spec/cancan/ability_spec.rb | 6 +-- spec/cancan/query_spec.rb | 8 ++-- .../{can_definition_spec.rb => rule_spec.rb} | 26 +++++------ 8 files changed, 59 insertions(+), 59 deletions(-) rename lib/cancan/{can_definition.rb => rule.rb} (99%) rename spec/cancan/{can_definition_spec.rb => rule_spec.rb} (57%) diff --git a/lib/cancan.rb b/lib/cancan.rb index 8df4caa..1238cf5 100644 --- a/lib/cancan.rb +++ b/lib/cancan.rb @@ -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' diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index 0d9e9d9..709df00 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -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 diff --git a/lib/cancan/mongoid_additions.rb b/lib/cancan/mongoid_additions.rb index 9efd01f..86181c3 100644 --- a/lib/cancan/mongoid_additions.rb +++ b/lib/cancan/mongoid_additions.rb @@ -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? diff --git a/lib/cancan/query.rb b/lib/cancan/query.rb index 09c4bde..67aadd4 100644 --- a/lib/cancan/query.rb +++ b/lib/cancan/query.rb @@ -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 diff --git a/lib/cancan/can_definition.rb b/lib/cancan/rule.rb similarity index 99% rename from lib/cancan/can_definition.rb rename to lib/cancan/rule.rb index a7c96d5..fee6e72 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/rule.rb @@ -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 diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 70f00fd..e022182 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -32,7 +32,7 @@ describe CanCan::Ability do @ability.can?(:read, Symbol).should be_true end - it "should pass to previous can definition, if block returns false or nil" do + it "should pass to previous rule, if block returns false or nil" do @ability.can :read, Symbol @ability.can :read, Integer do |i| i < 5 @@ -144,7 +144,7 @@ describe CanCan::Ability do @ability.can?(:update, 123).should be_false end - it "should support custom objects in the can definition" do + it "should support custom objects in the rule" do @ability.can :read, :stats @ability.can?(:read, :stats).should be_true @ability.can?(:update, :stats).should be_false @@ -165,7 +165,7 @@ describe CanCan::Ability do @ability.can?(:read, 123).should be_false end - it "should pass to previous can definition, if block returns false or nil" do + it "should pass to previous rule, if block returns false or nil" do @ability.can :read, :all @ability.cannot :read, Integer do |int| int > 10 ? nil : ( int > 5 ) diff --git a/spec/cancan/query_spec.rb b/spec/cancan/query_spec.rb index a44b6a6..989059d 100644 --- a/spec/cancan/query_spec.rb +++ b/spec/cancan/query_spec.rb @@ -15,19 +15,19 @@ describe CanCan::Query do @ability.query(:read, Project).conditions.should == { :blocked => false, :user_id => 1 } end - it "should merge multiple can definitions into single SQL string joining with OR" do + it "should merge multiple rules into single SQL string joining with OR" do @ability.can :read, Project, :blocked => false @ability.can :read, Project, :admin => true @ability.query(:read, Project).conditions.should == "(admin=true) OR (blocked=false)" end - it "should merge multiple can definitions into single SQL string joining with OR and AND" do + it "should merge multiple rules into single SQL string joining with OR and AND" do @ability.can :read, Project, :blocked => false, :active => true @ability.can :read, Project, :admin => true @ability.query(:read, Project).conditions.should orderlessly_match("(blocked=false AND active=true) OR (admin=true)") end - it "should merge multiple can definitions into single SQL string joining with OR and AND" do + it "should merge multiple rules into single SQL string joining with OR and AND" do @ability.can :read, Project, :blocked => false, :active => true @ability.can :read, Project, :admin => true @ability.query(:read, Project).conditions.should orderlessly_match("(blocked=false AND active=true) OR (admin=true)") @@ -77,7 +77,7 @@ describe CanCan::Query do @ability.query(:read, Project).conditions.should == 'true=true' end - it "should have nil joins if no can definitions" do + it "should have nil joins if no rules" do @ability.query(:read, Project).joins.should be_nil end diff --git a/spec/cancan/can_definition_spec.rb b/spec/cancan/rule_spec.rb similarity index 57% rename from spec/cancan/can_definition_spec.rb rename to spec/cancan/rule_spec.rb index dab6345..7a12571 100644 --- a/spec/cancan/can_definition_spec.rb +++ b/spec/cancan/rule_spec.rb @@ -1,57 +1,57 @@ require "spec_helper" -# Most of CanDefinition functionality is tested in Ability specs -describe CanCan::CanDefinition do +# Most of Rule functionality is tested in Ability specs +describe CanCan::Rule do before(:each) do @conditions = {} - @can = CanCan::CanDefinition.new(true, :read, Integer, @conditions, nil) + @rule = CanCan::Rule.new(true, :read, Integer, @conditions, nil) end it "should return no association joins if none exist" do - @can.associations_hash.should == {} + @rule.associations_hash.should == {} end it "should return no association for joins if just attributes" do @conditions[:foo] = :bar - @can.associations_hash.should == {} + @rule.associations_hash.should == {} end it "should return single association for joins" do @conditions[:foo] = {:bar => 1} - @can.associations_hash.should == {:foo => {}} + @rule.associations_hash.should == {:foo => {}} end it "should return multiple associations for joins" do @conditions[:foo] = {:bar => 1} @conditions[:test] = {1 => 2} - @can.associations_hash.should == {:foo => {}, :test => {}} + @rule.associations_hash.should == {:foo => {}, :test => {}} end it "should return nested associations for joins" do @conditions[:foo] = {:bar => {1 => 2}} - @can.associations_hash.should == {:foo => {:bar => {}}} + @rule.associations_hash.should == {:foo => {:bar => {}}} end it "should tableize correctly for absurdly complex permissions" do @conditions[:unit] = {:property=>{:landlord=>{:weasle_id=>560}}} @conditions[:test] = 1 - @can.tableized_conditions.should == {:units => {:properties => {:landlords=>{:weasle_id=>560}}}, :test => 1} + @rule.tableized_conditions.should == {:units => {:properties => {:landlords=>{:weasle_id=>560}}}, :test => 1} end it "should tableize correctly for complex permissions" do @conditions[:unit] = {:property=>{:landlord_id=>560}} @conditions[:test] = 1 - @can.tableized_conditions.should == {:units => {:properties => {:landlord_id=>560}}, :test => 1} + @rule.tableized_conditions.should == {:units => {:properties => {:landlord_id=>560}}, :test => 1} end it "should return table names in conditions for association joins" do @conditions[:foo] = {:bar => 1} @conditions[:test] = 1 - @can.tableized_conditions.should == {:foos => {:bar => 1}, :test => 1} + @rule.tableized_conditions.should == {:foos => {:bar => 1}, :test => 1} end it "should return no association joins if conditions is nil" do - can = CanCan::CanDefinition.new(true, :read, Integer, nil, nil) - can.associations_hash.should == {} + rule = CanCan::Rule.new(true, :read, Integer, nil, nil) + rule.associations_hash.should == {} end end