diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9c8e6c7d9..cc9b37261 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -24,6 +24,16 @@ def process_action(*args) head :not_acceptable end + def record_file_download(attachment) + ::Analytics::FileDownload.create( + user: current_user, + record: attachment.record, + name: attachment.name, + filename: attachment.filename, + byte_size: attachment.byte_size, + ) + end + def route_not_found raise ::ActionController::RoutingError, "Route does not exist" end diff --git a/app/controllers/lotteries/entrant_service_details_controller.rb b/app/controllers/lotteries/entrant_service_details_controller.rb index 3f206c72b..794e546f6 100644 --- a/app/controllers/lotteries/entrant_service_details_controller.rb +++ b/app/controllers/lotteries/entrant_service_details_controller.rb @@ -38,6 +38,7 @@ def attach_completed_form def download_completed_form if @service_detail.completed_form.attached? redirect_to @service_detail.completed_form.url(disposition: :attachment), allow_other_host: true + record_file_download(@service_detail.completed_form) else redirect_to organization_lottery_entrant_service_detail_path(@organization, @lottery, @service_detail), notice: "No completed service form is attached" diff --git a/app/controllers/lotteries_controller.rb b/app/controllers/lotteries_controller.rb index de743ee9e..9f2285f52 100644 --- a/app/controllers/lotteries_controller.rb +++ b/app/controllers/lotteries_controller.rb @@ -190,8 +190,8 @@ def attach_service_form # GET /organizations/:organization_id/lotteries/:id/download_service_form def download_service_form if @lottery.service_form.attached? - @lottery.increment!(:service_form_download_count) redirect_to @lottery.service_form.url(disposition: :attachment), allow_other_host: true + record_file_download(@lottery.service_form) else redirect_to setup_organization_lottery_path(@organization, @lottery), notice: "No service form is attached" end diff --git a/app/models/analytics/file_download.rb b/app/models/analytics/file_download.rb new file mode 100644 index 000000000..b008219d7 --- /dev/null +++ b/app/models/analytics/file_download.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class Analytics::FileDownload < ApplicationRecord + belongs_to :user + belongs_to :record, polymorphic: true +end