Skip to content

Commit 0cbb6c4

Browse files
committed
Allow unsubscribe to work for logged in user
1 parent 57342d3 commit 0cbb6c4

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

app/controllers/users/newsletter_subscriptions_controller.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,16 @@ def create
5151
end
5252

5353
def unsubscribe
54-
subscription = find_subscription or raise ActiveRecord::RecordNotFound
55-
56-
subscription.destroy
54+
if params[:token]
55+
subscription = NewsletterSubscription.find_by_token_for(:unsubscribe, params[:token]) or raise ActiveRecord::RecordNotFound
56+
subscription.destroy
57+
elsif current_user
58+
# Even though we model the subscription as a has_one, we should destroy
59+
# all because has_one is not enforced as a constraint
60+
NewsletterSubscription.where(subscriber: current_user).destroy_all
61+
else
62+
raise ActiveRecord::RecordNotFound
63+
end
5764

5865
if request.post? && params["List-Unsubscribe"] == "One-Click"
5966
# must not redirect according to RFC 8058
@@ -63,14 +70,4 @@ def unsubscribe
6370
redirect_to root_path, notice: "You have been unsubscribed"
6471
end
6572
end
66-
67-
private
68-
69-
def find_subscription
70-
if params[:token]
71-
NewsletterSubscription.find_by_token_for(:unsubscribe, params[:token])
72-
elsif current_user&.subscribed_to_newsletter?
73-
current_user.newsletter_subscription
74-
end
75-
end
7673
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
resources :newsletter_subscriptions, only: [:new, :create, :show]
4141
resources :newsletter_subscriptions, only: [], param: :token do
42+
match :unsubscribe, on: :collection, via: [:get, :post, :delete]
4243
match :unsubscribe, on: :member, via: [:get, :post, :delete]
4344
end
4445

spec/requests/users/newsletter_subscriptions_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,31 @@
170170
expect(response).to have_http_status(:not_found)
171171
expect(user.newsletter_subscription).to be_present
172172
end
173+
174+
it "unsubscribes a logged in user with an existing subscription on collection route" do
175+
user = FactoryBot.create(:user, :subscribed)
176+
login_user user
177+
178+
expect(user.newsletter_subscription).to be_present
179+
180+
expect {
181+
delete unsubscribe_users_newsletter_subscriptions_path
182+
}.to change(NewsletterSubscription, :count).by(-1)
183+
184+
user.reload
185+
186+
expect(response).to redirect_to(root_path)
187+
expect(flash[:notice]).to eq("You have been unsubscribed")
188+
189+
expect(user.newsletter_subscription).to be_nil
190+
end
191+
192+
it "disallows when not logged in on collection route" do
193+
expect {
194+
post unsubscribe_users_newsletter_subscriptions_path
195+
}.not_to change(NewsletterSubscription, :count)
196+
197+
expect(response).to have_http_status(:not_found)
198+
end
173199
end
174200
end

0 commit comments

Comments
 (0)