|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | class SchoolMoves::ExportsController < ApplicationController |
4 | | - before_action :set_school_move_export |
5 | | - |
6 | | - include WizardControllerConcern |
7 | | - |
8 | 4 | skip_after_action :verify_policy_scoped |
9 | 5 |
|
10 | | - def create |
11 | | - @school_move_export.clear! |
12 | | - redirect_to school_move_export_path(Wicked::FIRST_STEP) |
| 6 | + def new |
| 7 | + authorize Export.new(team: current_team, user: current_user) |
| 8 | + @form = SchoolMovesExportForm.new |
13 | 9 | end |
14 | 10 |
|
15 | | - def show |
16 | | - render_wizard |
17 | | - end |
| 11 | + def create |
| 12 | + @form = SchoolMovesExportForm.new(**create_params) |
18 | 13 |
|
19 | | - def update |
20 | | - @school_move_export.assign_attributes(update_params) |
| 14 | + if from_and_to_dates_valid? && @form.valid? |
| 15 | + exportable = |
| 16 | + SchoolMovesExport.new( |
| 17 | + date_from: @form.date_from, |
| 18 | + date_to: @form.date_to |
| 19 | + ) |
| 20 | + @export = |
| 21 | + Export.from_exportable( |
| 22 | + exportable, |
| 23 | + user: current_user, |
| 24 | + team: current_team |
| 25 | + ) |
21 | 26 |
|
22 | | - render_wizard @school_move_export |
23 | | - end |
| 27 | + authorize @export |
| 28 | + |
| 29 | + @export.save! |
| 30 | + GenerateExportJob.perform_later(@export) |
24 | 31 |
|
25 | | - def download |
26 | | - send_data( |
27 | | - @school_move_export.csv_data, |
28 | | - filename: @school_move_export.csv_filename |
29 | | - ) |
| 32 | + flash[:success] = { |
| 33 | + heading: t("exports_flash.heading"), |
| 34 | + heading_link_text: t("exports_flash.heading_link_text"), |
| 35 | + heading_link_href: downloads_path |
| 36 | + } |
| 37 | + redirect_to downloads_path |
| 38 | + else |
| 39 | + authorize Export.new(team: current_team, user: current_user) |
| 40 | + |
| 41 | + @form.date_from = date_from_validator.date_params_as_struct |
| 42 | + @form.date_to = date_to_validator.date_params_as_struct |
| 43 | + render :new, status: :unprocessable_content |
| 44 | + end |
30 | 45 | end |
31 | 46 |
|
32 | | - def finish_wizard_path |
33 | | - download_school_move_export_path |
| 47 | + private |
| 48 | + |
| 49 | + def create_params |
| 50 | + raw = params.fetch(:school_moves_export_form, {}) |
| 51 | + { |
| 52 | + date_from: |
| 53 | + begin |
| 54 | + Date.new( |
| 55 | + raw["date_from(1i)"].to_i, |
| 56 | + raw["date_from(2i)"].to_i, |
| 57 | + raw["date_from(3i)"].to_i |
| 58 | + ) |
| 59 | + rescue StandardError |
| 60 | + nil |
| 61 | + end, |
| 62 | + date_to: |
| 63 | + begin |
| 64 | + Date.new( |
| 65 | + raw["date_to(1i)"].to_i, |
| 66 | + raw["date_to(2i)"].to_i, |
| 67 | + raw["date_to(3i)"].to_i |
| 68 | + ) |
| 69 | + rescue StandardError |
| 70 | + nil |
| 71 | + end |
| 72 | + } |
34 | 73 | end |
35 | 74 |
|
36 | | - def set_school_move_export |
37 | | - @school_move_export = |
38 | | - authorize SchoolMoveExport.new(request_session: session, current_user:) |
| 75 | + def from_and_to_dates_valid? |
| 76 | + date_from_validator.date_params_valid? && |
| 77 | + date_to_validator.date_params_valid? |
39 | 78 | end |
40 | 79 |
|
41 | | - def set_steps |
42 | | - self.steps = @school_move_export.wizard_steps |
| 80 | + def date_from_validator |
| 81 | + @date_from_validator ||= |
| 82 | + DateParamsValidator.new( |
| 83 | + field_name: :date_from, |
| 84 | + object: @form, |
| 85 | + params: params.fetch(:school_moves_export_form, {}) |
| 86 | + ) |
43 | 87 | end |
44 | 88 |
|
45 | | - def update_params |
46 | | - params |
47 | | - .fetch(:school_move_export, {}) |
48 | | - .permit(:date_from, :date_to) |
49 | | - .merge(wizard_step: current_step) |
| 89 | + def date_to_validator |
| 90 | + @date_to_validator ||= |
| 91 | + DateParamsValidator.new( |
| 92 | + field_name: :date_to, |
| 93 | + object: @form, |
| 94 | + params: params.fetch(:school_moves_export_form, {}) |
| 95 | + ) |
50 | 96 | end |
51 | 97 | end |
0 commit comments