aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/comments_controller.rb
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2024-03-13 18:03:43 +0100
committerMiquel Sabaté Solà <msabate@suse.com>2024-03-13 18:03:43 +0100
commita7791635aa98f012d35f97c3c5288494bbb28e38 (patch)
treebad987e6b4d10ad2f7cd02e40658d464907595ee /app/controllers/comments_controller.rb
downloadoperum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz
operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app/controllers/comments_controller.rb')
-rw-r--r--app/controllers/comments_controller.rb68
1 files changed, 68 insertions, 0 deletions
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
new file mode 100644
index 0000000..d362460
--- /dev/null
+++ b/app/controllers/comments_controller.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+class CommentsController < ApplicationController
+ include Taggable
+
+ before_action :set_thing
+ before_action :set_comment, only: %i[update destroy]
+
+ def create
+ ps = comment_params
+ from = ps.delete(:from)
+ tag_ids = ps.delete(:tag_ids)
+
+ ok = ActiveRecord::Base.transaction do
+ comment = @thing.comments.create(ps)
+ next unless comment
+
+ update_tags!(object: comment, ids: tag_ids)
+ end
+
+ flash[:alert] = t('comments.bad-create') unless ok
+ handle_from_param!(from:)
+ end
+
+ def update
+ ps = comment_params
+ from = ps.delete(:from)
+ tag_ids = ps.delete(:tag_ids)
+
+ ok = ActiveRecord::Base.transaction do
+ next unless @comment.update(ps)
+
+ update_tags!(object: @comment, ids: tag_ids)
+ end
+
+ flash[:alert] = t('comments.bad-update') unless ok
+ handle_from_param!(from:)
+ end
+
+ def destroy
+ comment = Comment.find(params[:id])
+ comment.destroy
+
+ handle_from_param!(from: params[:from])
+ end
+
+ protected
+
+ def comment_params
+ params.required(:comment).permit(:content, :from, tag_ids: [])
+ end
+
+ def set_thing
+ @thing = Thing.find(params[:thing_id])
+ end
+
+ def set_comment
+ @comment = Comment.find(params[:id])
+ end
+
+ def handle_from_param!(from:)
+ if from == 'things/edit'
+ redirect_to edit_thing_path(@thing)
+ else
+ redirect_to @thing
+ end
+ end
+end