From b69bb9866f25261ae1c8fa41d63157abc6405047 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 13 Sep 2017 07:56:02 +0200 Subject: [PATCH] Add controller logic for subscription#create --- app/controllers/subscription_controller.rb | 20 ------------ app/controllers/subscriptions_controller.rb | 33 ++++++++++++++++++++ app/views/request_for_comments/show.html.erb | 28 +++++++++++++++++ 3 files changed, 61 insertions(+), 20 deletions(-) delete mode 100644 app/controllers/subscription_controller.rb create mode 100644 app/controllers/subscriptions_controller.rb diff --git a/app/controllers/subscription_controller.rb b/app/controllers/subscription_controller.rb deleted file mode 100644 index afeedb9b..00000000 --- a/app/controllers/subscription_controller.rb +++ /dev/null @@ -1,20 +0,0 @@ -class SubscriptionController < ApplicationController - - def authorize! - authorize(@submission || @submissions) - end - private :authorize! - - def set_subscription - @subscription = Subscription.find(params[:id]) - authorize! - end - private :set_subscription - - 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, :type).merge(user_id: current_user_id, user_type: current_user_class_name) - end - private :subscription_params -end diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb new file mode 100644 index 00000000..6bfdb1b1 --- /dev/null +++ b/app/controllers/subscriptions_controller.rb @@ -0,0 +1,33 @@ +class SubscriptionsController < ApplicationController + + def authorize! + authorize(@subscription || @subscriptions) + end + private :authorize! + + # POST /subscriptions.json + def create + @subscription = Subscription.new(subscription_params) + respond_to do |format| + if @subscription.save + format.json { render json: @subscription, status: :created } + else + format.json { render json: @subscription.errors, status: :unprocessable_entity } + end + end + authorize! + end + + def set_subscription + @subscription = Subscription.find(params[:id]) + authorize! + end + private :set_subscription + + 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_comments, :subscription_type).merge(user_id: current_user_id, user_type: current_user_class_name) + end + private :subscription_params +end diff --git a/app/views/request_for_comments/show.html.erb b/app/views/request_for_comments/show.html.erb index c8b8d3b9..17b27f07 100644 --- a/app/views/request_for_comments/show.html.erb +++ b/app/views/request_for_comments/show.html.erb @@ -312,6 +312,24 @@ also, all settings from the rails model needed for the editor configuration in t jqxhr.fail(ajaxError); } + function subscribeToRFC(subscriptionType, checkbox){ + var jqxhr = $.ajax({ + data: { + subscription: { + request_for_comments_id: $('h4#exercise_caption').data('rfc-id'), + subscription_type: subscriptionType + } + }, + dataType: 'json', + method: 'POST', + url: "/subscriptions.json" + }); + jqxhr.fail(function() { + checkbox.prop('checked', false); + ajaxError(arguments); + }); + } + function handleSidebarClick(e) { var target = e.domEvent.target; var editor = e.editor; @@ -377,6 +395,16 @@ also, all settings from the rails model needed for the editor configuration in t otherComments.hide(); } + var subscribeCheckbox = commentModal.find('#subscribe'); + subscribeCheckbox.off('change'); + subscribeCheckbox.on('change', function() { + if (this.checked) { + subscribeToRFC('author', $(this)); + } else { + // unsubscribe + } + }); + var addCommentButton = commentModal.find('#addCommentButton'); addCommentButton.off('click'); addCommentButton.on('click', function(){