Skip to content

Commit

Permalink
Route, action, policy, and view for lottery calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
moveson committed Dec 2, 2024
1 parent 8bc1bf8 commit 3ff85bc
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/lotteries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def destroy
end
end

# GET /organizations/:organization_id/lotteries/:id/calculations
def calculations
@presenter = LotteryCalculationsPresenter.new(@lottery, view_context)
end

# GET /organizations/:organization_id/lotteries/:id/draw_tickets
def draw_tickets
@presenter = LotteryPresenter.new(@lottery, view_context)
Expand Down
6 changes: 6 additions & 0 deletions app/models/lotteries/calculations/hardrock_2025.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
class Lotteries::Calculations::Hardrock2025 < ApplicationRecord
self.table_name = :lotteries_calculations_hardrock_2025s

enum gender: {
male: 0,
female: 1,
nonbinary: 2,
}

belongs_to :organization
belongs_to :person
end
4 changes: 4 additions & 0 deletions app/policies/lottery_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def export_entrants?
user.authorized_for_lotteries?(organization)
end

def calculations?
user.authorized_for_lotteries?(organization)
end

def draw?
user.authorized_for_lotteries?(organization)
end
Expand Down
61 changes: 61 additions & 0 deletions app/presenters/lottery_calculations_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

class LotteryCalculationsPresenter < LotteryPresenter
CalculatedGroup = Struct.new(:name, :entrants_count, :tickets_count)

delegate :calculation_class, to: :lottery

def initialize(lottery, view_context)
super
validate_setup
end

def division_calculations
@division_calculations ||=
lottery_calculation.group(:division).pluck(Arel.sql("division, count(*), sum(ticket_count)")).map do |row|
CalculatedGroup.new(*row)
end
end

def division_entrants_count
division_calculations.sum(&:entrants_count)
end

def division_tickets_count
division_calculations.sum(&:tickets_count)
end

def gender_calculations
@gender_calculations ||=
lottery_calculation.group(:gender).pluck(Arel.sql("gender, count(*), sum(ticket_count)")).map do |row|
CalculatedGroup.new(*row)
end
end

def gender_entrants_count
gender_calculations.sum(&:entrants_count)
end

def gender_tickets_count
gender_calculations.sum(&:tickets_count)
end

private

def calculated_division_names
@calculated_divisions ||= lottery_calculation.group(:division).pluck(:division)
end

def lottery_calculation
@lottery_calculation ||= lottery_calculation_class.where(organization: organization)
end

def lottery_calculation_class
"Lotteries::Calculations::#{calculation_class}".safe_constantize
end

def validate_setup
raise "Lottery must have an assigned calculation_class" unless calculation_class.present?
raise "Lottery calculation class does not exist" unless lottery_calculation_class.present?
end
end
7 changes: 7 additions & 0 deletions app/views/lotteries/_admin_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%# locals: (presenter:) %>

<ul class="nav nav-tabs nav-tabs-ost" data-turbo="false">
<%= content_tag :li, class: "nav-item #{'active' if presenter.action_name == 'setup'}" do %>
<%= link_to "Setup", setup_organization_lottery_path(presenter.organization, presenter.lottery) %>
Expand All @@ -11,4 +13,9 @@
<%= content_tag :li, class: "nav-item #{'active' if presenter.controller_name == 'partners'}" do %>
<%= link_to "Partners", organization_lottery_partners_path(presenter.organization, presenter.lottery) %>
<% end %>
<% if presenter.lottery.calculation_class? %>
<%= content_tag :li, class: "nav-item #{'active' if presenter.action_name == 'calculations'}" do %>
<%= link_to "Preview Calculations", calculations_organization_lottery_path(presenter.organization, presenter.lottery) %>
<% end %>
<% end %>
</ul>
124 changes: 124 additions & 0 deletions app/views/lotteries/calculations.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<% content_for :title do %>
<% "OpenSplitTime: Lottery Calculations - #{@presenter.name}" %>
<% end %>

<%= render "lotteries/header", presenter: @presenter, breadcrumbs: ["Calculations"] %>

<aside class="ost-toolbar">
<div class="container">
<%= render partial: "shared/callout_with_link",
locals: {
callout_color: :info,
icon_color: :info,
icon_name: "info-circle",
main_text: "This is the Lottery Calculations Preview Page",
detail_paragraphs: [
"Information shown here is based on Historical Facts that have been entered or imported for your organization, which are used as inputs for the query set up for this Lottery (Query class name: #{@presenter.calculation_class}).",
"To view Historical Facts, click the button to the right.",
"This page shows a summary of tickets and divisions that are currently being calculated for this Lottery, together with a detailed list view showing calculations for each applicant.",
],
link: (button_to "Historical facts", organization_historical_facts_path(@presenter.organization), class: "btn btn-outline-secondary")
} %>
<%= render partial: "shared/callout_with_link",
locals: {
callout_color: :warning,
icon_color: :warning,
icon_name: "exclamation-circle",
main_text: "Note: These are not the actual Divisions and ticket counts for your Lottery",
detail_paragraphs: [
"Click on the Setup tab to see the Divisions and Tickets that have been set up for your Lottery. To transfer the Divisions and tickets from this Preview page to your lottery, click the button to the right.",
],
link: (button_to "Accept calculations", setup_organization_lottery_path(@presenter.organization, @presenter.lottery), class: "btn btn-outline-danger")
} %>
</div>
</aside>

<article class="ost-article container">
<div class="row">
<div class="col">
<div class="card mt-4">
<div class="card-header">
<div class="row">
<div class="col">
<h2 class="fw-bold">By Division</h2>
</div>
</div>
</div>
<div class="card-body table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th class="text-center">Entrants</th>
<th class="text-center">%</th>
<th class="text-center">Tickets</th>
<th class="text-center">%</th>
</tr>
</thead>
<tbody>
<% @presenter.division_calculations.each do |calc| %>
<tr>
<td><%= calc.name %></td>
<td class="text-center"><%= calc.entrants_count %></td>
<th class="text-center"><%= "#{(calc.entrants_count / @presenter.division_entrants_count.to_f * 100).round(1)}%" %></th>
<td class="text-center"><%= calc.tickets_count %></td>
<th class="text-center"><%= "#{(calc.tickets_count / @presenter.division_tickets_count.to_f * 100).round(1)}%" %></th>
</tr>
<% end %>
<tr class="fw-bold bg-light">
<td>Totals</td>
<td class="text-center"><%= @presenter.division_entrants_count %></td>
<th class="text-center">100.0%</th>
<td class="text-center"><%= @presenter.division_tickets_count %></td>
<th class="text-center">100.0%</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>

<div class="col">
<div class="card mt-4">
<div class="card-header">
<div class="row">
<div class="col">
<h2 class="fw-bold">By Gender</h2>
</div>
</div>
</div>
<div class="card-body table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th class="text-center">Entrants</th>
<th class="text-center">%</th>
<th class="text-center">Tickets</th>
<th class="text-center">%</th>
</tr>
</thead>
<tbody>
<% @presenter.gender_calculations.each do |calc| %>
<tr>
<td><%= calc.name.titleize %></td>
<td class="text-center"><%= calc.entrants_count %></td>
<th class="text-center"><%= "#{(calc.entrants_count / @presenter.gender_entrants_count.to_f * 100).round(1)}%" %></th>
<td class="text-center"><%= calc.tickets_count %></td>
<th class="text-center"><%= "#{(calc.tickets_count / @presenter.gender_tickets_count.to_f * 100).round(1)}%" %></th>
</tr>
<% end %>
<tr class="fw-bold bg-light">
<td>Totals</td>
<td class="text-center"><%= @presenter.gender_entrants_count %></td>
<th class="text-center">100.0%</th>
<td class="text-center"><%= @presenter.gender_tickets_count %></td>
<th class="text-center">100.0%</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</article>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
end

resources :lotteries do
member { get :calculations }
member { get :draw_tickets }
member { get :setup }
member { get :withdraw_entrants }
Expand Down

0 comments on commit 3ff85bc

Please sign in to comment.