Skip to content

Commit 6d71bb6

Browse files
author
Matt Swanson
committed
wire up mark all as read button
1 parent 79a637c commit 6d71bb6

5 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎app/controllers/stories_controller.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ class Stringer < Sinatra::Base
1212
story.is_read = true
1313
StoryRepository.save(story)
1414
end
15+
16+
post "/mark_all_as_read" do
17+
StoryRepository.mark_all_as_read
18+
19+
redirect to("/news")
20+
end
1521
end

‎app/public/js/app.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ $(document).ready(function() {
3838
$.post("/mark_as_read", { story_id: $this.data("id") })
3939
.fail(function() { alert("something broke!"); });
4040
});
41+
42+
$("#mark-all").click(function(e) {
43+
e.preventDefault();
44+
45+
$("form#mark-all-as-read").submit();
46+
});
4147
});

‎app/repositories/story_repository.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ def self.save(story)
2121
def self.unread
2222
Story.where(is_read: false).order(:published)
2323
end
24+
25+
def self.mark_all_as_read
26+
Story.update_all(is_read: false, is_read: true)
27+
end
2428
end

‎app/views/index.erb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
</a>
1919
</div>
2020
</div>
21+
22+
<div class="hidden">
23+
<form id="mark-all-as-read" method="POST" action="/mark_all_as_read"></form>
24+
</div>
2125
</div>
2226

2327
<% unless @unread_stories.empty? %>

‎spec/controllers/stories_controller_spec.rb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,15 @@
5454
story_one.is_read.should be_true
5555
end
5656
end
57+
58+
describe "/mark_all_as_read" do
59+
it "marks all unread stories as read and reload the page" do
60+
StoryRepository.should_receive(:mark_all_as_read).once
61+
62+
post "/mark_all_as_read"
63+
64+
last_response.status.should be 302
65+
URI::parse(last_response.location).path.should eq "/news"
66+
end
67+
end
5768
end

0 commit comments

Comments
 (0)