Skip to content

Commit 0a0141d

Browse files
committed
Merge pull request #6 from ingilniero/working
Atom feed support
2 parents c7c4f95 + 20f3c45 commit 0a0141d

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

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
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
atom_feed do |feed|
2+
feed.title 'rails.mx'
3+
feed.updated @items.first.updated_at if @items.first
4+
@items.each do |item|
5+
if item.class == Crowdblog::Post
6+
feed.entry(item, url: post_url(*item.url_params)) do |entry|
7+
entry.title item.title
8+
entry.content item.html_body, type: 'html'
9+
entry.author do |author|
10+
author.name item.author.username
11+
author.email item.author_email
12+
end
13+
end
14+
else
15+
feed.entry(item, url: event_url(item.slug)) do |entry|
16+
entry.title item.name
17+
entry.content item.description, type: 'html'
18+
entry.author do |author|
19+
author.name item.organizer
20+
author.email item.contact
21+
end
22+
end
23+
end
24+
end
25+
end

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

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +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, '/blog.xml') %>
12+
<%= auto_discovery_link_tag(:rss, '/blog.rss') %>
1113
<%= favicon_link_tag "favicon-#{ENV['THEME'].downcase}.ico" %>
1214
<%= stylesheet_link_tag "railsmx" %>
1315
<%= javascript_include_tag "railsmx" %>

config/routes.rb

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
get "/contacto" => "contact_form#new", as: :contact_form_new
1313
post "/contacto" => "contact_form#create", as: :contact_form
1414

15+
16+
get "/blog.(:format)" => 'feeds#show'
17+
1518
get "/:id" => "home#show", as: :static
1619

1720
if Rails.env.test?
+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)