Merge pull request #632 from andhapp/fix-issue-327
Fix to handle MetaWhere and non-MetaWhere conditions correctly.
This commit is contained in:
commit
80a8c39a93
|
@ -89,7 +89,12 @@ module CanCan
|
||||||
if override_scope
|
if override_scope
|
||||||
@model_class.scoped.merge(override_scope)
|
@model_class.scoped.merge(override_scope)
|
||||||
elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins)
|
elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins)
|
||||||
|
mergeable_conditions = @rules.select {|rule| rule.unmergeable? }.blank?
|
||||||
|
if mergeable_conditions
|
||||||
@model_class.where(conditions).joins(joins)
|
@model_class.where(conditions).joins(joins)
|
||||||
|
else
|
||||||
|
@model_class.where(*(@rules.map(&:conditions))).joins(joins)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
@model_class.scoped(:conditions => conditions, :joins => joins)
|
@model_class.scoped(:conditions => conditions, :joins => joins)
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,6 +54,10 @@ module CanCan
|
||||||
@conditions == {} || @conditions.nil?
|
@conditions == {} || @conditions.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unmergeable?
|
||||||
|
@conditions.respond_to?(:keys) && (! @conditions.keys.first.kind_of? Symbol)
|
||||||
|
end
|
||||||
|
|
||||||
def associations_hash(conditions = @conditions)
|
def associations_hash(conditions = @conditions)
|
||||||
hash = {}
|
hash = {}
|
||||||
conditions.map do |name, value|
|
conditions.map do |name, value|
|
||||||
|
|
|
@ -236,6 +236,16 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
|
||||||
@ability.should_not be_able_to(:read, article2)
|
@ability.should_not be_able_to(:read, article2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should merge MetaWhere and non-MetaWhere conditions" do
|
||||||
|
@ability.can :read, Article, :priority.lt => 2
|
||||||
|
@ability.can :read, Article, :priority => 1
|
||||||
|
article1 = Article.create!(:priority => 1)
|
||||||
|
article2 = Article.create!(:priority => 3)
|
||||||
|
Article.accessible_by(@ability).should == [article1]
|
||||||
|
@ability.should be_able_to(:read, article1)
|
||||||
|
@ability.should_not be_able_to(:read, article2)
|
||||||
|
end
|
||||||
|
|
||||||
it "should match any MetaWhere condition" do
|
it "should match any MetaWhere condition" do
|
||||||
adapter = CanCan::ModelAdapters::ActiveRecordAdapter
|
adapter = CanCan::ModelAdapters::ActiveRecordAdapter
|
||||||
article1 = Article.new(:priority => 1, :name => "Hello World")
|
article1 = Article.new(:priority => 1, :name => "Hello World")
|
||||||
|
|
|
@ -36,4 +36,11 @@ describe CanCan::Rule do
|
||||||
rule = CanCan::Rule.new(true, :read, Integer, nil, nil)
|
rule = CanCan::Rule.new(true, :read, Integer, nil, nil)
|
||||||
rule.associations_hash.should == {}
|
rule.associations_hash.should == {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should not be mergeable if conditions are not simple hashes" do
|
||||||
|
meta_where = OpenStruct.new(:name => 'metawhere', :column => 'test')
|
||||||
|
@conditions[meta_where] = :bar
|
||||||
|
|
||||||
|
@rule.should be_unmergeable
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user