From 6016685a913220d8ecf7dbc82909ab3e3cf9da9a Mon Sep 17 00:00:00 2001 From: gamov Date: Thu, 3 Apr 2014 15:08:59 +0800 Subject: [PATCH 1/3] Update rule.rb Optimize DB hits when supplying a scope (AR:Relation) --- lib/cancan/rule.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index a3c86ef4..36e0fbc2 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -39,7 +39,7 @@ def matches_conditions?(action, subject, attribute) matches_conditions_hash?(subject) else # Don't stop at "cannot" definitions when there are conditions. - @conditions.empty? ? true : @base_behavior + (@conditions.is_a?(ActiveRecord::Relation) || @conditions.empty?) ? true : @base_behavior end end @@ -48,7 +48,7 @@ def only_block? end def only_raw_sql? - @block.nil? && conditions? && !@conditions.kind_of?(Hash) + @block.nil? && conditions? && !@conditions.kind_of?(Hash) && !@conditions.is_a?(ActiveRecord::Relation) end def attributes? @@ -56,11 +56,11 @@ def attributes? end def conditions? - @conditions.present? + @conditions.is_a?(ActiveRecord::Relation) || @conditions.present? end def instance_conditions? - @block || conditions? + @block || (!@conditions.is_a?(ActiveRecord::Relation) && conditions?) end def unmergeable? From 6b95ba926b26a255888df4792b8d2f1656f4e9fe Mon Sep 17 00:00:00 2001 From: gamov Date: Wed, 24 Feb 2016 15:11:56 +0800 Subject: [PATCH 2/3] Fix rails 4 deprecations --- lib/cancan/model_adapters/active_record_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cancan/model_adapters/active_record_adapter.rb b/lib/cancan/model_adapters/active_record_adapter.rb index 3cc55a4b..0d808e9e 100644 --- a/lib/cancan/model_adapters/active_record_adapter.rb +++ b/lib/cancan/model_adapters/active_record_adapter.rb @@ -87,7 +87,7 @@ def joins def database_records if override_scope - @model_class.scoped.merge(override_scope) + @model_class.all.merge(override_scope) elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins) mergeable_conditions = @rules.select {|rule| rule.unmergeable? }.blank? if mergeable_conditions From 435b65569b46182f05c94a4c9eb130b6ffdd7a3f Mon Sep 17 00:00:00 2001 From: gamov Date: Mon, 4 Apr 2016 13:18:38 +0800 Subject: [PATCH 3/3] Workaround issue #727 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If conditions are the same, we don’t merge them. https://github.com/ryanb/cancan/issues/727 --- lib/cancan/model_adapters/active_record_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cancan/model_adapters/active_record_adapter.rb b/lib/cancan/model_adapters/active_record_adapter.rb index 0d808e9e..74cc5e23 100644 --- a/lib/cancan/model_adapters/active_record_adapter.rb +++ b/lib/cancan/model_adapters/active_record_adapter.rb @@ -52,7 +52,7 @@ def self.meta_where_match?(subject_value, method, value) # query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))" # def conditions - if @rules.size == 1 && @rules.first.base_behavior + if (@rules.size == 1 || @rules.uniq{|r| r.conditions}.size == 1) && @rules.first.base_behavior # Return the conditions directly if there's just one definition tableized_conditions(@rules.first.conditions).dup else