From be5b1df76aeeff4966722a21818032ed4c83215c Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 20 Sep 2017 17:14:55 +0200 Subject: [PATCH] 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|