From a7791635aa98f012d35f97c3c5288494bbb28e38 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 13 Mar 2024 18:03:43 +0100 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- app/controllers/things_controller.rb | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 app/controllers/things_controller.rb (limited to 'app/controllers/things_controller.rb') diff --git a/app/controllers/things_controller.rb b/app/controllers/things_controller.rb new file mode 100644 index 0000000..450b409 --- /dev/null +++ b/app/controllers/things_controller.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +class ThingsController < ApplicationController + include Taggable + + before_action :set_thing, only: %i[edit show update destroy] + + def show; end + + def new + @thing = Thing.new + end + + def edit; end + + def create + ps = thing_params + tag_ids = ps.delete(:tag_ids) + @thing = Thing.new(ps) + @thing.user_id = @current_user.id + + updated = ActiveRecord::Base.transaction do + next unless @thing.save + + update_tags!(object: @thing, ids: tag_ids) + end + + if updated + redirect_to thing_url(@thing.reload), notice: t('things.create-success') + else + render :new, status: :unprocessable_entity + end + end + + def update + ps = thing_params + tag_ids = ps.delete(:tag_ids) + + updated = ActiveRecord::Base.transaction do + next unless @thing.update(ps) + + update_tags!(object: @thing, ids: tag_ids) + end + + if updated + redirect_to edit_thing_url(@thing), notice: t('things.update-success') + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @thing.destroy! + + redirect_to root_url, notice: t('things.destroy-success') + end + + private + + def set_thing + @thing = Thing.find(params[:id]) + end + + def thing_params + params.require(:thing).permit(:target, :title, :publisher, :address, :year, :url, :access, + :authors, :location, :insideof, :pages, :rate, :status, :kind, + :bought_at, :editors, :note, :where_is_it, tag_ids: []) + end +end -- cgit v1.2.3