Skip to content
This repository
Browse code

removing query.rb since it is no longer used

  • Loading branch information...
commit f9f71d679d0368402347f4116ddfb9a2d2cf6c69 1 parent fc867c9
Ryan Bates authored December 30, 2010
1  lib/cancan.rb
@@ -4,7 +4,6 @@
4 4
 require 'cancan/controller_additions'
5 5
 require 'cancan/model_additions'
6 6
 require 'cancan/exceptions'
7  
-require 'cancan/query'
8 7
 require 'cancan/inherited_resource'
9 8
 
10 9
 require 'cancan/model_adapters/abstract_adapter'
97  lib/cancan/query.rb
... ...
@@ -1,97 +0,0 @@
1  
-module CanCan
2  
-
3  
-  # Generates the sql conditions and association joins for use in ActiveRecord queries.
4  
-  # Normally you will not use this class directly, but instead through ModelAdditions#accessible_by.
5  
-  class Query
6  
-    def initialize(sanitizer, rules)
7  
-      @sanitizer = sanitizer
8  
-      @rules = rules
9  
-    end
10  
-
11  
-    # Returns conditions intended to be used inside a database query. Normally you will not call this
12  
-    # method directly, but instead go through ModelAdditions#accessible_by.
13  
-    #
14  
-    # If there is only one "can" definition, a hash of conditions will be returned matching the one defined.
15  
-    #
16  
-    #   can :manage, User, :id => 1
17  
-    #   query(:manage, User).conditions # => { :id => 1 }
18  
-    #
19  
-    # If there are multiple "can" definitions, a SQL string will be returned to handle complex cases.
20  
-    #
21  
-    #   can :manage, User, :id => 1
22  
-    #   can :manage, User, :manager_id => 1
23  
-    #   cannot :manage, User, :self_managed => true
24  
-    #   query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))"
25  
-    #
26  
-    def conditions
27  
-      if @rules.size == 1 && @rules.first.base_behavior
28  
-        # Return the conditions directly if there's just one definition
29  
-        @rules.first.tableized_conditions.dup
30  
-      else
31  
-        @rules.reverse.inject(false_sql) do |sql, rule|
32  
-          merge_conditions(sql, rule.tableized_conditions.dup, rule.base_behavior)
33  
-        end
34  
-      end
35  
-    end
36  
-
37  
-    # Returns the associations used in conditions for the :joins option of a search.
38  
-    # See ActiveRecordAdditions#accessible_by for use in Active Record.
39  
-    def joins
40  
-      joins_hash = {}
41  
-      @rules.each do |rule|
42  
-        merge_joins(joins_hash, rule.associations_hash)
43  
-      end
44  
-      clean_joins(joins_hash) unless joins_hash.empty?
45  
-    end
46  
-
47  
-    private
48  
-
49  
-    def merge_conditions(sql, conditions_hash, behavior)
50  
-      if conditions_hash.blank?
51  
-        behavior ? true_sql : false_sql
52  
-      else
53  
-        conditions = sanitize_sql(conditions_hash)
54  
-        case sql
55  
-        when true_sql
56  
-          behavior ? true_sql : "not (#{conditions})"
57  
-        when false_sql
58  
-          behavior ? conditions : false_sql
59  
-        else
60  
-          behavior ? "(#{conditions}) OR (#{sql})" : "not (#{conditions}) AND (#{sql})"
61  
-        end
62  
-      end
63  
-    end
64  
-
65  
-    def false_sql
66  
-      sanitize_sql(['?=?', true, false])
67  
-    end
68  
-
69  
-    def true_sql
70  
-      sanitize_sql(['?=?', true, true])
71  
-    end
72  
-
73  
-    def sanitize_sql(conditions)
74  
-      @sanitizer.send(:sanitize_sql, conditions)
75  
-    end
76  
-
77  
-    # Takes two hashes and does a deep merge.
78  
-    def merge_joins(base, add)
79  
-      add.each do |name, nested|
80  
-        if base[name].is_a?(Hash) && !nested.empty?
81  
-          merge_joins(base[name], nested)
82  
-        else
83  
-          base[name] = nested
84  
-        end
85  
-      end
86  
-    end
87  
-
88  
-    # Removes empty hashes and moves everything into arrays.
89  
-    def clean_joins(joins_hash)
90  
-      joins = []
91  
-      joins_hash.each do |name, nested|
92  
-        joins << (nested.empty? ? name : {name => clean_joins(nested)})
93  
-      end
94  
-      joins
95  
-    end
96  
-  end
97  
-end

0 notes on commit f9f71d6

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