Module: ActiveRecord::QueryMethods
- Extended by:
- ActiveSupport::Concern
- Included in:
- Relation
- Defined in:
- lib/active_record/relation/query_methods.rb
Instance Attribute Summary collapse
-
#create_with_value ⇒ Object
Returns the value of attribute create_with_value.
-
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
-
#from_value ⇒ Object
Returns the value of attribute from_value.
-
#group_values ⇒ Object
Returns the value of attribute group_values.
-
#having_values ⇒ Object
Returns the value of attribute having_values.
-
#includes_values ⇒ Object
Returns the value of attribute includes_values.
-
#joins_values ⇒ Object
Returns the value of attribute joins_values.
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#lock_value ⇒ Object
Returns the value of attribute lock_value.
-
#offset_value ⇒ Object
Returns the value of attribute offset_value.
-
#order_values ⇒ Object
Returns the value of attribute order_values.
-
#preload_values ⇒ Object
Returns the value of attribute preload_values.
-
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
-
#select_values ⇒ Object
Returns the value of attribute select_values.
-
#where_values ⇒ Object
Returns the value of attribute where_values.
Instance Method Summary collapse
- #arel ⇒ Object
- #build_arel ⇒ Object
- #build_where(opts, other = []) ⇒ Object
- #create_with(value = true) ⇒ Object
- #custom_join_sql(*joins) ⇒ Object
- #eager_load(*args) ⇒ Object
- #extending(*modules, &block) ⇒ Object
- #from(value = true) ⇒ Object
- #group(*args) ⇒ Object
- #having(*args) ⇒ Object
- #includes(*args) ⇒ Object
- #joins(*args) ⇒ Object
- #limit(value = true) ⇒ Object
- #lock(locks = true) ⇒ Object
- #offset(value = true) ⇒ Object
- #order(*args) ⇒ Object
- #preload(*args) ⇒ Object
- #readonly(value = true) ⇒ Object
- #reorder(*args) ⇒ Object
- #reverse_order ⇒ Object
- #select(*args) ⇒ Object
- #where(opts, *rest) ⇒ Object
Instance Attribute Details
#create_with_value ⇒ Object
Returns the value of attribute create_with_value.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def create_with_value @create_with_value end |
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def eager_load_values @eager_load_values end |
#from_value ⇒ Object
Returns the value of attribute from_value.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def from_value @from_value end |
#group_values ⇒ Object
Returns the value of attribute group_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def group_values @group_values end |
#having_values ⇒ Object
Returns the value of attribute having_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def having_values @having_values end |
#includes_values ⇒ Object
Returns the value of attribute includes_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def includes_values @includes_values end |
#joins_values ⇒ Object
Returns the value of attribute joins_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def joins_values @joins_values end |
#limit_value ⇒ Object
Returns the value of attribute limit_value.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def limit_value @limit_value end |
#lock_value ⇒ Object
Returns the value of attribute lock_value.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def lock_value @lock_value end |
#offset_value ⇒ Object
Returns the value of attribute offset_value.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def offset_value @offset_value end |
#order_values ⇒ Object
Returns the value of attribute order_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def order_values @order_values end |
#preload_values ⇒ Object
Returns the value of attribute preload_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def preload_values @preload_values end |
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def readonly_value @readonly_value end |
#select_values ⇒ Object
Returns the value of attribute select_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def select_values @select_values end |
#where_values ⇒ Object
Returns the value of attribute where_values.
8 9 10 |
# File 'lib/active_record/relation/query_methods.rb', line 8 def where_values @where_values end |
Instance Method Details
#arel ⇒ Object
109 110 111 |
# File 'lib/active_record/relation/query_methods.rb', line 109 def arel @arel ||= build_arel end |
#build_arel ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/active_record/relation/query_methods.rb', line 135 def build_arel arel = table arel = build_joins(arel, @joins_values) unless @joins_values.empty? (@where_values - ['']).uniq.each do |where| case where when Arel::SqlLiteral arel = arel.where(where) else sql = where.is_a?(String) ? where : where.to_sql arel = arel.where(Arel::SqlLiteral.new("(#{sql})")) end end arel = arel.having(*@having_values.uniq.select{|h| h.present?}) unless @having_values.empty? arel = arel.take(@limit_value) if @limit_value arel = arel.skip(@offset_value) if @offset_value arel = arel.group(*@group_values.uniq.select{|g| g.present?}) unless @group_values.empty? arel = arel.order(*@order_values.uniq.select{|o| o.present?}) unless @order_values.empty? arel = build_select(arel, @select_values.uniq) arel = arel.from(@from_value) if @from_value arel = arel.lock(@lock_value) if @lock_value arel end |
#build_where(opts, other = []) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_record/relation/query_methods.rb', line 167 def build_where(opts, other = []) case opts when String, Array @klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other)) when Hash attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) PredicateBuilder.new(table.engine).build_from_hash(attributes, table) else opts end end |
#create_with(value = true) ⇒ Object
85 86 87 |
# File 'lib/active_record/relation/query_methods.rb', line 85 def create_with(value = true) clone.tap {|r| r.create_with_value = value } end |
#custom_join_sql(*joins) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_record/relation/query_methods.rb', line 113 def custom_join_sql(*joins) arel = table joins.each do |join| next if join.blank? @implicit_readonly = true case join when Hash, Array, Symbol if array_of_strings?(join) join_string = join.join(' ') arel = arel.join(Arel::SqlLiteral.new(join_string)) end when String arel = arel.join(Arel::SqlLiteral.new(join)) else arel = arel.join(join) end end arel.joins(arel) end |
#eager_load(*args) ⇒ Object
17 18 19 |
# File 'lib/active_record/relation/query_methods.rb', line 17 def eager_load(*args) clone.tap {|r| r.eager_load_values += args if args.present? } end |
#extending(*modules, &block) ⇒ Object
93 94 95 96 |
# File 'lib/active_record/relation/query_methods.rb', line 93 def extending(*modules, &block) modules << Module.new(&block) if block_given? clone.tap {|r| r.send(:apply_modules, *modules) } end |
#from(value = true) ⇒ Object
89 90 91 |
# File 'lib/active_record/relation/query_methods.rb', line 89 def from(value = true) clone.tap {|r| r.from_value = value } end |
#group(*args) ⇒ Object
33 34 35 |
# File 'lib/active_record/relation/query_methods.rb', line 33 def group(*args) clone.tap {|r| r.group_values += args.flatten if args.present? } end |
#having(*args) ⇒ Object
57 58 59 60 |
# File 'lib/active_record/relation/query_methods.rb', line 57 def having(*args) value = build_where(*args) clone.tap {|r| r.having_values += Array.wrap(value) if value.present? } end |
#includes(*args) ⇒ Object
12 13 14 15 |
# File 'lib/active_record/relation/query_methods.rb', line 12 def includes(*args) args.reject! { |a| a.blank? } clone.tap {|r| r.includes_values = (r.includes_values + args).flatten.uniq if args.present? } end |
#joins(*args) ⇒ Object
45 46 47 48 |
# File 'lib/active_record/relation/query_methods.rb', line 45 def joins(*args) args.flatten! clone.tap {|r| r.joins_values += args if args.present? } end |
#limit(value = true) ⇒ Object
62 63 64 65 66 |
# File 'lib/active_record/relation/query_methods.rb', line 62 def limit(value = true) copy = clone copy.limit_value = value copy end |
#lock(locks = true) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/active_record/relation/query_methods.rb', line 72 def lock(locks = true) case locks when String, TrueClass, NilClass clone.tap {|r| r.lock_value = locks || true } else clone.tap {|r| r.lock_value = false } end end |
#offset(value = true) ⇒ Object
68 69 70 |
# File 'lib/active_record/relation/query_methods.rb', line 68 def offset(value = true) clone.tap {|r| r.offset_value = value } end |
#order(*args) ⇒ Object
37 38 39 |
# File 'lib/active_record/relation/query_methods.rb', line 37 def order(*args) clone.tap {|r| r.order_values += args if args.present? } end |
#preload(*args) ⇒ Object
21 22 23 |
# File 'lib/active_record/relation/query_methods.rb', line 21 def preload(*args) clone.tap {|r| r.preload_values += args if args.present? } end |
#readonly(value = true) ⇒ Object
81 82 83 |
# File 'lib/active_record/relation/query_methods.rb', line 81 def readonly(value = true) clone.tap {|r| r.readonly_value = value } end |
#reorder(*args) ⇒ Object
41 42 43 |
# File 'lib/active_record/relation/query_methods.rb', line 41 def reorder(*args) clone.tap {|r| r.order_values = args if args.present? } end |
#reverse_order ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/active_record/relation/query_methods.rb', line 98 def reverse_order order_clause = arel.order_clauses.join(', ') relation = except(:order) order = order_clause.blank? ? "#{@klass.table_name}.#{@klass.primary_key} DESC" : reverse_sql_order(order_clause) relation.order Arel::SqlLiteral.new order end |
#select(*args) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/active_record/relation/query_methods.rb', line 25 def select(*args) if block_given? to_a.select {|*block_args| yield(*block_args) } else clone.tap {|r| r.select_values += args if args.present? } end end |
#where(opts, *rest) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/active_record/relation/query_methods.rb', line 50 def where(opts, *rest) value = build_where(opts, rest) copy = clone copy.where_values += Array.wrap(value) if value copy end |