DEV: Refactor user_badge select_for_grouping scope (#13334)

This commit is contained in:
Mark VanLandingham 2021-06-08 13:37:42 -05:00 committed by GitHub
parent d500d0cc99
commit a27de199b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,8 @@ class UserBadge < ActiveRecord::Base
belongs_to :notification, dependent: :destroy
belongs_to :post
BOOLEAN_ATTRIBUTES = %w(is_favorite)
scope :grouped_with_count, -> {
group(:badge_id, :user_id)
.select_for_grouping
@ -17,11 +19,8 @@ class UserBadge < ActiveRecord::Base
scope :select_for_grouping, -> {
select(
UserBadge.attribute_names.map do |name|
if name == 'is_favorite'
"BOOL_OR(user_badges.#{name}) AS is_favorite"
else
"MAX(user_badges.#{name}) AS #{name}"
end
operation = BOOLEAN_ATTRIBUTES.include?(name) ? "BOOL_OR" : "MAX"
"#{operation}(user_badges.#{name}) AS #{name}"
end,
'COUNT(*) AS "count"'
)