From 964a4765b1374874c639e647091c1133252da1b9 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 20 Jul 2010 13:43:43 -0700 Subject: [PATCH] removing need to pass tableize option around for query conditions --- lib/cancan/ability.rb | 4 ++-- lib/cancan/active_record_additions.rb | 2 +- lib/cancan/can_definition.rb | 12 ++++-------- lib/cancan/query.rb | 7 +++---- spec/cancan/can_definition_spec.rb | 2 +- spec/cancan/query_spec.rb | 15 --------------- 6 files changed, 11 insertions(+), 31 deletions(-) diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index 8b2c1b7..aba0dd7 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -183,8 +183,8 @@ module CanCan end # Returns a CanCan::Query instance to help generate database queries based on the ability. - def query(action, subject, options = {}) - Query.new(relevant_can_definitions_without_block(action, subject), subject, options) + def query(action, subject) + Query.new(subject, relevant_can_definitions_without_block(action, subject)) end private diff --git a/lib/cancan/active_record_additions.rb b/lib/cancan/active_record_additions.rb index 903aa5f..f09eb39 100644 --- a/lib/cancan/active_record_additions.rb +++ b/lib/cancan/active_record_additions.rb @@ -20,7 +20,7 @@ module CanCan # Here only the articles which the user can update are returned. This # internally uses Ability#conditions method, see that for more information. def accessible_by(ability, action = :read) - query = ability.query(action, self, :tableize => true) + query = ability.query(action, self) conditions = query.sql_conditions || {:id => nil} if respond_to? :where where(conditions).joins(query.association_joins) diff --git a/lib/cancan/can_definition.rb b/lib/cancan/can_definition.rb index 69508d8..32b6e12 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/can_definition.rb @@ -4,7 +4,6 @@ module CanCan # helpful methods to determine permission checking and conditions hash generation. class CanDefinition # :nodoc: attr_reader :conditions, :block, :base_behavior - include ActiveSupport::Inflector attr_reader :block attr_reader :actions attr_writer :expanded_actions @@ -37,17 +36,14 @@ module CanCan end end - # Returns a hash of conditions. If the ":tableize => true" option is passed - # it will pluralize the association conditions to match the table name. - def conditions(options = {}) - if options[:tableize] && @conditions.kind_of?(Hash) + # Returns a hash of conditions, pluralizing the table names + def tableized_conditions + if @conditions @conditions.inject({}) do |tableized_conditions, (name, value)| - name = tableize(name).to_sym if value.kind_of? Hash + name = name.to_s.tableize.to_sym if value.kind_of? Hash tableized_conditions[name] = value tableized_conditions end - else - @conditions end end diff --git a/lib/cancan/query.rb b/lib/cancan/query.rb index c2017b7..4f99581 100644 --- a/lib/cancan/query.rb +++ b/lib/cancan/query.rb @@ -3,10 +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(can_definitions, sanitizer, options) - @can_definitions = can_definitions + def initialize(sanitizer, can_definitions) @sanitizer = sanitizer - @options = options + @can_definitions = can_definitions end # Returns an array of arrays composing from desired action and hash of conditions which match the given ability. @@ -27,7 +26,7 @@ module CanCan def conditions unless @can_definitions.empty? @can_definitions.map do |can_definition| - [can_definition.base_behavior, can_definition.conditions(@options)] + [can_definition.base_behavior, can_definition.tableized_conditions] end else false diff --git a/spec/cancan/can_definition_spec.rb b/spec/cancan/can_definition_spec.rb index 00f605b..7ef74dc 100644 --- a/spec/cancan/can_definition_spec.rb +++ b/spec/cancan/can_definition_spec.rb @@ -34,7 +34,7 @@ describe CanCan::CanDefinition do it "should return table names in conditions for association joins" do @conditions[:foo] = {:bar => 1} @conditions[:test] = 1 - @can.conditions(:tableize => true).should == { :foos => { :bar => 1}, :test => 1 } + @can.tableized_conditions.should == { :foos => { :bar => 1}, :test => 1 } end it "should return no association joins if conditions is nil" do diff --git a/spec/cancan/query_spec.rb b/spec/cancan/query_spec.rb index d3d64e0..4510d46 100644 --- a/spec/cancan/query_spec.rb +++ b/spec/cancan/query_spec.rb @@ -84,19 +84,4 @@ describe CanCan::Query do @ability.query(:manage, Person).sql_conditions.should == {:id=>1} @ability.query(:read, Person).sql_conditions.should == 'true=true' end - - it "should accept array condition for use in sql" do - @ability.can :read, Person, ["user_id = ?", 1] - - @ability.query(:read, Person).sql_conditions.should == ['user_id = ?', 1] - @ability.query(:read, Person).association_joins.should be_nil - end - - it "should accept array condition for use in sql and do sanitizing in complex conditions" do - @ability.cannot :read, Person - @ability.can :read, Person, ["user_id = ?", 1] - - @ability.query(:read, Person).sql_conditions.should == 'user_id = 1' - @ability.query(:read, Person).association_joins.should be_nil - end end