|
| 1 | +class Admin::PracticalTasksController < Admin::BaseController |
| 2 | + before_action :set_test |
| 3 | + before_action :set_practical_task, only: %i[show edit update destroy activate deactivate] |
| 4 | + |
| 5 | + def show |
| 6 | + end |
| 7 | + |
| 8 | + def new |
| 9 | + @practical_task = @test.practical_tasks.build |
| 10 | + end |
| 11 | + |
| 12 | + def create |
| 13 | + @practical_task = @test.practical_tasks.build(practical_task_params) |
| 14 | + @practical_task.display_order = @test.practical_tasks.maximum(:display_order).to_i + 1 |
| 15 | + |
| 16 | + respond_to do |format| |
| 17 | + if @practical_task.save |
| 18 | + format.html { redirect_to admin_test_path(@test), notice: t('admin.practical_tasks.created') } |
| 19 | + else |
| 20 | + flash[:alert] = @practical_task.errors.full_messages.join(', ') |
| 21 | + format.html { redirect_back_or_to root_path } |
| 22 | + format.turbo_stream { |
| 23 | + render turbo_stream: turbo_stream.update_all('.dialog_flash', partial: 'common/dialog_flash') |
| 24 | + } |
| 25 | + end |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def edit |
| 30 | + end |
| 31 | + |
| 32 | + def update |
| 33 | + respond_to do |format| |
| 34 | + if @practical_task.update(practical_task_params) |
| 35 | + format.html { redirect_to admin_test_path(@test), notice: t('admin.practical_tasks.updated') } |
| 36 | + else |
| 37 | + flash[:alert] = @practical_task.errors.full_messages.join(', ') |
| 38 | + format.html { redirect_back_or_to root_path } |
| 39 | + format.turbo_stream { |
| 40 | + render turbo_stream: turbo_stream.update_all('.dialog_flash', partial: 'common/dialog_flash') |
| 41 | + } |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + def destroy |
| 47 | + @practical_task.destroy |
| 48 | + redirect_to admin_test_path(@test), notice: t('admin.practical_tasks.destroyed') |
| 49 | + end |
| 50 | + |
| 51 | + def activate |
| 52 | + @practical_task.update!(active: true) |
| 53 | + redirect_to admin_test_practical_task_path(@test, @practical_task), notice: t('admin.practical_tasks.activated') |
| 54 | + end |
| 55 | + |
| 56 | + def deactivate |
| 57 | + @practical_task.update!(active: false) |
| 58 | + redirect_to admin_test_practical_task_path(@test, @practical_task), notice: t('admin.practical_tasks.deactivated') |
| 59 | + end |
| 60 | + |
| 61 | + private |
| 62 | + |
| 63 | + def set_test |
| 64 | + @test = Test.friendly.find(params[:test_id]) |
| 65 | + end |
| 66 | + |
| 67 | + def set_practical_task |
| 68 | + @practical_task = @test.practical_tasks.find(params[:id]) |
| 69 | + end |
| 70 | + |
| 71 | + def practical_task_params |
| 72 | + params.require(:practical_task).permit( |
| 73 | + :title_et, :title_en, :body_et, :body_en, |
| 74 | + :validator, :display_order, :active |
| 75 | + ) |
| 76 | + end |
| 77 | +end |
0 commit comments