Skip to content

Commit 86251a2

Browse files
Atom feed cleanup
* Wrap tweets in presenter, clean up view * Add sanity check view specs * Add DatabaseCleaner [#1]
1 parent 81d4508 commit 86251a2

File tree

6 files changed

+124
-6
lines changed

6 files changed

+124
-6
lines changed

app/models/tweet.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class Tweet < ActiveRecord::Base
44
mount_uploader :image, ImageUploader
55

66
def self.published
7-
where(published: true)
7+
where(published: true).where("published_at is not null")
88
end
99
end
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Tweet::FeedPresenter
2+
attr_accessor :tweet
3+
4+
def initialize(tweet)
5+
self.tweet = tweet
6+
end
7+
8+
def self.model_name
9+
Tweet.model_name
10+
end
11+
12+
def author
13+
username
14+
end
15+
16+
def content
17+
<<-HTML.gsub /^ {6}/, ''
18+
#{tweet_text}
19+
<br />
20+
<img src="#{media_url}" />
21+
HTML
22+
end
23+
24+
def title
25+
"#{username}: #{tweet_text}".truncate 50
26+
end
27+
28+
def url
29+
media_display_url
30+
end
31+
32+
def username
33+
"@#{tweet.username}"
34+
end
35+
36+
private
37+
38+
def method_missing(*args)
39+
tweet.send *args
40+
end
41+
end

app/views/tweets/index.atom.builder

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
@tweets.map! { |tweet| Tweet::FeedPresenter.new tweet }
2+
13
atom_feed root_url: tweets_url do |feed|
24
feed.title "#RubyFriends"
35
feed.updated @tweets.first.updated_at
46

57
@tweets.each do |tweet|
6-
feed.entry tweet, url: tweet.media_display_url, id: tweet.tweet_id do |entry|
7-
entry.url tweet.media_display_url
8-
entry.title "@#{tweet.username}: #{tweet.tweet_text.truncate 50}"
9-
entry.content tweet.tweet_text, type: :html
8+
feed.entry tweet do |entry|
9+
entry.url tweet.url
10+
entry.title tweet.title
11+
entry.content tweet.content, type: :html
1012
entry.updated tweet.updated_at.iso8601
1113

1214
entry.author do |author|
13-
author.name "@#{tweet.username}"
15+
author.name tweet.author
1416
end
1517
end
1618
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
describe Tweet::FeedPresenter do
4+
subject { feed_presenter }
5+
6+
let(:feed_presenter) { described_class.new tweet }
7+
let(:tweet) {
8+
Tweet.new tweet_id: "263515718753079296",
9+
tweet_text: "at @SteelCityRuby with @coreyhaines - one of my favorite #rubyfriends http://t.co/cyL9StoS",
10+
username: "joshsusser",
11+
media_url: "http://p.twimg.com/A6gyJmlCUAA9Il2.jpg",
12+
image: "A6gyJmlCUAA9Il2.jpg",
13+
media_display_url: "pic.twitter.com/cyL9StoS"
14+
}
15+
16+
its(:author) { should == "@joshsusser" }
17+
its(:url) { should == tweet.media_display_url }
18+
its(:username) { should == "@joshsusser" }
19+
20+
describe "#content" do
21+
subject(:content) { feed_presenter.content }
22+
23+
it "contains the tweet's text" do
24+
should match /#{tweet.tweet_text}/
25+
end
26+
27+
it "contains an image tag with the image" do
28+
should match /<img /
29+
should match /src="#{tweet.media_url}"/
30+
end
31+
end
32+
33+
describe "#title" do
34+
subject(:title) { feed_presenter.title }
35+
36+
it "begins with the username" do
37+
title.starts_with?("@joshsusser: at @SteelCityRuby with").should be_true
38+
end
39+
40+
it "truncates to 50 characters" do
41+
should have(50).characters
42+
end
43+
end
44+
end

spec/requests/tweets_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe "tweets" do
4+
before :each do
5+
Tweet.create! tweet_text: "Hi!", published: true, published_at: Time.now
6+
end
7+
8+
it "responds to html" do
9+
visit tweets_path
10+
11+
page.should have_content("Hi!")
12+
end
13+
14+
it "responds to atom" do
15+
visit tweets_path
16+
17+
page.should have_content("Hi!")
18+
end
19+
end

spec/spec_helper.rb

+12
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@
1616
config.infer_base_class_for_anonymous_controllers = true
1717
config.order = :random
1818
config.treat_symbols_as_metadata_keys_with_true_values = true
19+
20+
config.before :suite do
21+
DatabaseCleaner.strategy = :truncation
22+
end
23+
24+
config.before :each do
25+
DatabaseCleaner.start
26+
end
27+
28+
config.after :each do
29+
DatabaseCleaner.clean
30+
end
1931
end

0 commit comments

Comments
 (0)