Updates and RSS generation done
authorNeil Smith <neil.git@njae.me.uk>
Fri, 17 Jul 2009 15:42:41 +0000 (15:42 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Fri, 17 Jul 2009 15:42:41 +0000 (15:42 +0000)
app/controllers/feed_controller.rb
app/models/feed_item.rb
app/views/feed/index.rss.builder [new file with mode: 0644]
app/views/feed/show.rss.builder [new file with mode: 0644]

index 7fa4e283185e163323ea5d4dbea1944cbd84ae28..2eddc8ca05f06eb2ea34ec6d76ead08e3c448703 100644 (file)
@@ -1,13 +1,33 @@
 class FeedController < ApplicationController
   def index
     @feeds = FeedItem.find(:all, :select => 'DISTINCT feed_name')
+    respond_to do |format|
+      format.html
+      format.rss { render :layout => false }
+    end
   end
 
   def show
     @feed_items = FeedItem.find_all_by_feed_name(params[:feed_name])
+    redirect_to index_url if @feed_items == []
+    respond_to do |format|
+      format.html
+      format.rss { render :layout => false }
+    end
   end
 
   def update
+    item = FeedItem.find_by_feed_name_and_title(params[:feed_item][:feed_name], params[:feed_item][:title])
+    if item
+      if params[:feed_item][:description] == ''
+        item.destroy
+      else
+        item.update_attribute(:description, params[:feed_item][:description])
+      end
+    else
+      FeedItem.create(params[:feed_item])
+    end
+    redirect_to feed_url(params[:feed_item][:feed_name])
   end
 
 end
index ce5b8c6c05e674dd46de604e79797cfdad41d305..cf01358f8ee2b01e5361b4d664ceeb1ea760199c 100644 (file)
@@ -1,6 +1,6 @@
 class FeedItem < ActiveRecord::Base
 
-  require 'cgi' # needed for url decoding
+  require 'cgi' # needed for url decoding
 
   validates_presence_of :feed_name, :title, :description
   validate :feed_name_must_be_legal
@@ -8,8 +8,8 @@ class FeedItem < ActiveRecord::Base
 protected
 
   def feed_name_must_be_legal
-    if url_encode(feed_name) != feed_name or
-        CGI::unescape(feed_name) != feed_name or
+    if Rack::Utils::escape(feed_name) != feed_name or
+        Rack::Utils::unescape(feed_name) != feed_name or
         feed_name == 'index' or
         feed_name == 'show' or
         feed_name == 'update' or
diff --git a/app/views/feed/index.rss.builder b/app/views/feed/index.rss.builder
new file mode 100644 (file)
index 0000000..93ce050
--- /dev/null
@@ -0,0 +1,17 @@
+# index.rss.builder
+xml.instruct! :xml, :version => "1.0"
+xml.rss :version => "2.0" do
+  xml.channel do
+    xml.title "Feedcatcher"
+    xml.description "Feeds available"
+    xml.link index_url(:rss)
+
+    for feed in @feeds
+      xml.item do
+        xml.title feed.feed_name
+        xml.link feed_url(feed.feed_name, :rss)
+        xml.guid feed_url(feed.feed_name, :rss)
+      end
+    end
+  end
+end
diff --git a/app/views/feed/show.rss.builder b/app/views/feed/show.rss.builder
new file mode 100644 (file)
index 0000000..522b662
--- /dev/null
@@ -0,0 +1,16 @@
+# index.rss.builder
+xml.instruct! :xml, :version => "1.0"
+xml.rss :version => "2.0" do
+  xml.channel do
+    xml.title @feed_items[0].feed_name
+    xml.link feed_url(@feed_items[0].feed_name, :rss)
+
+    for item in @feed_items
+      xml.item do
+        xml.title item.title
+        xml.description item.description
+        xml.guid item.id, :isPermaLink => 'false'
+      end
+    end
+  end
+end