Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize lottery views #1356

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/models/lottery_entrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ class LotteryEntrant < ApplicationRecord
validates_presence_of :first_name, :last_name, :gender, :number_of_tickets
validates_with ::LotteryEntrantUniqueValidator

# @param [String] param
# @return [ActiveRecord::Relation<LotteryEntrant>]
def self.search(param)
return all unless param.present?
return none unless param.size > 2

search_names_and_locations(param)
end

# @param [String] param
# @return [ActiveRecord::Relation<LotteryEntrant>]
def self.search_default_none(param)
return none unless param && param.size > 2

Expand All @@ -56,6 +60,7 @@ def self.search_default_none(param)
delegate :organization, to: :lottery
delegate :draw_status, :accepted?, :waitlisted?, to: :division_ranking

# @return [String]
def division_name
if attributes.key?("division_name")
attributes["division_name"]
Expand All @@ -73,21 +78,25 @@ def draw_ticket!
lottery.create_draw_for_ticket!(selected_ticket)
end

# @return [Boolean]
def drawn?
tickets.joins(:draw).exists?
end

# @return [Boolean]
def service_completed?
service_detail.present? && service_detail.completed_date?
end

# @return [String]
def to_s
full_name
end

private

# Needed to keep PersonalInfo#bio from breaking
# @return [nil]
def current_age_approximate
nil
end
Expand Down
24 changes: 20 additions & 4 deletions app/presenters/lottery_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ def initialize(lottery, view_context)
@params = view_context.prepared_params
end

# @return [ActiveRecord::Relation<LotteryDivision>]
def ordered_divisions
@ordered_divisions ||= divisions.ordered_by_name
end

# @return [ActiveRecord::Relation<LotteryDraw>]
def lottery_draws_ordered
lottery_draws
.with_sortable_entrant_attributes
.include_entrant_and_division
.most_recent_first
end

# @return [ActiveRecord::Relation<LotteryEntrant>]
def lottery_entrants_default_none
unfiltered_entrants = lottery.entrants
.with_division_name
.includes(:division)
.includes(:division, :division_ranking)
entrant_id = params[:entrant_id]

if entrant_id.present?
Expand All @@ -40,33 +43,39 @@ def lottery_entrants_default_none
end
end

# @return [ActiveRecord::Relation<LotteryEntrant>]
def lottery_entrants_paginated
@lottery_entrants_paginated ||= lottery_entrants_filtered.paginate(page: page, per_page: per_page).to_a
@lottery_entrants_paginated ||= lottery_entrants_filtered.paginate(page: page, per_page: per_page)
end

# @return [String, nil]
def next_page_url
view_context.url_for(request.params.merge(page: page + 1)) if records_from_context_count == per_page
end

# @return [ActiveRecord::Relation<Partner>]
def partners
@partners ||= lottery.partners.order(:name)
end

# @return [ActiveRecord::Relation<LotteryEntrant>, ActiveRecord::Relation<LotteryDraw>]
def records_from_context
case display_style
when "entrants"
lottery_entrants_paginated
when "draws"
lottery_draws
else
[]
lottery_draws.none
end
end

# @return [Integer]
def records_from_context_count
@records_from_context_count ||= records_from_context.size
end

# @return [Hash{String->Array<LotteryDivisionTicketStat>}]
def stats
@stats ||= ::LotteryDivisionTicketStat.where(lottery: lottery).order(:division_name, :number_of_tickets).group_by(&:division_name)
end
Expand All @@ -88,14 +97,17 @@ def stats_chart_data(division_stats)
]
end

# @return [Boolean]
def viewable_results?
lottery.live? || lottery.finished? || current_user&.authorized_for_lotteries?(lottery)
end

# @return [String (frozen)]
def display_style
params[:display_style].presence || default_display_style
end

# @return [String (frozen)]
def default_display_style
case status
when "preview" then "entrants"
Expand All @@ -105,14 +117,17 @@ def default_display_style
end
end

# @return [Partner, nil]
def partner_with_banner
@partner_with_banner ||= lottery.pick_partner_with_banner
end

# @return [Boolean]
def show_partner_banners?
lottery.live? && partner_with_banner.present?
end

# @return [Boolean]
def tickets_not_generated?
@tickets_not_generated ||= lottery_tickets.empty?
end
Expand All @@ -122,9 +137,10 @@ def tickets_not_generated?
attr_reader :view_context
delegate :current_user, :request, to: :view_context, private: true

# @return [ActiveRecord::Relation<LotteryEntrant>]
def lottery_entrants_filtered
lottery_entrants
.includes([:division_ranking, division: { lottery: :organization }])
.includes([:division_ranking, :service_detail, division: { lottery: :organization }])
.search(search_text)
.order(:last_name)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/lotteries/_pre_selected_entrants_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</div>
</div>
<div class="card-body table-responsive" data-visibility-target="element">
<%= render partial: "lottery_entrants/lottery_entrant_admin", collection: presenter.lottery_entrants.pre_selected, as: :record %>
<%= render partial: "lottery_entrants/lottery_entrant_admin", collection: presenter.lottery_entrants.pre_selected.includes(:division_ranking), as: :record %>
</div>
</div>
</div>
12 changes: 6 additions & 6 deletions app/views/lotteries/_results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
<h4 class="card-header fw-bold bg-primary text-white"><%= "#{division.name}" %></h4>
<div class="card-body">
<h5>Accepted</h5>
<% if division.accepted_entrants.present? %>
<% if division.accepted_entrants.any? %>
<ol>
<%= render partial: "entrant_for_results", collection: division.accepted_entrants, as: :entrant %>
<%= render partial: "entrant_for_results", collection: division.accepted_entrants.includes(:service_detail), as: :entrant %>
</ol>
<% else %>
<p>No entrants have been drawn yet</p>
<% end %>

<% if division.waitlisted_entrants.present? %>
<% if division.waitlisted_entrants.any? %>
<h5>Wait List</h5>
<ol>
<%= render partial: "entrant_for_results", collection: division.waitlisted_entrants, as: :entrant %>
<%= render partial: "entrant_for_results", collection: division.waitlisted_entrants.includes(:service_detail), as: :entrant %>
</ol>
<% end %>

<% if division.withdrawn_entrants.present? %>
<% if division.withdrawn_entrants.any? %>
<h5>Withdrawn</h5>
<ol>
<%= render partial: "entrant_for_results", collection: division.withdrawn_entrants, as: :entrant %>
<%= render partial: "entrant_for_results", collection: division.withdrawn_entrants.includes(:service_detail), as: :entrant %>
</ol>
<% end %>
</div>
Expand Down
12 changes: 6 additions & 6 deletions app/views/lotteries/manage_entrants.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
<%= render "lotteries/header", presenter: @presenter, breadcrumbs: ["Manage Entrants"] %>

<article class="ost-article container">
<% if @presenter.lottery_draws.present? %>
<% if @presenter.lottery_draws.any? %>
<% @presenter.ordered_divisions.each do |division| %>
<div class="card">
<h4 class="card-header fw-bold bg-primary text-white"><%= "#{division.name}" %></h4>
<div class="card-body">
<h5>Accepted</h5>
<%= render partial: "manage_entrants_table", locals: { entrants: division.accepted_entrants } %>
<%= render partial: "manage_entrants_table", locals: { entrants: division.accepted_entrants.includes(:service_detail) } %>

<% if division.waitlisted_entrants.present? %>
<% if division.waitlisted_entrants.any? %>
<h5>Wait List</h5>
<%= render partial: "manage_entrants_table", locals: { entrants: division.waitlisted_entrants } %>
<%= render partial: "manage_entrants_table", locals: { entrants: division.waitlisted_entrants.includes(:service_detail) } %>
<% end %>

<% if division.withdrawn_entrants.present? %>
<% if division.withdrawn_entrants.any? %>
<h5>Withdrawn</h5>
<%= render partial: "manage_entrants_table", locals: { entrants: division.withdrawn_entrants } %>
<%= render partial: "manage_entrants_table", locals: { entrants: division.withdrawn_entrants.includes(:service_detail) } %>
<% end %>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion app/views/lotteries/setup.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@

<div class="container">
<% if @presenter.lottery_entrants_default_none.present? %>
<%= render partial: "lottery_entrants/lottery_entrant_admin", collection: @presenter.lottery_entrants_default_none, as: :record %>
<%= render partial: "lottery_entrants/lottery_entrant_admin",
collection: @presenter.lottery_entrants_default_none,
as: :record %>
<% else %>
<div class="card bg-light mt-2">
<div class="card-body">
Expand Down
Loading