Skip to content

Commit

Permalink
Add atom & rss feed links
Browse files Browse the repository at this point in the history
  • Loading branch information
raghureddyram authored and tjgrathwell committed Aug 18, 2015
1 parent 155c852 commit 00b158d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
1.10.4
1.10.5
15 changes: 12 additions & 3 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EventsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :past_events, :all_events, :show, :levels]
before_filter :find_event, except: [:index, :past_events, :all_events, :create, :new]
before_filter :validate_organizer!, except: [:index, :past_events, :all_events, :create, :show, :new, :levels]
before_filter :authenticate_user!, except: [:index, :feed, :past_events, :all_events, :show, :levels]
before_filter :find_event, except: [:index, :feed, :past_events, :all_events, :create, :new]
before_filter :validate_organizer!, except: [:index, :feed, :past_events, :all_events, :create, :show, :new, :levels]
before_filter :set_time_zone, only: [:create, :update]

def index
Expand All @@ -15,6 +15,15 @@ def index
end
end

def feed
@events = Event.upcoming.published_or_organized_by(current_user).includes(:event_sessions, :location, :chapter)

respond_to do |format|
format.rss {render 'events/feed.rss.builder', layout: false}
format.atom {render 'events/feed.atom.builder', layout: false}
end
end

def past_events
respond_to do |format|
format.json { render json: sort_by_starts_at(combined_past_events_for_json) }
Expand Down
16 changes: 16 additions & 0 deletions app/views/events/feed.atom.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
atom_feed do |feed|
feed.title("RailsBridge")
feed.link root_url
feed.updated @events.last.updated_at
feed.id root_url

@events.each do |event|
feed.entry(event) do |entry|
entry.title(event.title)
entry.summary(event.details, type: 'html')
entry.link event_path(event)
entry.id event_path(event)
entry.updated event.updated_at
end
end
end
18 changes: 18 additions & 0 deletions app/views/events/feed.rss.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.channel do
xml.title "RailsBridge"
xml.link root_url
xml.language "en"

for event in @events
xml.item do
xml.title event.title
xml.pubDate event.created_at.to_s(:rfc822)
xml.link event_path(event)
xml.guid event_path(event)
xml.description event.details
end
end
end
end
6 changes: 6 additions & 0 deletions app/views/events/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<% end %>
</div>

<p>
Subscribe to our calendar via:
<%= link_to "RSS", feed_events_path(format: :rss) %>
<%= link_to "Atom", feed_events_path(format: :atom) %>
</p>

<% if user_signed_in? %>
<h1>Get Involved</h1>
<% links = [
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
post :arrange, on: :collection
end

collection do
get :feed
end

resources :rsvps, except: [:show, :index, :new] do
new do
get :volunteer
Expand Down
58 changes: 58 additions & 0 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,62 @@ def make_request
result_titles.should == [@past_event, @past_external_event, @future_external_event, @future_event].map(&:title)
end
end

describe "GET feed" do
let!(:event) { create(:event, title: 'DonutBridge') }
let!(:other_event) { create(:event, title: 'C5 Event!') }
render_views

context "when format is RSS" do
before do
get :feed, format: :rss
end

it "successfully directs to xml rss feed" do
response.should be_success

event.should be_in(assigns(:events))
other_event.should be_in(assigns(:events))
end

it "has rss formatting" do
response.body.should include 'rss'
end

it "includes the website title" do
response.body.should include ('RailsBridge')
end

it "includes all events" do
response.body.should include ('DonutBridge')
response.body.should include ('C5 Event!')
end
end

context "when format is Atom" do
before do
get :feed, format: :atom
end

it "successfully directs to xml rss feed" do
response.should be_success

event.should be_in(assigns(:events))
other_event.should be_in(assigns(:events))
end

it "has rss formatting" do
response.body.should include 'feed'
end

it "includes the website title" do
response.body.should include ('RailsBridge')
end

it "includes all events" do
response.body.should include ('DonutBridge')
response.body.should include ('C5 Event!')
end
end
end
end

0 comments on commit 00b158d

Please sign in to comment.