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