Module: ActiveRecord::QueryMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#create_with_valueObject

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_valuesObject

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_valueObject

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_valuesObject

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_valuesObject

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_valuesObject

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_valuesObject

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_valueObject

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_valueObject

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_valueObject

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_valuesObject

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_valuesObject

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_valueObject

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

#reorder_flagObject

Returns the value of attribute reorder_flag.



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

def reorder_flag
  @reorder_flag
end

#select_valuesObject

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_valuesObject

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

#arelObject



148
149
150
# File 'lib/active_record/relation/query_methods.rb', line 148

def arel
  @arel ||= build_arel
end

#build_arelObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/active_record/relation/query_methods.rb', line 173

def build_arel
  arel = table

  arel = build_joins(arel, @joins_values) unless @joins_values.empty?

  arel = collapse_wheres(arel, (@where_values - ['']).uniq)

  arel = arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?

  arel = arel.take(connection.sanitize_limit(@limit_value)) if @limit_value
  arel = arel.skip(@offset_value) if @offset_value

  arel = arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?

  arel = arel.order(*@order_values.uniq.reject{|o| o.blank?}) 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

#create_with(value) ⇒ Object



117
118
119
120
121
# File 'lib/active_record/relation/query_methods.rb', line 117

def create_with(value)
  relation = clone
  relation.create_with_value = value
  relation
end

#custom_join_sql(*joins) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/active_record/relation/query_methods.rb', line 152

def custom_join_sql(*joins)
  arel = table.select_manager

  joins.each do |join|
    next if join.blank?

    @implicit_readonly = true

    case join
    when Array
      join = Arel.sql(join.join(' ')) if array_of_strings?(join)
    when String
      join = Arel.sql(join)
    end

    arel.join(join)
  end

  arel.join_sql
end

#eager_load(*args) ⇒ Object



22
23
24
25
26
# File 'lib/active_record/relation/query_methods.rb', line 22

def eager_load(*args)
  relation = clone
  relation.eager_load_values += args unless args.blank?
  relation
end

#extending(*modules, &block) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/active_record/relation/query_methods.rb', line 129

def extending(*modules, &block)
  modules << Module.new(&block) if block_given?

  relation = clone
  relation.send(:apply_modules, modules.flatten)
  relation
end

#from(value) ⇒ Object



123
124
125
126
127
# File 'lib/active_record/relation/query_methods.rb', line 123

def from(value)
  relation = clone
  relation.from_value = value
  relation
end

#group(*args) ⇒ Object



44
45
46
47
48
# File 'lib/active_record/relation/query_methods.rb', line 44

def group(*args)
  relation = clone
  relation.group_values += args.flatten unless args.blank?
  relation
end

#having(*args) ⇒ Object



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

def having(*args)
  relation = clone
  relation.having_values += build_where(*args) unless args.blank?
  relation
end

#includes(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/active_record/relation/query_methods.rb', line 12

def includes(*args)
  args.reject! {|a| a.blank? }

  return clone if args.empty?

  relation = clone
  relation.includes_values = (relation.includes_values + args).flatten.uniq
  relation
end

#joins(*args) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/active_record/relation/query_methods.rb', line 65

def joins(*args)
  relation = clone

  args.flatten!
  relation.joins_values += args unless args.blank?

  relation
end

#limit(value) ⇒ Object



86
87
88
89
90
# File 'lib/active_record/relation/query_methods.rb', line 86

def limit(value)
  relation = clone
  relation.limit_value = value
  relation
end

#lock(locks = true) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_record/relation/query_methods.rb', line 98

def lock(locks = true)
  relation = clone

  case locks
  when String, TrueClass, NilClass
    relation.lock_value = locks || true
  else
    relation.lock_value = false
  end

  relation
end

#offset(value) ⇒ Object



92
93
94
95
96
# File 'lib/active_record/relation/query_methods.rb', line 92

def offset(value)
  relation = clone
  relation.offset_value = value
  relation
end

#order(*args) ⇒ Object



50
51
52
53
54
# File 'lib/active_record/relation/query_methods.rb', line 50

def order(*args)
  relation = clone
  relation.order_values += args.flatten unless args.blank?
  relation
end

#preload(*args) ⇒ Object



28
29
30
31
32
# File 'lib/active_record/relation/query_methods.rb', line 28

def preload(*args)
  relation = clone
  relation.preload_values += args unless args.blank?
  relation
end

#readonly(value = true) ⇒ Object



111
112
113
114
115
# File 'lib/active_record/relation/query_methods.rb', line 111

def readonly(value = true)
  relation = clone
  relation.readonly_value = value
  relation
end

#reorder(*args) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/active_record/relation/query_methods.rb', line 56

def reorder(*args)
  relation = clone
  unless args.blank?
    relation.order_values = args
    relation.reorder_flag = true
  end
  relation
end

#reverse_orderObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/active_record/relation/query_methods.rb', line 137

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.sql(order))
end

#select(value = Proc.new) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/active_record/relation/query_methods.rb', line 34

def select(value = Proc.new)
  if block_given?
    to_a.select {|*block_args| value.call(*block_args) }
  else
    relation = clone
    relation.select_values += Array.wrap(value)
    relation
  end
end

#where(opts, *rest) ⇒ Object



74
75
76
77
78
# File 'lib/active_record/relation/query_methods.rb', line 74

def where(opts, *rest)
  relation = clone
  relation.where_values += build_where(opts, rest) unless opts.blank?
  relation
end