diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 63630fd1..3e6fbac9 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -18,6 +18,34 @@ class SubscriptionsController < ApplicationController authorize! end + # DELETE /subscriptions/1 + # DELETE /subscriptions/1.json + def destroy + begin + @subscription = Subscription.find(params[:id]) + rescue + skip_authorization + respond_to do |format| + format.html { redirect_to request_for_comments_url, alert: t('subscriptions.subscription_not_existent') } + format.json { head :internal_server_error } + end + else + authorize! + rfc = @subscription.try(:request_for_comment) + if @subscription.destroy + respond_to do |format| + format.html { redirect_to request_for_comment_url(rfc), notice: t('subscriptions.successfully_unsubscribed') } + format.json { head :destroyed } + end + else + respond_to do |format| + format.html { redirect_to request_for_comment_url(rfc), :flash => { :danger => t('shared.message_failure') } } + format.json { head :internal_server_error } + end + end + end + end + def set_subscription @subscription = Subscription.find(params[:id]) authorize! diff --git a/app/policies/subscription_policy.rb b/app/policies/subscription_policy.rb index 8b2259f7..289bd5ac 100644 --- a/app/policies/subscription_policy.rb +++ b/app/policies/subscription_policy.rb @@ -2,4 +2,17 @@ class SubscriptionPolicy < ApplicationPolicy def create? everyone end + + def destroy? + author? || admin? + end + + def show_error? + everyone + end + + def author? + @user == @record.user + end + private :author? end diff --git a/config/locales/de.yml b/config/locales/de.yml index cb8c02cf..f0c00da1 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -630,4 +630,7 @@ de: comments: deleted: "Gelöscht" save_update: "Speichern" + subscriptions: + successfully_unsubscribed: "Ihr Abonnement für weitere Kommentare auf dieser Kommentaranfrage wurde erfolgreich beendet." + subscription_not_existent: "Das Abonnement, von dem Sie sich abmelden wollen, existiert nicht." diff --git a/config/locales/en.yml b/config/locales/en.yml index b89d88d2..1db04d05 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -651,3 +651,6 @@ en: comments: deleted: "Deleted" save_update: "Save" + subscriptions: + successfully_unsubscribed: "You successfully unsubscribed from this Request for Comment" + subscription_not_existent: "The subscription you want to unsubscribe from does not exist." diff --git a/config/routes.rb b/config/routes.rb index 3d4f6d38..eb243464 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,7 +21,11 @@ Rails.application.routes.draw do delete '/comment_by_id', to: 'comments#destroy_by_id' put '/comments', to: 'comments#update' - resources :subscriptions + resources :subscriptions do + member do + get :unsubscribe, to: 'subscriptions#destroy' + end + end root to: 'application#welcome'