diff --git a/lib/cancan/can_definition.rb b/lib/cancan/can_definition.rb index ea679e5..4b9c9f2 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/can_definition.rb @@ -34,21 +34,16 @@ module CanCan end end - def tableized_conditions(c=nil) - conditions = c || @conditions - if conditions - conditions.inject({}) do |tableized_conditions, (name, value)| - if value.kind_of? Hash - name = name.to_s.tableize.to_sym - value = tableized_conditions(value) - end - tableized_conditions[name] = value - tableized_conditions - end - end - end - - + def tableized_conditions(conditions = @conditions) + conditions.inject({}) do |result_hash, (name, value)| + if value.kind_of? Hash + name = name.to_s.tableize.to_sym + value = tableized_conditions(value) + end + result_hash[name] = value + result_hash + end + end def only_block? conditions_empty? && !@block.nil? diff --git a/spec/cancan/active_record_additions_spec.rb b/spec/cancan/active_record_additions_spec.rb index 65c9c38..1675a4b 100644 --- a/spec/cancan/active_record_additions_spec.rb +++ b/spec/cancan/active_record_additions_spec.rb @@ -31,8 +31,8 @@ describe CanCan::ActiveRecordAdditions do @ability.can :read, @model_class, :too => {:car => 1, :far => {:bar => 1}} condition_variants = [ - '(toos.far.bar=1 AND toos.car=1) OR (foos.bar=1)', # faked sql sanitizer is stupid ;-) - '(toos.car=1 AND toos.far.bar=1) OR (foos.bar=1)' + '(toos.fars.bar=1 AND toos.car=1) OR (foos.bar=1)', # faked sql sanitizer is stupid ;-) + '(toos.car=1 AND toos.fars.bar=1) OR (foos.bar=1)' ] joins_variants = [ [:foo, {:too => [:far]}], diff --git a/spec/cancan/can_definition_spec.rb b/spec/cancan/can_definition_spec.rb index 7732815..2b2851a 100644 --- a/spec/cancan/can_definition_spec.rb +++ b/spec/cancan/can_definition_spec.rb @@ -30,11 +30,13 @@ describe CanCan::CanDefinition do @conditions[:foo] = {:bar => {1 => 2}} @can.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} end + it "should tableize correctly for complex permissions" do @conditions[:unit] = {:property=>{:landlord_id=>560}} @conditions[:test] = 1