diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
| commit | a7791635aa98f012d35f97c3c5288494bbb28e38 (patch) | |
| tree | bad987e6b4d10ad2f7cd02e40658d464907595ee /app/controllers/things_controller.rb | |
| download | operum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip | |
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app/controllers/things_controller.rb')
| -rw-r--r-- | app/controllers/things_controller.rb | 69 |
1 files changed, 69 insertions, 0 deletions
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 |
