Merge pull request #765 from jonsgreen/issue/cancan_inserting_and_null_687

Issue #687: cancan inserting "AND (NULL)" at the end of sql
This commit is contained in:
Ryan Bates 2012-10-25 14:25:46 -07:00
commit 4dcd544594
3 changed files with 27 additions and 1 deletions

View File

@ -55,7 +55,8 @@ module CanCan
end end
def unmergeable? def unmergeable?
@conditions.respond_to?(:keys) && (! @conditions.keys.first.kind_of? Symbol) @conditions.respond_to?(:keys) && @conditions.present? &&
(!@conditions.keys.first.kind_of? Symbol)
end end
def associations_hash(conditions = @conditions) def associations_hash(conditions = @conditions)

View File

@ -20,10 +20,12 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
t.boolean "secret" t.boolean "secret"
t.integer "priority" t.integer "priority"
t.integer "category_id" t.integer "category_id"
t.integer "user_id"
end end
model do model do
belongs_to :category belongs_to :category
has_many :comments has_many :comments
belongs_to :user
end end
end end
@ -37,6 +39,15 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
end end
end end
with_model :user do
table do |t|
end
model do
has_many :articles
end
end
before(:each) do before(:each) do
Article.delete_all Article.delete_all
Comment.delete_all Comment.delete_all
@ -233,6 +244,15 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.model_adapter(Article, :read).joins.should == [{:project=>[:comments]}] @ability.model_adapter(Article, :read).joins.should == [{:project=>[:comments]}]
end end
it "should merge :all conditions with other conditions" do
user = User.create!
article = Article.create!(:user => user)
ability = Ability.new(user)
ability.can :manage, :all
ability.can :manage, Article, :user_id => user.id
Article.accessible_by(ability).should == [article]
end
it "should restrict articles given a MetaWhere condition" do it "should restrict articles given a MetaWhere condition" do
@ability.can :read, Article, :priority.lt => 2 @ability.can :read, Article, :priority.lt => 2
article1 = Article.create!(:priority => 1) article1 = Article.create!(:priority => 1)

View File

@ -44,4 +44,9 @@ describe CanCan::Rule do
@rule.should be_unmergeable @rule.should be_unmergeable
end end
it "should be mergeable if conditions is an empty hash" do
@conditions = {}
@rule.should_not be_unmergeable
end
end end