From cfc355c006a3f531e4024e4cd54cbc4740f8635a Mon Sep 17 00:00:00 2001 From: David Mike Simon Date: Mon, 18 Jun 2012 19:40:48 -0700 Subject: [PATCH 1/2] Fix for issue 560 where joins could be thrown away by ActiveRecordAdapter::merge_joins --- lib/cancan/model_adapters/active_record_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cancan/model_adapters/active_record_adapter.rb b/lib/cancan/model_adapters/active_record_adapter.rb index f20da17..d7d856f 100644 --- a/lib/cancan/model_adapters/active_record_adapter.rb +++ b/lib/cancan/model_adapters/active_record_adapter.rb @@ -145,8 +145,8 @@ module CanCan # Takes two hashes and does a deep merge. def merge_joins(base, add) add.each do |name, nested| - if base[name].is_a?(Hash) && !nested.empty? - merge_joins(base[name], nested) + if base[name].is_a?(Hash) + merge_joins(base[name], nested) unless nested.empty? else base[name] = nested end From b162871c6d0e8189cab428e0526490b17d08be81 Mon Sep 17 00:00:00 2001 From: David Mike Simon Date: Tue, 19 Jun 2012 16:58:15 -0700 Subject: [PATCH 2/2] Spec to test against nested joins being thrown away ala issue 560 --- spec/cancan/model_adapters/active_record_adapter_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/cancan/model_adapters/active_record_adapter_spec.rb b/spec/cancan/model_adapters/active_record_adapter_spec.rb index a9debdd..1483875 100644 --- a/spec/cancan/model_adapters/active_record_adapter_spec.rb +++ b/spec/cancan/model_adapters/active_record_adapter_spec.rb @@ -227,6 +227,12 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record" @ability.model_adapter(Article, :read).joins.should == [:project] end + it "should merge nested and non-nested joins" do + @ability.can :read, Article, :project => { :blocked => false } + @ability.can :read, Article, :project => { :comments => { :spam => true } } + @ability.model_adapter(Article, :read).joins.should == [{:project=>[:comments]}] + end + it "should restrict articles given a MetaWhere condition" do @ability.can :read, Article, :priority.lt => 2 article1 = Article.create!(:priority => 1)