Tweaked the controller to use 'protect_from_forgery' properly
[feedcatcher.git] / app / controllers / feed_controller.rb
1 class FeedController < ApplicationController
2
3 # skip_before_filter :verify_authenticity_token
4
5 def index
6 @feeds = FeedItem.select(:feed_name).distinct
7 respond_to do |format|
8 format.html
9 format.rss { render :layout => false }
10 end
11 end
12
13
14 def show
15 if FeedItem::valid_feed_name?(params[:feed_name])
16 @feed_name = params[:feed_name]
17 @feed_items = FeedItem.in_feed(@feed_name)
18 respond_to do |format|
19 if @feed_items.empty?
20 flash[:notice] = "No items in feed #{@feed_name}"
21 format.html { redirect_to index_path }
22 format.rss { render :layout => false }
23 else
24 format.html
25 format.rss { render :layout => false }
26 end
27 end
28 else
29 respond_to do |format|
30 flash[:notice] = "Invalid feed name"
31 format.html { redirect_to index_path }
32 format.rss { head :not_found}
33 end
34 end
35 end
36
37
38 def update
39 if FeedItem::valid_feed_name?(params[:feed_name])
40 item = FeedItem.in_feed(params[:feed_name]).entitled(params[:title]).take
41 if item
42 if params[:description].empty?
43 destroy_item(item)
44 else
45 update_item(item)
46 end
47 else
48 create_item
49 end
50 else
51 respond_to do |format|
52 flash[:notice] = "Invalid feed name"
53 format.html { redirect_to index_path }
54 format.rss { head :not_found }
55 end
56 end
57 end
58
59
60 # private
61
62
63 private def create_item
64 item = FeedItem.new(:feed_name => params[:feed_name],
65 :title => params[:title],
66 :description => params[:description])
67 item.save!
68 flash[:notice] = "Element #{params[:title]} created"
69 respond_to do |format|
70 format.html { redirect_to feed_path(params[:feed_name]) }
71 format.rss { head :ok }
72 end
73 rescue ActiveRecord::RecordInvalid => error
74 flash[:notice] = "Element #{params[:title]} could not be created"
75 respond_to do |format|
76 format.html { redirect_to feed_path(params[:feed_name]) }
77 format.rss { head :unprocessable_entity }
78 end
79 end
80
81
82 private def update_item(item)
83 if item.update_attribute(:description, params[:description])
84 flash[:notice] = "Element #{params[:title]} updated"
85 respond_to do |format|
86 format.html { redirect_to feed_path(params[:feed_name]) }
87 format.rss { head :ok }
88 end
89 else
90 flash[:notice] = "Element #{params[:title]} could not be updated"
91 respond_to do |format|
92 format.html { redirect_to feed_path(params[:feed_name]) }
93 format.rss { head :unprocessable_entity }
94 end
95 end
96 end
97
98
99 private def destroy_item(item)
100 if item.destroy
101 flash[:notice] = "Element #{params[:title]} deleted"
102 respond_to do |format|
103 format.html { redirect_to feed_path(params[:feed_name]) }
104 format.rss { head :ok }
105 end
106 else
107 flash[:notice] = "Element #{params[:title]} could not be deleted"
108 respond_to do |format|
109 format.html { redirect_to feed_path(params[:feed_name]) }
110 format.rss { head :unprocessable_entity }
111 end
112 end
113 end
114
115 end