Skip to content

Commit

Permalink
Merge pull request #1288 from SplitTime/oveson/lottery-calculations-d…
Browse files Browse the repository at this point in the history
…etails

Lottery calculations details
  • Loading branch information
moveson authored Dec 2, 2024
2 parents db56970 + 1ea460e commit a6a3cdd
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 95 deletions.
7 changes: 7 additions & 0 deletions app/helpers/lottery_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def link_to_entrant_edit(entrant)
link_to fa_icon("pencil-alt"), url, options
end

def options_for_calculation_class(lottery)
directory_path = Rails.root.join("app", "models", "lotteries", "calculations")
files = Dir.glob("#{directory_path}/*")
class_names = files.map { |file| file.split("/").last.split(".").first.classify }
options_for_select(class_names, selected: lottery.calculation_class)
end

def pre_selected_badge_with_label(entrant, tag: :h5)
content_tag(tag) do
concat "Pre-selected: "
Expand Down
1 change: 1 addition & 0 deletions app/madmin/resources/lottery_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class LotteryResource < Madmin::Resource
attribute :updated_at, form: false
attribute :concealed
attribute :status
attribute :calculation_class

# Associations
attribute :partners
Expand Down
29 changes: 29 additions & 0 deletions app/models/lotteries/calculations/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class Lotteries::Calculations::Base < ApplicationRecord
include PgSearch::Model

self.abstract_class = true

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

pg_search_scope :person_search,
associated_against: {
person: [:first_name, :last_name, :city, :state_name, :country_name]
}

belongs_to :organization
belongs_to :person

delegate :bio, :flexible_geolocation, :name, to: :person

def self.search_default_none(param)
return none unless param && param.size > 2

person_search(param)
end
end
15 changes: 6 additions & 9 deletions app/models/lotteries/calculations/hardrock_2025.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# frozen_string_literal: true

class Lotteries::Calculations::Hardrock2025 < ApplicationRecord
class Lotteries::Calculations::Hardrock2025 < Lotteries::Calculations::Base
# self.table_name must be set for a Lotteries::Calculations class to work
self.table_name = :lotteries_calculations_hardrock_2025s

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

belongs_to :organization
belongs_to :person
# self.primary_key must be set to :id for PgSearch to work
# Also, the view must return an "id" column
# Use select row_number() over () as id if no other unique id is available
self.primary_key = :id
end
6 changes: 6 additions & 0 deletions app/presenters/lottery_calculations_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def initialize(lottery, view_context)
validate_setup
end

def calculation_applicants_default_none
lottery_calculation
.joins(:person)
.search_default_none(search_text)
end

def division_calculations
@division_calculations ||=
lottery_calculation.group(:division).pluck(Arel.sql("division, count(*), sum(ticket_count)")).map do |row|
Expand Down
8 changes: 8 additions & 0 deletions app/views/historical_facts/_calculations_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%# locals: (fact:) %>

<tr class="align-middle">
<td><%= historical_fact_kind_badge(fact.kind) %></td>
<td class="text-center"><%= fact.year %></td>
<td class="text-center"><%= fact.quantity %></td>
<td><%= fact.comments %></td>
</tr>
35 changes: 35 additions & 0 deletions app/views/lotteries/_calculations_applicant.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%# locals: (record:) %>

<%= turbo_frame_tag dom_id(record) do %>
<div class="card bg-light mt-3">
<div class="card-header">
<div class="row">
<div class="col">
<h5 class="fw-bold"><%= record.name %></h5>
<h6><%= record.flexible_geolocation %></h6>
<h6><%= record.bio %></h6>
</div>
<div class="col text-end">
<h4><span class="badge bg-secondary"><%= record.division %></span></h4>
<h6 class="fw-bold mb-4"><%= "#{pluralize(record.ticket_count, 'ticket')}" %></h6>
</div>
</div>
</div>

<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Kind</th>
<th class="text-center">Year</th>
<th class="text-center">Quantity</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
<%= render partial: "historical_facts/calculations_row", collection: record.person.historical_facts.ordered, as: :fact %>
</tbody>
</table>
</div>
</div>
<% end %>
16 changes: 16 additions & 0 deletions app/views/lotteries/_calculations_applicant_lookup.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%# locals: (presenter:) -%>

<%= form_tag calculations_organization_lottery_path(presenter.organization, presenter.lottery, anchor: "applicant_lookup_input"),
class: "w-100",
method: :get do %>
<div id="applicant_lookup_input" class="input-group">
<%= text_field_tag "filter[search]",
prepared_params[:search],
placeholder: "Find an applicant",
autofocus: true,
class: "form-control search-box" %>
<%= button_tag(type: :submit, id: "calculations-applicant-lookup-submit", class: "btn btn-primary input-group-text") do %>
<i class="fas fa-search"></i>
<% end %>
</div>
<% end %>
42 changes: 42 additions & 0 deletions app/views/lotteries/_calculations_division_card.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%# locals: (presenter:) %>

<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>
42 changes: 42 additions & 0 deletions app/views/lotteries/_calculations_gender_card.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%# locals: (presenter:) %>

<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>
18 changes: 15 additions & 3 deletions app/views/lotteries/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div class="row">
<div class="col-md-12">
<%= form_with(model: [@lottery.organization, @lottery], local: true, html: {class: "form-horizontal", data: {turbo: false}}) do |f| %>
<%= form_with(model: [@lottery.organization, @lottery], local: true, html: { class: "form-horizontal", data: { turbo: false } }) do |f| %>
<div class="mb-3">
<div class="control-label col">
<%= f.label :name %>
Expand All @@ -29,7 +29,19 @@
<%= f.select :status,
options_for_select(::Lottery.statuses.map { |key, value| [key.titleize, ::Lottery.statuses.key(value)] }, selected: @lottery.status),
{},
{class: "form-control dropdown-select-field"} %>
{ class: "form-control dropdown-select-field" } %>
</div>
</div>

<div class="mb-3">
<div class="control-label col-sm-2">
<%= f.label :calculation_class %>
</div>
<div class="col-sm-8">
<%= f.select :calculation_class,
options_for_calculation_class(@lottery),
{},
{ class: "form-control dropdown-select-field" } %>
</div>
</div>

Expand All @@ -52,5 +64,5 @@
<span class="brackets"><%= link_to 'Cancel', @lottery.new_record? ? organization_lotteries_path(@lottery.organization) : setup_organization_lottery_path(@lottery.organization, @lottery) %></span>
</div>
<% end %>
</div>
</div>
</div>
Loading

0 comments on commit a6a3cdd

Please sign in to comment.