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