Skip to content

Commit a7d10b7

Browse files
author
matt swanson
committed
Merge pull request stringer-rss#213 from ajalexander/view_feeds
Allow viewing stories by feed
2 parents 1389645 + 0f56ed1 commit a7d10b7

10 files changed

Lines changed: 118 additions & 3 deletions

File tree

‎app/controllers/stories_controller.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ class Stringer < Sinatra::Base
88
erb :index
99
end
1010

11+
get "/feed/:feed_id" do
12+
@feed = FeedRepository.fetch(params[:feed_id])
13+
14+
@stories = StoryRepository.feed(params[:feed_id])
15+
@unread_stories = @stories.find_all {|story| !story.is_read }
16+
17+
erb :feed
18+
end
19+
1120
get "/archive" do
1221
@read_stories = StoryRepository.read(params[:page])
1322

‎app/models/feed.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def status_bubble
1818
status
1919
end
2020

21+
def unread_stories
22+
stories.where('is_read = ?', false)
23+
end
24+
25+
def has_unread_stories
26+
unread_stories.any?
27+
end
28+
2129
def as_fever_json
2230
{
2331
id: self.id,

‎app/public/css/styles.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ li.feed .feed-title a {
317317
text-decoration: none;
318318
}
319319

320+
li.feed .feed-unread {
321+
font-weight: bold;
322+
}
323+
320324
li.feed .feed-title a:hover {
321325
color: #7F8281;
322326
}
@@ -576,3 +580,9 @@ li.blank {
576580
text-decoration: none;
577581
margin: 0 7px;
578582
}
583+
584+
#feed-title {
585+
margin-bottom: 10px;
586+
font-size: 28px;
587+
text-align: center;
588+
}

‎app/public/js/app.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ var StoryView = Backbone.View.extend({
100100
},
101101

102102
render: function() {
103-
this.$el.html(this.template(this.model.toJSON()));
103+
var jsonModel = this.model.toJSON();
104+
this.$el.html(this.template(jsonModel));
105+
if (jsonModel.is_read) {
106+
this.$el.addClass('read');
107+
}
104108
return this;
105109
},
106110

‎app/repositories/feed_repository.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ def self.set_status(status, feed)
2222
def self.list
2323
Feed.order('lower(name)')
2424
end
25+
26+
def self.fetch(id)
27+
Feed.find(id)
28+
end
2529
end
2630

‎app/repositories/story_repository.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def self.unread_since_id(since_id)
4343
unread.where('id > ?', since_id)
4444
end
4545

46+
def self.feed(feed_id)
47+
Story.where('feed_id = ?', feed_id).order("published desc").includes(:feed)
48+
end
49+
4650
def self.read(page = 1)
4751
Story.where(is_read: true).includes(:feed)
4852
.order("published desc").page(page).per_page(20)

‎app/views/feed.erb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<% content_for :title do %>
2+
<% unless @unread_stories.empty? %>
3+
<%= "(#{@unread_stories.count})" %>
4+
<% end %>
5+
<% end %>
6+
7+
<div id="action-bar">
8+
<%= render_partial :action_bar, { stories: @unread_stories } %>
9+
</div>
10+
11+
<div id="feed-title">
12+
<%= @feed.name %>
13+
</div>
14+
15+
<%= render_js :stories, { stories: @stories } %>
16+
17+
<div id="stories">
18+
<ul id="story-list">
19+
</ul>
20+
</div>

‎app/views/partials/_feed.erb‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<li class="feed" id="feed-<%= feed.id%>" data-id="<%= feed.id %>">
22
<div class="row-fluid feed-line">
33
<div class="span7 feed-title-container">
4-
<p class="feed-title">
4+
<p class="feed-title <%= feed.has_unread_stories ? "feed-unread" : "" %>">
55
<i class="icon-circle status <%= feed.status_bubble %>" data-toggle="tooltip" title="<%= t("partials.feed.status_bubble.#{feed.status_bubble}") if feed.status_bubble %>" data-placement="left"></i>
6-
<a href="<%= feed.url %>"><%= feed.name %></a>
6+
<a href="/feed/<%= feed.id %>">
7+
<% if feed.has_unread_stories %>
8+
(<%= feed.unread_stories.count %>)
9+
<% end %>
10+
<%= feed.name %>
11+
</a>
712
</p>
813
</div>
914
<div class="span4 feed-last-updated">
@@ -17,6 +22,9 @@
1722
</span>
1823
</div>
1924
<div class="span1">
25+
<span class="read-feed">
26+
<a class="icon-external-link" href="<%= feed.url %>"></a>
27+
<span/>
2028
<span class="remove-feed">
2129
<a class="icon-remove"></a>
2230
<span/>

‎spec/controllers/stories_controller_spec.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,35 @@
157157
URI::parse(last_response.location).path.should eq "/news"
158158
end
159159
end
160+
161+
describe "GET /feed/:feed_id" do
162+
it "looks for a particular feed" do
163+
FeedRepository.should_receive(:fetch).with(story_one.feed.id.to_s).and_return(story_one.feed)
164+
StoryRepository.should_receive(:feed).with(story_one.feed.id.to_s).and_return([story_one])
165+
166+
get "/feed/#{story_one.feed.id}"
167+
end
168+
169+
it "displays a list of stories" do
170+
FeedRepository.stub(:fetch).and_return(story_one.feed)
171+
StoryRepository.stub(:feed).and_return(stories)
172+
173+
get "/feed/#{story_one.feed.id}"
174+
175+
last_response.body.should have_tag("#stories")
176+
end
177+
178+
it "differentiates between read and unread" do
179+
FeedRepository.stub(:fetch).and_return(story_one.feed)
180+
StoryRepository.stub(:feed).and_return(stories)
181+
182+
story_one.is_read = false
183+
story_two.is_read = true
184+
185+
get "/feed/#{story_one.feed.id}"
186+
187+
last_response.body.should have_tag('li', :class => 'story')
188+
last_response.body.should have_tag('li', :class => 'unread')
189+
end
190+
end
160191
end

‎spec/repositories/feed_repository_spec.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,21 @@
3333
feed.last_fetched.should eq timestamp
3434
end
3535
end
36+
37+
describe "fetch" do
38+
let(:feed) { Feed.new(id: 1) }
39+
40+
it "finds by id" do
41+
Feed.should_receive(:find).with(feed.id).and_return(feed)
42+
FeedRepository.fetch(feed.id)
43+
end
44+
45+
it "returns found feed" do
46+
Feed.stub(:find).with(feed.id).and_return(feed)
47+
48+
result = FeedRepository.fetch(feed.id)
49+
50+
result.should eq feed
51+
end
52+
end
3653
end

0 commit comments

Comments
 (0)