Fix summary query to leverage ActsAsTenant scopes
This commit is contained in:
parent
9e097361d0
commit
20127398c6
1
Gemfile
1
Gemfile
@ -23,6 +23,7 @@ gem 'rubytree'
|
|||||||
gem 'acts_as_tenant'
|
gem 'acts_as_tenant'
|
||||||
gem 'httparty'
|
gem 'httparty'
|
||||||
gem 'rswag'
|
gem 'rswag'
|
||||||
|
gem 'pluck_to_hash'
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
gem 'annotaterb'
|
gem 'annotaterb'
|
||||||
|
@ -222,6 +222,9 @@ GEM
|
|||||||
ast (~> 2.4.1)
|
ast (~> 2.4.1)
|
||||||
racc
|
racc
|
||||||
pg (1.5.9)
|
pg (1.5.9)
|
||||||
|
pluck_to_hash (1.0.2)
|
||||||
|
activerecord (>= 4.0.2)
|
||||||
|
activesupport (>= 4.0.2)
|
||||||
pry (0.15.0)
|
pry (0.15.0)
|
||||||
coderay (~> 1.1)
|
coderay (~> 1.1)
|
||||||
method_source (~> 1.0)
|
method_source (~> 1.0)
|
||||||
@ -413,6 +416,7 @@ DEPENDENCIES
|
|||||||
license_finder
|
license_finder
|
||||||
money
|
money
|
||||||
pg (~> 1.1)
|
pg (~> 1.1)
|
||||||
|
pluck_to_hash
|
||||||
pry
|
pry
|
||||||
puma (>= 5.0)
|
puma (>= 5.0)
|
||||||
rack-cors
|
rack-cors
|
||||||
|
@ -3,29 +3,19 @@
|
|||||||
module Groups
|
module Groups
|
||||||
class SummaryQuery
|
class SummaryQuery
|
||||||
def call
|
def call
|
||||||
ActiveRecord::Base.connection.execute(query).to_a
|
Group.left_joins(:guests).group(:id).pluck_to_hash(
|
||||||
end
|
:id,
|
||||||
|
:name,
|
||||||
private
|
:icon,
|
||||||
|
:parent_id,
|
||||||
def query
|
:color,
|
||||||
<<~SQL.squish
|
Arel.sql('count(*) filter (where status IS NOT NULL) as total'),
|
||||||
SELECT
|
Arel.sql('count(*) filter (where status = 0) as considered'),
|
||||||
groups.id,
|
Arel.sql('count(*) filter (where status = 10) as invited'),
|
||||||
groups.name,
|
Arel.sql('count(*) filter (where status = 20) as confirmed'),
|
||||||
groups.icon,
|
Arel.sql('count(*) filter (where status = 30) as declined'),
|
||||||
groups.parent_id,
|
Arel.sql('count(*) filter (where status = 40) as tentative'),
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user