From 3fb89b9858b6e4c6865602b574ae35fe5c1213d1 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sun, 26 May 2024 20:59:10 -0700 Subject: [PATCH] Adding Download ALL vs Active CSV Button --- app/controllers/ticket_requests_controller.rb | 5 ++++- app/models/ticket_request.rb | 7 +++++-- .../_table_ticket_request_statuses.html.haml | 7 +++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/ticket_requests_controller.rb b/app/controllers/ticket_requests_controller.rb index e07118ae..b1604cb3 100644 --- a/app/controllers/ticket_requests_controller.rb +++ b/app/controllers/ticket_requests_controller.rb @@ -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 @@ -261,6 +263,7 @@ def permitted_params :password, :authenticity_token, :commit, + :type, :_method, ticket_request: [ :user_id, diff --git a/app/models/ticket_request.rb b/app/models/ticket_request.rb index 230c51c5..c41e467d 100644 --- a/app/models/ticket_request.rb +++ b/app/models/ticket_request.rb @@ -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 = [] @@ -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) diff --git a/app/views/ticket_requests/_table_ticket_request_statuses.html.haml b/app/views/ticket_requests/_table_ticket_request_statuses.html.haml index 78f8c9c3..b0da7113 100644 --- a/app/views/ticket_requests/_table_ticket_request_statuses.html.haml +++ b/app/views/ticket_requests/_table_ticket_request_statuses.html.haml @@ -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