Skip to content

Commit

Permalink
Fix for Rails 8 issue with select + count
Browse files Browse the repository at this point in the history
This is not ideal but will do as a quick fix.
  • Loading branch information
shioyama committed Dec 1, 2024
1 parent a0457b9 commit 7c8c780
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/mobility/plugins/active_record/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Query

requires :query, include: false

ATTRIBUTE_ALIAS = "__mobility_%s_%s__"

included_hook do |klass, backend_class|
plugin = self
if options[:query]
Expand All @@ -39,7 +41,7 @@ module Query

class << self
def attribute_alias(attribute, locale = Mobility.locale)
"__mobility_%s_%s__" % [attribute, ::Mobility.normalize_locale(locale)]
ATTRIBUTE_ALIAS % [attribute, ::Mobility.normalize_locale(locale)]
end

def build_query(klass, locale = Mobility.locale, &block)
Expand Down Expand Up @@ -182,6 +184,22 @@ def order(opts, *rest)
end
end

# Fix for https://github.com/shioyama/mobility/pull/654#issuecomment-2503479112
def select_for_count
return super unless klass.respond_to?(:mobility_attribute?)

if select_values.any? { |value| value.right.start_with?("__mobility") }
filtered_select_values = select_values.map do |value|
value.right.start_with?("__mobility") ? value.left : value
end
with_connection do |conn|
arel_columns(filtered_select_values).map { |column| conn.visitor.compile(column) }.join(", ")
end
else
super
end
end

# Return backend node for attribute name.
# @param [Symbol,String] name Name of attribute
# @param [Symbol] locale Locale
Expand Down

0 comments on commit 7c8c780

Please sign in to comment.