Skip to content

Commit 20f3c45

Browse files
author
Fernando Perales & Gilberto Villa
committed
Add RSS support
Change controller name to FeedsController Change endpoint to "blog" Add caching to posts and events queries Update tests
1 parent ea6ec24 commit 20f3c45

File tree

8 files changed

+79
-26
lines changed

8 files changed

+79
-26
lines changed

app/controllers/atom_feeds_controller.rb

-7
This file was deleted.

app/controllers/feeds_controller.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class FeedsController < ApplicationController
2+
def show
3+
4+
posts = Rails.cache.fetch('posts', expires_in: 12.hours) do
5+
Crowdblog::Post.all_for_feed
6+
end
7+
8+
events = Rails.cache.fetch('events', expires_in: 12.hours) do
9+
Event.limit(15)
10+
end
11+
12+
@items = (posts + events).sort_by(&:updated_at).reverse
13+
14+
respond_to do | format |
15+
format.xml { render layout: false }
16+
format.rss { render layout: false }
17+
end
18+
end
19+
end
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
xml.instruct! :xml, :version => "1.0"
2+
xml.rss :version => "2.0" do
3+
xml.channel do
4+
xml.title "rails.mx"
5+
6+
@items.each do | item |
7+
if item.class == Crowdblog::Post
8+
9+
xml.item do
10+
xml.title item.title
11+
xml.description item.html_body
12+
xml.pubDate item.published_at.to_s(:rfc822)
13+
xml.link post_url(*item.url_params)
14+
end
15+
16+
else
17+
18+
xml.item do
19+
xml.title item.name
20+
xml.description item.description
21+
xml.link event_url(item.slug)
22+
end
23+
24+
end
25+
end
26+
end
27+
end

app/views/railsmx/atom_feeds/show.xml.builder renamed to app/views/railsmx/feeds/show.xml.builder

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
atom_feed do |feed|
2-
feed.title 'Rails Mx'
2+
feed.title 'rails.mx'
33
feed.updated @items.first.updated_at if @items.first
44
@items.each do |item|
55
if item.class == Crowdblog::Post

app/views/railsmx/layouts/application.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<meta name="description" content="Una comunidad de desarrolladores mexicanos e hispanohablantes.
99
Un punto de encuentro para compartir experiencias y mejores prácticas sobre el mundo de rails.">
1010
<meta name="robots" content="all" >
11-
<%= auto_discovery_link_tag(:atom, '/atom.xml') %>
11+
<%= auto_discovery_link_tag(:atom, '/blog.xml') %>
12+
<%= auto_discovery_link_tag(:rss, '/blog.rss') %>
1213
<%= favicon_link_tag "favicon-#{ENV['THEME'].downcase}.ico" %>
1314
<%= stylesheet_link_tag "railsmx" %>
1415
<%= javascript_include_tag "railsmx" %>

config/routes.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
post "/contacto" => "contact_form#create", as: :contact_form
1919

2020

21-
get "/atom" => 'atom_feeds#show'
21+
get "/blog.(:format)" => 'feeds#show'
22+
2223
get "/:id" => "home#show", as: :static
2324

2425
if Rails.env.test?

test/controllers/atom_feeds_controller_test.rb

-16
This file was deleted.
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "test_helper"
2+
3+
describe FeedsController do
4+
5+
describe "using xml as format" do
6+
it "should get show" do
7+
get :show, format: 'xml'
8+
assert_response :success
9+
end
10+
11+
it "should render xml" do
12+
get :show, format: 'xml'
13+
assert_template :show
14+
end
15+
end
16+
17+
describe "using rss as format" do
18+
it "should get show" do
19+
get :show, format: 'rss'
20+
assert_response :success
21+
end
22+
23+
it "should render rss" do
24+
get :show, format: 'rss'
25+
assert_template :show
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)