diff --git a/Gemfile b/Gemfile index 52342ea7..3c73ab90 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gem 'rails', '3.0.7' gem 'gravatar_image_tag', '1.0.0.pre2' gem 'will_paginate', '3.0.pre2' gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3' +gem 'possessive', '1.0.0' group :development do gem 'rspec-rails', '2.5.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5280848b..16748ca7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -49,6 +49,7 @@ GEM mime-types (1.16) nokogiri (1.4.4) polyglot (0.3.1) + possessive (1.0.0) rack (1.2.2) rack-mount (0.6.14) rack (>= 1.0.0) @@ -101,6 +102,7 @@ DEPENDENCIES factory_girl_rails (= 1.0) faker (= 0.3.1) gravatar_image_tag (= 1.0.0.pre2) + possessive (= 1.0.0) rails (= 3.0.7) rspec-rails (= 2.5.0) spork (= 0.9.0.rc5) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7914d1a0..3576eeb1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,8 +10,15 @@ def index def show @user = User.find(params[:id]) - @microposts = @user.microposts.paginate(:page => params[:page]) @title = @user.name + respond_to do |format| + format.html do + @microposts = @user.microposts.paginate(:page => params[:page]) + end + format.rss do + @microposts = @user.microposts + end + end end def following diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 711a45e0..533335c2 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -5,6 +5,8 @@ <%= gravatar_for @user %> <%= @user.name %> + <%= link_to image_tag('feed-icon-14x14.png') + " RSS feed", + user_path(@user, :rss) %> <%= render 'follow_form' if signed_in? %> <% if @user.microposts.any? %> diff --git a/app/views/users/show.rss.builder b/app/views/users/show.rss.builder new file mode 100644 index 00000000..ec9c92cd --- /dev/null +++ b/app/views/users/show.rss.builder @@ -0,0 +1,15 @@ +xml.instruct! :xml, :version => "1.0" +xml.rss :version => "2.0" do + xml.channel do + xml.title "#{@user.name.possessive} Microposts" + xml.description "User Microposts" + xml.link user_url(@user, :rss) + @microposts.each_with_index do |micropost, n| + xml.item do + xml.title "micropost #{n+1}" + xml.description micropost.content + xml.pubDate micropost.created_at.to_s(:rfc822) + end + end + end +end \ No newline at end of file diff --git a/public/images/feed-icon-14x14.png b/public/images/feed-icon-14x14.png new file mode 100755 index 00000000..b3c949d2 Binary files /dev/null and b/public/images/feed-icon-14x14.png differ diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 907420ee..58ae8d58 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -127,6 +127,11 @@ :content => @user.microposts.count.to_s) end + it "should have a link to the RSS feed" do + get :show, :id => @user + response.should have_selector('a', :href => user_path(@user, :rss)) + end + describe "when signed in as another user" do it "should be successful" do test_sign_in(Factory(:user, :email => Factory.next(:email))) @@ -136,6 +141,70 @@ end end + describe "GET 'show.rss'" do + before(:each) do + @user = Factory(:user) + @mp1 = Factory(:micropost, :user => @user, :content => "Foo bar") + @mp2 = Factory(:micropost, :user => @user, :content => "Baz quux") + end + + it "should be successful" do + do_get + response.should be_success + end + + it "should not paginate microposts" do + do_get + microposts = assigns(:microposts) + microposts.class.should == Array + end + + it "should have the right title" do + do_get + content = "#{@user.name.possessive} Microposts" + response.should have_selector('title', :content => content) + end + + it "should have the right URL" do + do_get + response.body.should =~ /#{user_url(@user, :rss)}/ + end + + it "should have the right description" do + do_get + response.should have_selector('description', + :content => "User Microposts") + end + + it "should have the micropost title" do + do_get + response.should have_selector('title', :content => 'micropost 1') + response.should have_selector('title', :content => 'micropost 2') + end + + it "should have descriptions for the microposts" do + do_get + response.should have_selector('description', :content => @mp1.content) + response.should have_selector('description', :content => @mp2.content) + end + + # Apparently RSpec does not handle uppercase correctly in selectors (i.e., + # 'pubDate'), so this test fails unless I downcase 'pubDate' to lowercase + # 'pubdate'. + + it "should have micropost publication date" do + do_get + response.should have_selector('pubDate'.downcase, + :content => @mp1.created_at.to_s(:rfc822)) + response.should have_selector('pubDate'.downcase, + :content => @mp2.created_at.to_s(:rfc822)) + end + + def do_get + get :show, :id => @user, :format => :rss + end + end + describe "GET 'new'" do it "should be successful" do