From be0d925230a5241ca3ddb93852bfac5f5d07c1ab Mon Sep 17 00:00:00 2001 From: fmendez Date: Thu, 9 May 2013 17:44:15 -0400 Subject: [PATCH 1/3] Added endpoint to get current unread stories count. This endpoint is used to update the title to reflect the correct unread stories count after clicking on a given story. [Fixes #5] --- app/controllers/stories_controller.rb | 4 ++++ app/public/js/app.js | 10 ++++++++++ spec/controllers/stories_controller_spec.rb | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 45ba375fe..991c78c63 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -8,6 +8,10 @@ class Stringer < Sinatra::Base erb :index end + get "/unread_stories_count" do + StoryRepository.unread.count.to_s + end + post "/mark_as_read" do story = StoryRepository.fetch(params[:story_id]) story.is_read = true diff --git a/app/public/js/app.js b/app/public/js/app.js index 504d1066d..bf3879701 100644 --- a/app/public/js/app.js +++ b/app/public/js/app.js @@ -39,6 +39,16 @@ $(document).ready(function() { if (storyId > 0) { $.post("/mark_as_read", { story_id: storyId }) + .done(function() { + $.ajax({ + type: "GET", + url: "/unread_stories_count", + success: function (data) { + var title = $("head > title")[0] + title.text = title.text.replace(/\d+/,data); + } + }); + }) .fail(function() { alert("something broke!"); }); } }); diff --git a/spec/controllers/stories_controller_spec.rb b/spec/controllers/stories_controller_spec.rb index f218c1a07..636f2eb5a 100644 --- a/spec/controllers/stories_controller_spec.rb +++ b/spec/controllers/stories_controller_spec.rb @@ -66,7 +66,18 @@ story_one.is_read.should be_true end end + + describe "/unread_stories_count" do + before { StoryRepository.stub(:unread).and_return(stories) } + + it "returns the current unread stories count" do + + get "/unread_stories_count" + last_response.body.to_i.should eq stories.count + end + end + describe "/mark_all_as_read" do it "marks all unread stories as read and reload the page" do MarkAllAsRead.any_instance.should_receive(:mark_as_read).once From ebe4aee986559805da69a3cd6069956a2bb08cf5 Mon Sep 17 00:00:00 2001 From: fmendez Date: Thu, 9 May 2013 17:44:15 -0400 Subject: [PATCH 2/3] Added endpoint to get current unread stories count. This endpoint is used to update the title to reflect the correct unread stories count after clicking on a given story. [Fixes #52] --- app/controllers/stories_controller.rb | 4 ++++ app/public/js/app.js | 10 ++++++++++ spec/controllers/stories_controller_spec.rb | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 45ba375fe..991c78c63 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -8,6 +8,10 @@ class Stringer < Sinatra::Base erb :index end + get "/unread_stories_count" do + StoryRepository.unread.count.to_s + end + post "/mark_as_read" do story = StoryRepository.fetch(params[:story_id]) story.is_read = true diff --git a/app/public/js/app.js b/app/public/js/app.js index 504d1066d..bf3879701 100644 --- a/app/public/js/app.js +++ b/app/public/js/app.js @@ -39,6 +39,16 @@ $(document).ready(function() { if (storyId > 0) { $.post("/mark_as_read", { story_id: storyId }) + .done(function() { + $.ajax({ + type: "GET", + url: "/unread_stories_count", + success: function (data) { + var title = $("head > title")[0] + title.text = title.text.replace(/\d+/,data); + } + }); + }) .fail(function() { alert("something broke!"); }); } }); diff --git a/spec/controllers/stories_controller_spec.rb b/spec/controllers/stories_controller_spec.rb index f218c1a07..636f2eb5a 100644 --- a/spec/controllers/stories_controller_spec.rb +++ b/spec/controllers/stories_controller_spec.rb @@ -66,7 +66,18 @@ story_one.is_read.should be_true end end + + describe "/unread_stories_count" do + before { StoryRepository.stub(:unread).and_return(stories) } + + it "returns the current unread stories count" do + + get "/unread_stories_count" + last_response.body.to_i.should eq stories.count + end + end + describe "/mark_all_as_read" do it "marks all unread stories as read and reload the page" do MarkAllAsRead.any_instance.should_receive(:mark_as_read).once From 0e3d8c6f979146ef5e7c7db13cd8fe88c899eeca Mon Sep 17 00:00:00 2001 From: fmendez Date: Thu, 9 May 2013 20:52:00 -0400 Subject: [PATCH 3/3] Refactored the previous approach to move the logic to the client side of the application. [Fixes #52] --- app/controllers/stories_controller.rb | 4 --- app/public/js/app.js | 27 ++++++++++++--------- spec/controllers/stories_controller_spec.rb | 11 --------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 991c78c63..3d8321f3e 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -7,10 +7,6 @@ class Stringer < Sinatra::Base erb :index end - - get "/unread_stories_count" do - StoryRepository.unread.count.to_s - end post "/mark_as_read" do story = StoryRepository.fetch(params[:story_id]) diff --git a/app/public/js/app.js b/app/public/js/app.js index bf3879701..54ba0c45b 100644 --- a/app/public/js/app.js +++ b/app/public/js/app.js @@ -1,4 +1,5 @@ $(document).ready(function() { + var previous_req_id = 0; $(".story-preview").click(function(e){ e.preventDefault(); @@ -38,18 +39,20 @@ $(document).ready(function() { var storyId = $this.data("id"); if (storyId > 0) { - $.post("/mark_as_read", { story_id: storyId }) - .done(function() { - $.ajax({ - type: "GET", - url: "/unread_stories_count", - success: function (data) { - var title = $("head > title")[0] - title.text = title.text.replace(/\d+/,data); - } - }); - }) - .fail(function() { alert("something broke!"); }); + $.post("/mark_as_read", { story_id: storyId }) + .done(function() { + if(previous_req_id != storyId ){ + var title = $("head > title")[0]; + var stories_count = parseInt(title.text.match(/\d+/)); + if (stories_count > 1){ + title.text = title.text.replace(/\d+/,stories_count - 1); + }else{ + title.text = title.text.replace("(1)","") + } + previous_req_id = storyId; + } + }) + .fail(function() { alert("something broke!"); }); } }); diff --git a/spec/controllers/stories_controller_spec.rb b/spec/controllers/stories_controller_spec.rb index 636f2eb5a..f218c1a07 100644 --- a/spec/controllers/stories_controller_spec.rb +++ b/spec/controllers/stories_controller_spec.rb @@ -66,18 +66,7 @@ story_one.is_read.should be_true end end - - describe "/unread_stories_count" do - before { StoryRepository.stub(:unread).and_return(stories) } - - it "returns the current unread stories count" do - - get "/unread_stories_count" - last_response.body.to_i.should eq stories.count - end - end - describe "/mark_all_as_read" do it "marks all unread stories as read and reload the page" do MarkAllAsRead.any_instance.should_receive(:mark_as_read).once