2d6fee9786f62166fb4414cef8fa241c67c7f96c
[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.find(:all, :select => 'DISTINCT feed_name')
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 valid_feed_name?(params[:feed_name])
16 @feed_items = FeedItem.find_all_by_feed_name(params[:feed_name])
17 @feed_name = params[:feed_name]
18 respond_to do |format|
19 if @feed_items == []
20 flash[:notice] = "No items in feed #{params[:feed_name]}"
21 format.html { redirect_to index_url }
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_url }
32 format.rss { head :not_found}
33 end
34 end
35 end
36
37
38 def update
39 if valid_feed_name?(params[:new_feed_name])
40 item = FeedItem.find_by_feed_name_and_title(params[:new_feed_name], params[:title])
41 if item
42 if params[:description] == ''
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_url }
54 format.rss { head :not_found }
55 end
56 end
57 end
58
59
60 private
61
62 def valid_feed_name?(feed_name)
63 Rack::Utils::escape(feed_name) == feed_name and
64 Rack::Utils::unescape(feed_name) == feed_name and
65 feed_name != 'index' and
66 feed_name != 'show' and
67 feed_name != 'update' and
68 feed_name != 'action'
69 end
70
71
72 def create_item
73 item = FeedItem.new(:feed_name => params[:new_feed_name],
74 :title => params[:title],
75 :description => params[:description])
76 item.save!
77 flash[:notice] = "Element #{params[:title]} created"
78 respond_to do |format|
79 format.html { redirect_to feed_url(params[:new_feed_name]) }
80 format.rss { head :ok }
81 end
82 rescue ActiveRecord::RecordInvalid => error
83 flash[:notice] = "Element #{params[:title]} could not be created"
84 respond_to do |format|
85 format.html { redirect_to feed_url(params[:new_feed_name]) }
86 format.rss { head :unprocessable_entity }
87 end
88 end
89
90
91 def update_item(item)
92 if item.update_attribute(:description, params[:description])
93 flash[:notice] = "Element #{params[:title]} updated"
94 respond_to do |format|
95 format.html { redirect_to feed_url(params[:new_feed_name]) }
96 format.rss { head :ok }
97 end
98 else
99 flash[:notice] = "Element #{params[:title]} could not be updated"
100 respond_to do |format|
101 format.html { redirect_to feed_url(params[:new_feed_name]) }
102 format.rss { head :unprocessable_entity }
103 end
104 end
105 end
106
107
108 def destroy_item(item)
109 if item.destroy
110 flash[:notice] = "Element #{params[:title]} destroyed"
111 respond_to do |format|
112 format.html { redirect_to feed_url(params[:new_feed_name]) }
113 format.rss { head :ok }
114 end
115 else
116 flash[:notice] = "Element #{params[:title]} could not be destroyed"
117 respond_to do |format|
118 format.html { redirect_to feed_url(params[:new_feed_name]) }
119 format.rss { head :unprocessable_entity }
120 end
121 end
122 end
123
124 end