Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Issue #687: cancan inserting "AND (NULL)" at the end of sql

Ensure that empty conditions does not trigger unmergeable conditions
  • Loading branch information...
commit f5b3fcd8db6e9ed01b627c6103752f0bb71f8cb6 1 parent 3b50fed
Jonathan authored
3  lib/cancan/rule.rb
@@ -55,7 +55,8 @@ def conditions_empty?
55 55 end
56 56
57 57 def unmergeable?
58   - @conditions.respond_to?(:keys) && (! @conditions.keys.first.kind_of? Symbol)
  58 + @conditions.respond_to?(:keys) && @conditions.present? &&
  59 + (!@conditions.keys.first.kind_of? Symbol)
59 60 end
60 61
61 62 def associations_hash(conditions = @conditions)
20 spec/cancan/model_adapters/active_record_adapter_spec.rb
@@ -20,10 +20,12 @@
20 20 t.boolean "secret"
21 21 t.integer "priority"
22 22 t.integer "category_id"
  23 + t.integer "user_id"
23 24 end
24 25 model do
25 26 belongs_to :category
26 27 has_many :comments
  28 + belongs_to :user
27 29 end
28 30 end
29 31
@@ -37,6 +39,15 @@
37 39 end
38 40 end
39 41
  42 + with_model :user do
  43 + table do |t|
  44 +
  45 + end
  46 + model do
  47 + has_many :articles
  48 + end
  49 + end
  50 +
40 51 before(:each) do
41 52 Article.delete_all
42 53 Comment.delete_all
@@ -233,6 +244,15 @@
233 244 @ability.model_adapter(Article, :read).joins.should == [{:project=>[:comments]}]
234 245 end
235 246
  247 + it "should merge :all conditions with other conditions" do
  248 + user = User.create!
  249 + article = Article.create!(:user => user)
  250 + ability = Ability.new(user)
  251 + ability.can :manage, :all
  252 + ability.can :manage, Article, :user_id => user.id
  253 + Article.accessible_by(ability).should == [article]
  254 + end
  255 +
236 256 it "should restrict articles given a MetaWhere condition" do
237 257 @ability.can :read, Article, :priority.lt => 2
238 258 article1 = Article.create!(:priority => 1)
5 spec/cancan/rule_spec.rb
@@ -44,4 +44,9 @@
44 44
45 45 @rule.should be_unmergeable
46 46 end
  47 +
  48 + it "should be mergeable if conditions is an empty hash" do
  49 + @conditions = {}
  50 + @rule.should_not be_unmergeable
  51 + end
47 52 end

0 comments on commit f5b3fcd

Please sign in to comment.
Something went wrong with that request. Please try again.