-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharel_assist.rb
81 lines (73 loc) · 2.4 KB
/
arel_assist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
require_relative "arel_assist/version"
require "active_record"
module ArelAssist
extend self
def self.included(base)
base.extend self
end
# Generates SQL: COUNT("<table-name>"."<column-name>")
#
# @example
# arel_count(Post.arel_table[:days_active])
# #=> 'COUNT("member_summaries"."days_active")'
#
# @param [Arel::Attributes::Attribute] field
# @return [Arel::Nodes::NamedFunction]
def arel_count(field)
Arel::Nodes::NamedFunction.new("COUNT", [field])
end
# Generates SQL: date_trunc('<date-part>', "<table-name>"."<column-name>")
#
# @example
# arel_date_trunc('day', MemberSummary.arel_table[:created_at])
# #=> "date_trunc('day', \"member_summaries\".\"created_at\")"
#
# @param [String,Symbol] date_part - one of the following: year, month, day, hour, minute, second, millisecond, microsecond
# @param [Arel::Attributes::Attribute] field
# @return [Arel::Nodes::NamedFunction]
def arel_date_trunc(date_part, field)
Arel::Nodes::NamedFunction.new("date_trunc", [sqlv(date_part), field])
end
# Generates SQL: MAX("<table-name>"."<column-name>")
#
# @example
# arel_max(Activity.arel_table[:occurred_at])
# #=> 'MAX("activities"."occurred_at")'
#
# @param [Arel::Attributes::Attribute] field
# @return [Arel::Nodes::NamedFunction]
def arel_max(field)
Arel::Nodes::NamedFunction.new("MAX", [field])
end
# Generates SQL: MIN("<table-name>"."<column-name>")
#
# @example
# arel_max(Activity.arel_table[:occurred_at])
# #=> 'MIN("activities"."occurred_at")'
#
# @example with 'AS'
# arel_max(Activity.arel_table[:occurred_at]).as("first_activity_occurred_at")
# #=> 'MIN("activities"."occurred_at") AS first_activity_occurred_at'
#
# @param [Arel::Attributes::Attribute] field
# @return [Arel::Nodes::NamedFunction]
def arel_min(field)
Arel::Nodes::NamedFunction.new("MIN", [field])
end
def sqlv(node)
case node
when ->(n) { n.respond_to?(:to_sql) }
node.to_sql
when Arel::Attributes::Attribute
Arel::Nodes::SqlLiteral.new("\"#{node.relation.name}\".\"#{node.name}\"")
when Array, Range
value = node.map { |x| x.is_a?(String) ? "'#{x}'" : x }.join(",")
Arel::Nodes::SqlLiteral.new "ARRAY[#{value}]"
when Time, DateTime, Date, String
Arel::Nodes.build_quoted node
else
Arel::Nodes::SqlLiteral.new node.to_s
end
end
end