Add controller logic for subscription#create

This commit is contained in:
Maximilian Grundke
2017-09-13 07:56:02 +02:00
parent 0557255d74
commit b69bb9866f
3 changed files with 61 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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(){