|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module BetterTogether |
| 4 | + class ChecklistItemsController < FriendlyResourceController # rubocop:todo Style/Documentation |
| 5 | + before_action :set_checklist |
| 6 | + before_action :checklist_item, only: %i[show edit update destroy] |
| 7 | + |
| 8 | + helper_method :new_checklist_item |
| 9 | + |
| 10 | + def create # rubocop:todo Metrics/AbcSize |
| 11 | + @checklist_item = new_checklist_item |
| 12 | + @checklist_item.assign_attributes(resource_params) |
| 13 | + authorize @checklist_item |
| 14 | + |
| 15 | + if @checklist_item.save |
| 16 | + redirect_to request.referer || checklist_path(@checklist), notice: t('flash.generic.created') |
| 17 | + else |
| 18 | + redirect_to request.referer || checklist_path(@checklist), |
| 19 | + alert: @checklist_item.errors.full_messages.to_sentence |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + def update |
| 24 | + authorize @checklist_item |
| 25 | + |
| 26 | + if @checklist_item.update(resource_params) |
| 27 | + redirect_to request.referer || checklist_path(@checklist), notice: t('flash.generic.updated') |
| 28 | + else |
| 29 | + redirect_to request.referer || checklist_path(@checklist), |
| 30 | + alert: @checklist_item.errors.full_messages.to_sentence |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def destroy |
| 35 | + authorize @checklist_item |
| 36 | + |
| 37 | + @checklist_item.destroy |
| 38 | + redirect_to request.referer || checklist_path(@checklist), notice: t('flash.generic.deleted') |
| 39 | + end |
| 40 | + |
| 41 | + private |
| 42 | + |
| 43 | + def set_checklist |
| 44 | + @checklist = BetterTogether::Checklist.find(params[:checklist_id] || params[:id]) |
| 45 | + end |
| 46 | + |
| 47 | + def checklist_item |
| 48 | + @checklist_item = set_resource_instance |
| 49 | + end |
| 50 | + |
| 51 | + def new_checklist_item |
| 52 | + @checklist.checklist_items.new |
| 53 | + end |
| 54 | + |
| 55 | + def resource_class |
| 56 | + ::BetterTogether::ChecklistItem |
| 57 | + end |
| 58 | + |
| 59 | + def resource_collection |
| 60 | + resource_class.where(checklist: @checklist) |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments