Improve rails controllers (#118)

This commit is contained in:
Riccardo Graziosi
2022-06-10 12:03:33 +02:00
committed by GitHub
parent 8e75a85873
commit 94f77517a8
19 changed files with 244 additions and 66 deletions

View File

@@ -23,10 +23,10 @@ class CommentsController < ApplicationController
comment = Comment.new(comment_params)
if comment.save
send_notifications(comment)
SendNotificationForCommentWorkflow.new(comment: comment).run
render json: comment.attributes.merge(
{ user_full_name: current_user.full_name, user_email: current_user.email}
{ user_full_name: current_user.full_name, user_email: current_user.email }
), status: :created
else
render json: {
@@ -37,17 +37,12 @@ class CommentsController < ApplicationController
def update
comment = Comment.find(params[:id])
authorize comment
comment.assign_attributes(comment_params)
if !current_user.power_user? && current_user.id != post.user_id
render json: t('backend.errors.unauthorized'), status: :unauthorized
return
end
if comment.save
render json: comment.attributes.merge(
{ user_full_name: current_user.full_name, user_email: current_user.email}
{ user_full_name: current_user.full_name, user_email: current_user.email }
)
else
render json: {
@@ -57,41 +52,14 @@ class CommentsController < ApplicationController
end
private
def comment_params
params
.require(:comment)
.permit(:body, :parent_id, :is_post_update)
.merge(
user_id: current_user.id,
post_id: params[:post_id]
)
end
def send_notifications(comment)
if comment.is_post_update # Post update
UserMailer.notify_followers_of_post_update(comment: comment).deliver_later
return
def comment_params
params
.require(:comment)
.permit(:body, :parent_id, :is_post_update)
.merge(
user_id: current_user.id,
post_id: params[:post_id]
)
end
if comment.parent_id == nil # Reply to a post
user = comment.post.user
if comment.user.id != user.id and
user.notifications_enabled? and
comment.post.follows.exists?(user_id: user.id)
UserMailer.notify_post_owner(comment: comment).deliver_later
end
else # Reply to a comment
parent_comment = comment.parent
user = parent_comment.user
if user.notifications_enabled? and
parent_comment.user.id != comment.user.id
UserMailer.notify_comment_owner(comment: comment).deliver_later
end
end
end
end