Class: ActiveRecordQueryFixer
- Inherits:
-
Object
- Object
- ActiveRecordQueryFixer
show all
- Defined in:
- lib/active_record_query_fixer.rb,
lib/active_record_query_fixer/version.rb
Defined Under Namespace
Modules: RelationExtentions
Constant Summary
collapse
- VERSION =
"0.0.16".freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ActiveRecordQueryFixer.
12
13
14
15
|
# File 'lib/active_record_query_fixer.rb', line 12
def initialize(args)
@query = args.fetch(:query)
@count_select = 0
end
|
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
6
7
8
|
# File 'lib/active_record_query_fixer.rb', line 6
def query
@query
end
|
Class Method Details
.fix(query) ⇒ Object
8
9
10
|
# File 'lib/active_record_query_fixer.rb', line 8
def self.fix(query)
new(query: query).fix.query
end
|
Instance Method Details
#fix ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/active_record_query_fixer.rb', line 17
def fix
fix_reference_group if fix_reference_group?
fix_order_group if fix_order_group?
fix_order_select_distinct if fix_order_select_distinct?
fix_select_group if query.values[:select] && query.values[:group]
self
end
|
#fix_order_group ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/active_record_query_fixer.rb', line 46
def fix_order_group
@query = query.group(query.model.arel_table[query.model.primary_key])
sort_targets.each do |sort_target|
fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
next if !fields || fields.length != 2
table = fields.dig(0, "String", "str")
column = fields.dig(1, "String", "str")
@query = query.group("#{table}.#{column}") if table && column
end
self
end
|
#fix_order_select_distinct ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/active_record_query_fixer.rb', line 62
def fix_order_select_distinct
select_appends = []
sort_targets.each do |sort_target|
fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
next if !fields || fields.length != 2
table = fields.dig(0, "String", "str")
column = fields.dig(1, "String", "str")
next if !table || !column
select_appends << "#{table}.#{column} AS active_record_query_fixer_#{@count_select}"
@count_select += 1
end
prepend_table_wildcard if !table_wildcard_prepended? && select_appends.any? && query.values[:select].blank?
select_appends.each do |select_append|
@query = query.select(select_append)
end
self
end
|
#fix_reference_group ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/active_record_query_fixer.rb', line 88
def fix_reference_group
@query = query.group(query.model.arel_table[query.model.primary_key])
query.values[:references].each do |reference|
@query = query.group("#{reference}.id")
end
self
end
|
#fix_select_group ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/active_record_query_fixer.rb', line 26
def fix_select_group
select_targets.each do |select_target|
fields = select_target.dig!("ResTarget", "val").dig("ColumnRef", "fields")
next if !fields || fields.length != 2
table = fields[0].dig("String", "str")
column = fields[1].dig("String", "str")
if column
@query = query.group("#{table}.#{column}")
elsif fields[1].key?("A_Star")
@query = query.group("#{table}.id")
end
end
self
end
|