|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module AdminV2 |
| 4 | + class SchoolYearsController < BaseController |
| 5 | + before_action :set_school_year, only: %i[show edit update destroy] |
| 6 | + |
| 7 | + def index |
| 8 | + @school_years = apply_sorting(SchoolYear.includes(:school, :year), default: "id") |
| 9 | + |
| 10 | + @breadcrumbs = [ |
| 11 | + { label: "School Years" } |
| 12 | + ] |
| 13 | + end |
| 14 | + |
| 15 | + def show |
| 16 | + @breadcrumbs = [ |
| 17 | + { label: "School Years", path: admin_v2_school_years_path }, |
| 18 | + { label: @school_year.to_s } |
| 19 | + ] |
| 20 | + end |
| 21 | + |
| 22 | + def new |
| 23 | + @school_year = SchoolYear.new |
| 24 | + |
| 25 | + @breadcrumbs = [ |
| 26 | + { label: "School Years", path: admin_v2_school_years_path }, |
| 27 | + { label: "New School Year" } |
| 28 | + ] |
| 29 | + end |
| 30 | + |
| 31 | + def edit |
| 32 | + @breadcrumbs = [ |
| 33 | + { label: "School Years", path: admin_v2_school_years_path }, |
| 34 | + { label: @school_year.to_s, path: admin_v2_school_year_path(@school_year) }, |
| 35 | + { label: "Edit" } |
| 36 | + ] |
| 37 | + end |
| 38 | + |
| 39 | + def create |
| 40 | + school = School.find(school_year_params[:school_id]) |
| 41 | + year = Year.find(school_year_params[:year_id]) |
| 42 | + |
| 43 | + @school_year = SchoolYearCreationService.new(school: school, year: year).call |
| 44 | + |
| 45 | + if @school_year.persisted? |
| 46 | + redirect_to admin_v2_school_year_path(@school_year), notice: t(".notice") |
| 47 | + else |
| 48 | + @breadcrumbs = [ |
| 49 | + { label: "School Years", path: admin_v2_school_years_path }, |
| 50 | + { label: "New School Year" } |
| 51 | + ] |
| 52 | + render :new, status: :unprocessable_content |
| 53 | + end |
| 54 | + rescue ActiveRecord::RecordInvalid => e |
| 55 | + @school_year = e.record |
| 56 | + @breadcrumbs = [ |
| 57 | + { label: "School Years", path: admin_v2_school_years_path }, |
| 58 | + { label: "New School Year" } |
| 59 | + ] |
| 60 | + render :new, status: :unprocessable_content |
| 61 | + end |
| 62 | + |
| 63 | + def update |
| 64 | + if @school_year.update(school_year_params) |
| 65 | + redirect_to admin_v2_school_year_path(@school_year), notice: t(".notice") |
| 66 | + else |
| 67 | + @breadcrumbs = [ |
| 68 | + { label: "School Years", path: admin_v2_school_years_path }, |
| 69 | + { label: @school_year.to_s, path: admin_v2_school_year_path(@school_year) }, |
| 70 | + { label: "Edit" } |
| 71 | + ] |
| 72 | + render :edit, status: :unprocessable_content |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + def destroy |
| 77 | + @school_year.destroy |
| 78 | + redirect_to admin_v2_school_years_path, notice: t(".notice") |
| 79 | + rescue ActiveRecord::DeleteRestrictionError |
| 80 | + redirect_to admin_v2_school_year_path(@school_year), alert: t(".delete_restricted") |
| 81 | + end |
| 82 | + |
| 83 | + private |
| 84 | + |
| 85 | + def set_school_year |
| 86 | + @school_year = SchoolYear.find(params.expect(:id)) |
| 87 | + end |
| 88 | + |
| 89 | + def school_year_params |
| 90 | + params.expect(school_year: %i[school_id year_id]) |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments