|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module BetterTogether |
| 4 | + module Metrics |
| 5 | + # rubocop:todo Layout/LineLength |
| 6 | + # Manage LinkClickReport records tracking instances of reports run against the BetterTogether::Metrics::LinkClick records |
| 7 | + # rubocop:enable Layout/LineLength |
| 8 | + class LinkClickReportsController < ApplicationController |
| 9 | + before_action :set_link_click_report, only: [:download] |
| 10 | + |
| 11 | + # GET /metrics/link_click_reports |
| 12 | + def index |
| 13 | + @link_click_reports = BetterTogether::Metrics::LinkClickReport.order(created_at: :desc) |
| 14 | + if request.headers['Turbo-Frame'].present? |
| 15 | + render partial: 'better_together/metrics/link_click_reports/index', |
| 16 | + locals: { link_click_reports: @link_click_reports }, layout: false |
| 17 | + else |
| 18 | + render :index |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + # GET /metrics/link_click_reports/new |
| 23 | + def new |
| 24 | + @link_click_report = BetterTogether::Metrics::LinkClickReport.new |
| 25 | + # For LinkClick reports, you might want to let users filter by internal or external clicks. |
| 26 | + # For example, providing a selection list for internal (true) or external (false) clicks. |
| 27 | + @internal_options = [true, false] |
| 28 | + end |
| 29 | + |
| 30 | + # POST /metrics/link_click_reports |
| 31 | + def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength |
| 32 | + opts = { |
| 33 | + from_date: link_click_report_params.dig(:filters, :from_date), |
| 34 | + to_date: link_click_report_params.dig(:filters, :to_date), |
| 35 | + filter_internal: link_click_report_params.dig(:filters, :filter_internal), |
| 36 | + sort_by_total_clicks: link_click_report_params[:sort_by_total_clicks], |
| 37 | + file_format: link_click_report_params[:file_format] |
| 38 | + } |
| 39 | + |
| 40 | + @link_click_report = BetterTogether::Metrics::LinkClickReport.create_and_generate!(**opts) |
| 41 | + |
| 42 | + respond_to do |format| # rubocop:todo Metrics/BlockLength |
| 43 | + if @link_click_report.persisted? |
| 44 | + flash[:notice] = 'Report was successfully created.' |
| 45 | + format.html { redirect_to metrics_link_click_reports_path, notice: flash[:notice] } |
| 46 | + format.turbo_stream do |
| 47 | + render turbo_stream: [ |
| 48 | + turbo_stream.prepend('link_click_reports_table_body', |
| 49 | + partial: 'better_together/metrics/link_click_reports/link_click_report', |
| 50 | + locals: { link_click_report: @link_click_report }), |
| 51 | + turbo_stream.replace('flash_messages', |
| 52 | + partial: 'layouts/better_together/flash_messages', |
| 53 | + locals: { flash: flash }), |
| 54 | + turbo_stream.replace('new_report', '<turbo-frame id="new_report"></turbo-frame>') |
| 55 | + ] |
| 56 | + end |
| 57 | + else |
| 58 | + flash.now[:alert] = 'Error creating report.' |
| 59 | + format.html { render :new, status: :unprocessable_entity } |
| 60 | + format.turbo_stream do |
| 61 | + render turbo_stream: [ |
| 62 | + turbo_stream.update('form_errors', |
| 63 | + partial: 'layouts/errors', |
| 64 | + locals: { object: @link_click_report }), |
| 65 | + turbo_stream.replace('flash_messages', |
| 66 | + partial: 'layouts/better_together/flash_messages', |
| 67 | + locals: { flash: flash }) |
| 68 | + ] |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + # GET /metrics/link_click_reports/:id/download |
| 75 | + # rubocop:todo Metrics/MethodLength |
| 76 | + def download # rubocop:todo Metrics/AbcSize, Metrics/MethodLength |
| 77 | + report = @link_click_report |
| 78 | + if report.report_file.attached? |
| 79 | + # Log the download via a background job. |
| 80 | + BetterTogether::Metrics::TrackDownloadJob.perform_later( |
| 81 | + report, # Fully namespaced model |
| 82 | + report.report_file.filename.to_s, # Filename |
| 83 | + report.report_file.content_type, # Content type |
| 84 | + report.report_file.byte_size, # File size |
| 85 | + I18n.locale.to_s # Locale |
| 86 | + ) |
| 87 | + |
| 88 | + send_data report.report_file.download, |
| 89 | + filename: report.report_file.filename.to_s, |
| 90 | + type: report.report_file.content_type, |
| 91 | + disposition: 'attachment' |
| 92 | + else |
| 93 | + redirect_to metrics_link_click_reports_path, alert: t('resources.download_failed') |
| 94 | + end |
| 95 | + end |
| 96 | + # rubocop:enable Metrics/MethodLength |
| 97 | + |
| 98 | + private |
| 99 | + |
| 100 | + def set_link_click_report |
| 101 | + @link_click_report = BetterTogether::Metrics::LinkClickReport.find(params[:id]) |
| 102 | + end |
| 103 | + |
| 104 | + def link_click_report_params |
| 105 | + params.require(:metrics_link_click_report).permit( |
| 106 | + :sort_by_total_clicks, :file_format, |
| 107 | + filters: %i[from_date to_date filter_internal] |
| 108 | + ) |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | +end |
0 commit comments