Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Makes cancan2.0 compatible with Rails4 and optimise a view call #1001

Open
wants to merge 3 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/cancan/model_adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/cancan/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -48,19 +48,19 @@ 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?
@attributes.present?
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?
Expand Down