From be5b1df76aeeff4966722a21818032ed4c83215c Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 20 Sep 2017 17:14:55 +0200 Subject: [PATCH 1/3] Make subscriptions insert-only --- app/controllers/comments_controller.rb | 3 ++- app/controllers/subscriptions_controller.rb | 5 +++-- app/views/exercises/_comment_dialogcontent.html.slim | 2 +- db/migrate/20170920145852_add_deleted_to_subscription.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20170920145852_add_deleted_to_subscription.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 948f3e5b..90a4bcf1 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -123,7 +123,8 @@ class CommentsController < ApplicationController request_for_comment.commenters.each do |commenter| subscriptions = Subscription.where( :request_for_comment_id => request_for_comment.id, - :user_id => commenter.id, :user_type => commenter.class.name) + :user_id => commenter.id, :user_type => commenter.class.name, + :deleted => false) subscriptions.each do |subscription| if (subscription.subscription_type == 'author' and current_user == request_for_comment.user) or subscription.subscription_type == 'all' if subscription.user != current_user diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 21321caf..232e9f01 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -32,7 +32,8 @@ class SubscriptionsController < ApplicationController else authorize! rfc = @subscription.try(:request_for_comment) - if @subscription.destroy + @subscription.deleted = true + if @subscription.save respond_to do |format| format.html { redirect_to request_for_comment_url(rfc), notice: t('subscriptions.successfully_unsubscribed') } format.json { render json: {message: t('subscriptions.successfully_unsubscribed')}, status: :ok} @@ -55,7 +56,7 @@ class SubscriptionsController < ApplicationController def subscription_params current_user_id = current_user.try(:id) current_user_class_name = current_user.try(:class).try(:name) - params[:subscription].permit(:request_for_comment_id, :subscription_type).merge(user_id: current_user_id, user_type: current_user_class_name) + params[:subscription].permit(:request_for_comment_id, :subscription_type).merge(user_id: current_user_id, user_type: current_user_class_name, deleted: false) end private :subscription_params end diff --git a/app/views/exercises/_comment_dialogcontent.html.slim b/app/views/exercises/_comment_dialogcontent.html.slim index 29fb8208..f543f21d 100644 --- a/app/views/exercises/_comment_dialogcontent.html.slim +++ b/app/views/exercises/_comment_dialogcontent.html.slim @@ -2,7 +2,7 @@ h5 =t('exercises.implement.comment.others') .container label - input#subscribe type='checkbox' title=t('request_for_comments.subscribe_to_author') data-subscription=Subscription.where(user: current_user, request_for_comment_id: @request_for_comment.id).try(:first).try(:id) + input#subscribe type='checkbox' title=t('request_for_comments.subscribe_to_author') data-subscription=Subscription.where(user: current_user, request_for_comment_id: @request_for_comment.id, subscription_type: 'author', deleted: false).try(:first).try(:id) = t('request_for_comments.subscribe_to_author') #myComment diff --git a/db/migrate/20170920145852_add_deleted_to_subscription.rb b/db/migrate/20170920145852_add_deleted_to_subscription.rb new file mode 100644 index 00000000..eb1d47c4 --- /dev/null +++ b/db/migrate/20170920145852_add_deleted_to_subscription.rb @@ -0,0 +1,5 @@ +class AddDeletedToSubscription < ActiveRecord::Migration + def change + add_column :subscriptions, :deleted, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index c31baf1c..e7e0c6d6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170913054203) do +ActiveRecord::Schema.define(version: 20170920145852) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -288,6 +288,7 @@ ActiveRecord::Schema.define(version: 20170913054203) do t.string "subscription_type" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "deleted" end create_table "tags", force: :cascade do |t| From c32320503627c21805f5bb07e9e13dd7180f3682 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 20 Sep 2017 17:23:49 +0200 Subject: [PATCH 2/3] Only send one email per user when subscription gets a new comment --- app/controllers/comments_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 90a4bcf1..d292804e 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -121,14 +121,16 @@ class CommentsController < ApplicationController def send_mail_to_subscribers(comment, request_for_comment) request_for_comment.commenters.each do |commenter| + already_sent_mail = false subscriptions = Subscription.where( :request_for_comment_id => request_for_comment.id, :user_id => commenter.id, :user_type => commenter.class.name, :deleted => false) subscriptions.each do |subscription| if (subscription.subscription_type == 'author' and current_user == request_for_comment.user) or subscription.subscription_type == 'all' - if subscription.user != current_user + unless subscription.user == current_user or already_sent_mail UserMailer.got_new_comment_for_subscription(comment, subscription, current_user).deliver_now + already_sent_mail = true end end end From df5e8133d29b19df3216521279cac45d5253fed2 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 20 Sep 2017 17:27:47 +0200 Subject: [PATCH 3/3] Move subscribe box out of other comments container --- app/assets/stylesheets/request-for-comments.css.scss | 8 ++++---- app/views/exercises/_comment_dialogcontent.html.slim | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/assets/stylesheets/request-for-comments.css.scss b/app/assets/stylesheets/request-for-comments.css.scss index e0ff5fa0..8fb7bfea 100644 --- a/app/assets/stylesheets/request-for-comments.css.scss +++ b/app/assets/stylesheets/request-for-comments.css.scss @@ -127,11 +127,11 @@ } } } +} - input#subscribe { - margin-top: 5px; - margin-right: 5px; - } +input#subscribe { + margin-top: 5px; + margin-right: 5px; } #myComment { diff --git a/app/views/exercises/_comment_dialogcontent.html.slim b/app/views/exercises/_comment_dialogcontent.html.slim index f543f21d..ab6dc6b4 100644 --- a/app/views/exercises/_comment_dialogcontent.html.slim +++ b/app/views/exercises/_comment_dialogcontent.html.slim @@ -1,9 +1,10 @@ #otherComments h5 =t('exercises.implement.comment.others') .container - label - input#subscribe type='checkbox' title=t('request_for_comments.subscribe_to_author') data-subscription=Subscription.where(user: current_user, request_for_comment_id: @request_for_comment.id, subscription_type: 'author', deleted: false).try(:first).try(:id) - = t('request_for_comments.subscribe_to_author') + +label + input#subscribe type='checkbox' title=t('request_for_comments.subscribe_to_author') data-subscription=Subscription.where(user: current_user, request_for_comment_id: @request_for_comment.id, subscription_type: 'author', deleted: false).try(:first).try(:id) + = t('request_for_comments.subscribe_to_author') #myComment h5 =t('exercises.implement.comment.addyours')