Module: ActiveRecord::QueryMethods

Extended by:
ActiveSupport::Concern
Defined in:
activerecord/lib/active_record/relation/query_methods.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from ActiveSupport::Concern

append_features, extended, included

Instance Attribute Details

- (Object) create_with_value

Returns the value of attribute create_with_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def create_with_value
  @create_with_value
end

- (Object) eager_load_values

Returns the value of attribute eager_load_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def eager_load_values
  @eager_load_values
end

- (Object) from_value

Returns the value of attribute from_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def from_value
  @from_value
end

- (Object) group_values

Returns the value of attribute group_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def group_values
  @group_values
end

- (Object) having_values

Returns the value of attribute having_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def having_values
  @having_values
end

- (Object) includes_values

Returns the value of attribute includes_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def includes_values
  @includes_values
end

- (Object) joins_values

Returns the value of attribute joins_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def joins_values
  @joins_values
end

- (Object) limit_value

Returns the value of attribute limit_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def limit_value
  @limit_value
end

- (Object) lock_value

Returns the value of attribute lock_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def lock_value
  @lock_value
end

- (Object) offset_value

Returns the value of attribute offset_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def offset_value
  @offset_value
end

- (Object) order_values

Returns the value of attribute order_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def order_values
  @order_values
end

- (Object) preload_values

Returns the value of attribute preload_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def preload_values
  @preload_values
end

- (Object) readonly_value

Returns the value of attribute readonly_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def readonly_value
  @readonly_value
end

- (Object) select_values

Returns the value of attribute select_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def select_values
  @select_values
end

- (Object) where_values

Returns the value of attribute where_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def where_values
  @where_values
end

Instance Method Details

- (Object) arel



109
110
111
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 109

def arel
  @arel ||= build_arel
end

- (Object) build_arel



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 'activerecord/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

- (Object) build_where(opts, other = [])



167
168
169
170
171
172
173
174
175
176
177
# File 'activerecord/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

- (Object) create_with(value = true)



85
86
87
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 85

def create_with(value = true)
  clone.tap {|r| r.create_with_value = value }
end

- (Object) custom_join_sql(*joins)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'activerecord/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

- (Object) eager_load(*args)



17
18
19
# File 'activerecord/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

- (Object) extending(*modules, &block)



93
94
95
96
# File 'activerecord/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

- (Object) from(value = true)



89
90
91
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 89

def from(value = true)
  clone.tap {|r| r.from_value = value }
end

- (Object) group(*args)



33
34
35
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 33

def group(*args)
  clone.tap {|r| r.group_values += args.flatten if args.present? }
end

- (Object) having(*args)



57
58
59
60
# File 'activerecord/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

- (Object) includes(*args)



12
13
14
15
# File 'activerecord/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

- (Object) joins(*args)



45
46
47
48
# File 'activerecord/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

- (Object) limit(value = true)



62
63
64
65
66
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 62

def limit(value = true)
  copy = clone
  copy.limit_value = value
  copy
end

- (Object) lock(locks = true)



72
73
74
75
76
77
78
79
# File 'activerecord/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

- (Object) offset(value = true)



68
69
70
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 68

def offset(value = true)
  clone.tap {|r| r.offset_value = value }
end

- (Object) order(*args)



37
38
39
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 37

def order(*args)
  clone.tap {|r| r.order_values += args if args.present? }
end

- (Object) preload(*args)



21
22
23
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 21

def preload(*args)
  clone.tap {|r| r.preload_values += args if args.present? }
end

- (Object) readonly(value = true)



81
82
83
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 81

def readonly(value = true)
  clone.tap {|r| r.readonly_value = value }
end

- (Object) reorder(*args)



41
42
43
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 41

def reorder(*args)
  clone.tap {|r| r.order_values = args if args.present? }
end

- (Object) reverse_order



98
99
100
101
102
103
104
105
106
107
# File 'activerecord/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

- (Object) select(*args)



25
26
27
28
29
30
31
# File 'activerecord/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

- (Object) where(opts, *rest)



50
51
52
53
54
55
# File 'activerecord/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