Skip to content

Commit 6779c9f

Browse files
authored
Allow marking every entry in a subscription as read (#596)
1 parent a204e9c commit 6779c9f

File tree

7 files changed

+35
-2
lines changed

7 files changed

+35
-2
lines changed

app/controllers/subscriptions_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class SubscriptionsController < ApplicationController
4-
before_action :set_subscription, only: %i[show edit update destroy]
4+
before_action :set_subscription, only: %i[show edit update mark_all_as_read destroy]
55
def index
66
authorize Subscription
77
@subscriptions = policy_scope(Subscription)
@@ -42,6 +42,11 @@ def update
4242
end
4343
end
4444

45+
def mark_all_as_read
46+
@subscription.entries.unread.each { |it| it.update(read_at: DateTime.current) }
47+
redirect_to @subscription
48+
end
49+
4550
def destroy
4651
# This method should not fail
4752
@subscription.destroy!

app/models/subscription.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def refreshable?
4545
subscribable.respond_to? :refresh!
4646
end
4747

48+
def any_unread?
49+
entries.unread.exists?
50+
end
51+
4852
def last_fetched_info
4953
return nil unless refreshable? && subscribable.last_fetched_at.present?
5054

app/policies/subscription_policy.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def update?
2323
record.user_id == user&.id
2424
end
2525

26+
def mark_all_as_read?
27+
record.user_id == user&.id
28+
end
29+
2630
def destroy?
2731
record.user_id == user&.id
2832
end

app/views/subscriptions/show.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</p>
77
<%- end %>
88

9+
<%- if @subscription.any_unread? %>
10+
<%= button_to t('.mark_all_as_read'), mark_all_as_read_subscription_path(@subscription), method: :post %>
11+
<%- end %>
12+
913
<table class="table">
1014
<thead class="table__head">
1115
<tr class="table__row table__row--head">

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ en:
2222
mark_as_unread: Mark as unread
2323
delete: Delete
2424
delete_confirm: "Are you sure? This can not be undone. Note that rss feed entries might show up again when the feed refreshes."
25+
mark_all_as_read: Mark all as read
2526
next: "Next entry"
2627
previous: "Previous entry"
2728
nav_pagination_label: "Pagination navigation"

config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@
9191
root 'entries#index'
9292

9393
# Resource based routes
94-
resources :subscriptions
94+
resources :subscriptions do
95+
member do
96+
post :mark_all_as_read
97+
end
98+
end
9599
resources :entries, only: %i[index show update destroy]
96100

97101
# Special routes for sign in/out and profile

test/controllers/subscriptions_controller_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ class SubscriptionsControllerTest < ActionDispatch::IntegrationTest
9797
assert_response :unprocessable_entity
9898
end
9999

100+
# Mark all as read
101+
test 'should mark all as read' do
102+
entries = create_list :entry, 5, :with_all_details, subscription: @subscription
103+
104+
post mark_all_as_read_subscription_url(@subscription)
105+
106+
assert_redirected_to subscription_url(@subscription)
107+
108+
assert_equal((Array.new(5) { true }), (entries.map { |it| it.reload.read? }))
109+
end
110+
100111
# Destroy
101112
test 'should destroy subscription' do
102113
assert_difference 'Subscription.count', -1 do

0 commit comments

Comments
 (0)