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

Adding Download ALL vs Active CSV Button #195

Merged
merged 1 commit into from
May 27, 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
5 changes: 4 additions & 1 deletion app/controllers/ticket_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ def index
end

def download
download_type = permitted_params[:type] || :active

csv_file = Tempfile.new('csv')

CSV.open(csv_file.path, 'w',
write_headers: true,
headers: TicketRequest.csv_header) do |csv|
TicketRequest.for_csv(@event).each do |row|
TicketRequest.for_csv(@event, type: download_type).each do |row|
csv << row
end
end
Expand Down Expand Up @@ -261,6 +263,7 @@ def permitted_params
:password,
:authenticity_token,
:commit,
:type,
:_method,
ticket_request: [
:user_id,
Expand Down
7 changes: 5 additions & 2 deletions app/models/ticket_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ class << self
# This method returns a two-dimensional array. The first row is the header row,
# and then for each ticket request we return the primary user with the ticket request info,
# followed by one row per guest.
def for_csv(event)
def for_csv(event, type: :active)
[].tap do |table|
event.ticket_requests.active.each do |ticket_request|
raise ArgumentError, "#for_csv: invalid scope #{type}" if type && !TicketRequest.respond_to?(type)

event.ticket_requests.send(type).each do |ticket_request|
# Main Ticket Request User Row
row = []

Expand Down Expand Up @@ -191,6 +193,7 @@ def csv_columns
# Those TicketRequests that should be exported as a Guest List and include user record to avoid N+1
# @see https://medium.com/doctolib/how-to-find-fix-and-prevent-n-1-queries-on-rails-6b30d9cfbbaf
scope :active, -> { where(status: [STATUS_COMPLETED, STATUS_AWAITING_PAYMENT]).includes(:user) }
scope :everything, -> { includes(:user) }

def can_view?(user)
self.user == user || event.admin?(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
.card
.card-header.bg-primary-subtle
.btn-group.fs-5
= link_to download_event_ticket_requests_path(event), class: 'btn btn-warning ' do
= link_to download_event_ticket_requests_path(event, type: "active"), class: 'btn btn-warning ' do
%i.icon-th-list
Download CSV ▼
Download CSV of Approved ▼
= link_to download_event_ticket_requests_path(event, type: "all"), class: 'btn btn-warning ' do
%i.icon-th-list
Download CSV of All ▼
.card-body
.vertical-20

Expand Down