Module: ActiveRecord::Refined::QueryMethods

Defined in:
lib/active_record/refined.rb

Instance Method Summary collapse

Instance Method Details

#from(value, subquery_name = nil, as: nil) ⇒ Object

A symbol names a table, which ActiveRecord's own from only takes as a string. With as it is selected under another name; when that name is the model's own, from_cte says the same thing without repeating it.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/active_record/refined.rb', line 258

def from(value, subquery_name = nil, as: nil)
  unless value.is_a?(Symbol)
    if as
      raise ArgumentError, "as: needs the table named as a symbol"
    end
    return super(value, subquery_name)
  end
  arel_table = Arel::Table.new(value)
  arel_table = arel_table.alias(as) if as
  super(arel_table, subquery_name)
end

#from_cte(name) ⇒ Object

Selects a CTE in place of the model's own table. The alias is not a choice -- ActiveRecord keeps qualifying columns with the table name, so the model's is the only name that works -- which is why it is taken from the model rather than asked for: with_recursive(tree: [...]).from_cte(:tree)



275
276
277
278
279
280
# File 'lib/active_record/refined.rb', line 275

def from_cte(name)
  unless name.is_a?(Symbol)
    raise ArgumentError, "from_cte takes the CTE's name as a symbol"
  end
  from(name, as: klass.table_name)
end

#group(*args, &block) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/active_record/refined.rb', line 245

def group(*args, &block)
  if block
    result = evaluate_block(&block)
    arel = Array(result).map {|node| to_arel_field(node) }
    super(*arel, &nil)
  else
    super
  end
end

#having(opts = nil, *rest, &block) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/active_record/refined.rb', line 227

def having(opts = nil, *rest, &block)
  if block
    super(evaluate_block(&block).to_arel(table))
  else
    super
  end
end

#joins(*args, as: nil, &block) ⇒ Object

as names the table within the query, which is what makes a self join expressible: joins(:employees, as: :managers) { ... }.



284
285
286
287
288
289
290
291
# File 'lib/active_record/refined.rb', line 284

def joins(*args, as: nil, &block)
  if block
    super(build_join_node(args.first, Arel::Nodes::InnerJoin, as, &block))
  else
    reject_join_alias(as)
    super(*args, &block)
  end
end

#left_outer_joins(*args, as: nil, &block) ⇒ Object



293
294
295
296
297
298
299
300
# File 'lib/active_record/refined.rb', line 293

def left_outer_joins(*args, as: nil, &block)
  if block
    joins(build_join_node(args.first, Arel::Nodes::OuterJoin, as, &block))
  else
    reject_join_alias(as)
    super(*args, &block)
  end
end

#order(*args, &block) ⇒ Object



235
236
237
238
239
240
241
242
243
# File 'lib/active_record/refined.rb', line 235

def order(*args, &block)
  if block
    result = evaluate_block(&block)
    arel = Array(result).map {|node| to_arel_field(node) }
    super(*arel, &nil)
  else
    super
  end
end

#select(*fields, &block) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/active_record/refined.rb', line 217

def select(*fields, &block)
  if block
    result = evaluate_block(&block)
    arel = Array(result).map {|node| to_arel_field(node) }
    super(*arel, &nil)
  else
    super
  end
end

#where(opts = nil, *rest, &block) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/active_record/refined.rb', line 209

def where(opts = nil, *rest, &block)
  if block
    super(evaluate_block(&block).to_arel(table))
  else
    super
  end
end