Fix summary query to leverage ActsAsTenant scopes
All checks were successful
Check usage of free licenses / check-licenses (pull_request) Successful in 1m10s
Add copyright notice / copyright_notice (pull_request) Successful in 1m35s
Run unit tests / unit_tests (pull_request) Successful in 5m11s

This commit is contained in:
Manuel Bustillo 2024-12-08 11:39:50 +01:00
parent 9e097361d0
commit 20127398c6
3 changed files with 18 additions and 23 deletions

View File

@ -23,6 +23,7 @@ gem 'rubytree'
gem 'acts_as_tenant'
gem 'httparty'
gem 'rswag'
gem 'pluck_to_hash'
group :development, :test do
gem 'annotaterb'

View File

@ -222,6 +222,9 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.9)
pluck_to_hash (1.0.2)
activerecord (>= 4.0.2)
activesupport (>= 4.0.2)
pry (0.15.0)
coderay (~> 1.1)
method_source (~> 1.0)
@ -413,6 +416,7 @@ DEPENDENCIES
license_finder
money
pg (~> 1.1)
pluck_to_hash
pry
puma (>= 5.0)
rack-cors

View File

@ -3,29 +3,19 @@
module Groups
class SummaryQuery
def call
ActiveRecord::Base.connection.execute(query).to_a
end
private
def query
<<~SQL.squish
SELECT
groups.id,
groups.name,
groups.icon,
groups.parent_id,
groups.color,
count(*) filter (where status IS NOT NULL) as total,
count(*) filter (where status = 0) as considered,
count(*) filter (where status = 10) as invited,
count(*) filter (where status = 20) as confirmed,
count(*) filter (where status = 30) as declined,
count(*) filter (where status = 40) as tentative
FROM groups
LEFT JOIN guests on groups.id = guests.group_id
GROUP BY groups.id
SQL
Group.left_joins(:guests).group(:id).pluck_to_hash(
:id,
:name,
:icon,
:parent_id,
:color,
Arel.sql('count(*) filter (where status IS NOT NULL) as total'),
Arel.sql('count(*) filter (where status = 0) as considered'),
Arel.sql('count(*) filter (where status = 10) as invited'),
Arel.sql('count(*) filter (where status = 20) as confirmed'),
Arel.sql('count(*) filter (where status = 30) as declined'),
Arel.sql('count(*) filter (where status = 40) as tentative'),
)
end
end
end