Class: ActiveRecordQueryFixer
- Inherits:
-
Object
- Object
- ActiveRecordQueryFixer
- 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.14".freeze
Instance Attribute Summary collapse
-
#count_select ⇒ Object
readonly
Returns the value of attribute count_select.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
Instance Method Summary collapse
- #fix ⇒ Object
-
#fix_order_group ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#fix_order_select_distinct ⇒ Object
rubocop:disable Metrics/AbcSize.
- #fix_reference_group ⇒ Object
-
#fix_select_group ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(query:) ⇒ ActiveRecordQueryFixer
constructor
A new instance of ActiveRecordQueryFixer.
Constructor Details
#initialize(query:) ⇒ ActiveRecordQueryFixer
Returns a new instance of ActiveRecordQueryFixer.
15 16 17 18 |
# File 'lib/active_record_query_fixer.rb', line 15 def initialize(query:) @query = query @count_select = 0 end |
Instance Attribute Details
#count_select ⇒ Object (readonly)
Returns the value of attribute count_select.
6 7 8 |
# File 'lib/active_record_query_fixer.rb', line 6 def count_select @count_select end |
#query ⇒ Object (readonly)
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
11 12 13 |
# File 'lib/active_record_query_fixer.rb', line 11 def self.fix(query) new(query: query).fix.query end |
Instance Method Details
#fix ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/active_record_query_fixer.rb', line 20 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
rubocop:disable Metrics/AbcSize
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_record_query_fixer.rb', line 49 def fix_order_group # rubocop:disable Metrics/AbcSize @query = query.group(query.model.arel_table[query.model.primary_key]) sort_targets.each do |sort_target| fields = sort_target.sort_by.node.column_ref&.fields next if !fields || fields.length != 2 table = fields[0].string.str column = fields[1].string.str @query = query.group("#{quote_table_name(table)}.#{quote_column_name(column)}") if table && column end self end |
#fix_order_select_distinct ⇒ Object
rubocop:disable Metrics/AbcSize
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_record_query_fixer.rb', line 66 def fix_order_select_distinct # rubocop:disable Metrics/AbcSize select_appends = [] sort_targets.each do |sort_target| fields = sort_target.sort_by.node.column_ref.fields next if !fields || fields.length != 2 table = fields[0].string.str column = fields[1].string.str next if !table || !column select_appends << "#{quote_table_name(table)}.#{quote_column_name(column)} AS active_record_query_fixer_#{count_select}" @count_select += 1 end # Start by prepending a wild-card select before doing the fix-selects to avoid any issues with `DISTINCT COUNT` 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
93 94 95 96 97 98 99 100 101 |
# File 'lib/active_record_query_fixer.rb', line 93 def fix_reference_group @query = query.group(query.model.arel_table[query.model.primary_key]) query.values[:references].each do |reference| @query = query.group("#{quote_table_name(reference)}.#{quote_column_name("id")}") end self end |
#fix_select_group ⇒ Object
rubocop:disable Metrics/AbcSize
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_record_query_fixer.rb', line 29 def fix_select_group # rubocop:disable Metrics/AbcSize select_targets.each do |select_target| fields = select_target.res_target.val.column_ref.fields next if !fields || fields.length != 2 table = fields[0].string.str column = fields[1].string&.str if column # A table and a column has been selected - make sure to group by that @query = query.group("#{quote_table_name(table)}.#{quote_column_name(column)}") elsif fields[1].a_star # A table and a star has been selected - assume the primary key is called "id" and group by that @query = query.group("#{quote_table_name(table)}.#{quote_column_name("id")}") end end self end |