Added Flash[:notice] to HTML pages, fixed update code, done RSS rendering properly
authorNeil Smith <neil.git@njae.me.uk>
Wed, 22 Jul 2009 12:48:03 +0000 (12:48 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 22 Jul 2009 12:48:03 +0000 (12:48 +0000)
app/controllers/feed_controller.rb
app/models/feed_item.rb
app/views/feed/show.html.erb
app/views/feed/show.rss.builder
app/views/feed/update.html.erb [deleted file]
app/views/layouts/application.html.erb
heaaders.txt [new file with mode: 0644]
headers.txt [new file with mode: 0644]
public/stylesheets/feedcatcher.css [new file with mode: 0644]

index 71ab1df4f829d2530acf4163143ab6a042481099..2d6fee9786f62166fb4414cef8fa241c67c7f96c 100644 (file)
@@ -10,29 +10,115 @@ class FeedController < ApplicationController
     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 }
+    if valid_feed_name?(params[:feed_name])
+      @feed_items = FeedItem.find_all_by_feed_name(params[:feed_name])
+      @feed_name = params[:feed_name]
+      respond_to do |format|
+        if @feed_items == []
+          flash[:notice] = "No items in feed #{params[:feed_name]}"
+          format.html { redirect_to index_url }
+          format.rss  { render :layout => false }
+        else
+          format.html
+          format.rss { render :layout => false }
+        end
+      end
+    else
+      respond_to do |format|
+        flash[:notice] = "Invalid feed name"
+        format.html { redirect_to index_url }
+        format.rss  { head :not_found}
+      end
     end
   end
 
+
   def update
-    item = FeedItem.find_by_feed_name_and_title(params[:feed_name], params[:title])
-    if item
-      if params[:description] == ''
-        item.destroy
+    if valid_feed_name?(params[:new_feed_name])
+      item = FeedItem.find_by_feed_name_and_title(params[:new_feed_name], params[:title])
+      if item
+        if params[:description] == ''
+          destroy_item(item)
+        else
+          update_item(item)
+        end
       else
-        item.update_attribute(:description, params[:description])
+        create_item
+      end
+    else
+      respond_to do |format|
+        flash[:notice] = "Invalid feed name"
+        format.html { redirect_to index_url }
+        format.rss  { head :not_found }
+      end
+    end
+  end
+  
+
+  private
+
+  def valid_feed_name?(feed_name)
+    Rack::Utils::escape(feed_name) == feed_name and
+      Rack::Utils::unescape(feed_name) == feed_name and
+      feed_name != 'index' and
+      feed_name != 'show' and
+      feed_name != 'update' and
+      feed_name != 'action'
+  end
+
+
+  def create_item
+    item = FeedItem.new(:feed_name => params[:new_feed_name],
+      :title => params[:title],
+      :description => params[:description])
+    item.save!
+    flash[:notice] = "Element #{params[:title]} created"
+    respond_to do |format|
+      format.html { redirect_to feed_url(params[:new_feed_name]) }
+      format.rss  { head :ok }
+    end
+  rescue ActiveRecord::RecordInvalid => error
+    flash[:notice] = "Element #{params[:title]} could not be created"
+    respond_to do |format|
+      format.html { redirect_to feed_url(params[:new_feed_name]) }
+      format.rss  { head :unprocessable_entity }
+    end
+  end
+
+
+  def update_item(item)
+    if item.update_attribute(:description, params[:description])
+      flash[:notice] = "Element #{params[:title]} updated"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:new_feed_name]) }
+        format.rss  { head :ok }
       end
     else
-      FeedItem.create(:feed_name => params[:feed_name],
-        :title => params[:title],
-        :description => params[:description])
+      flash[:notice] = "Element #{params[:title]} could not be updated"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:new_feed_name]) }
+        format.rss  { head :unprocessable_entity }
+      end
+    end
+  end
+
+
+  def destroy_item(item)
+    if item.destroy
+      flash[:notice] = "Element #{params[:title]} destroyed"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:new_feed_name]) }
+        format.rss  { head :ok }
+      end
+    else
+      flash[:notice] = "Element #{params[:title]} could not be destroyed"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:new_feed_name]) }
+        format.rss  { head :unprocessable_entity }
+      end
     end
-    redirect_to feed_url(params[:feed_name])
   end
 
 end
index cf01358f8ee2b01e5361b4d664ceeb1ea760199c..75830a53f1f58287626fec37b24104aea36d4919 100644 (file)
@@ -5,7 +5,7 @@ class FeedItem < ActiveRecord::Base
   validates_presence_of :feed_name, :title, :description
   validate :feed_name_must_be_legal
 
-protected
+private
 
   def feed_name_must_be_legal
     if Rack::Utils::escape(feed_name) != feed_name or
index 2a2b9b1daaaad4094886073444761803d9270618..739584cda210b9f511d97ef9ef0ef7a16f195792 100644 (file)
@@ -2,7 +2,7 @@
 <p><%= link_to("List of all feeds", index_url) %></p>
 
 <% form_tag :action => 'update' do %>
-  <p>Set feed <%= text_field_tag :feed_name, h(params[:feed_name]), :size => 20 %>
+  <p>Set feed <%= text_field_tag :new_feed_name, h(params[:feed_name]), :size => 20 %>
     to include <%= text_field_tag :title, '', :size => 30 %>
     containing  <%= text_field_tag :description, '', :size => 50 %>
     <%= submit_tag 'Update' %></p>
index 522b66277949550c115269fe0a305b36f1452059..7fdfe205b8f2afe4094a5b4ae6e535c26370aff0 100644 (file)
@@ -2,8 +2,8 @@
 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)
+    xml.title @feed_name
+    xml.link feed_url(@feed_name, :rss)
 
     for item in @feed_items
       xml.item do
diff --git a/app/views/feed/update.html.erb b/app/views/feed/update.html.erb
deleted file mode 100644 (file)
index 96fff67..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<h1>Feed#update</h1>
-<p>Find me in app/views/feed/update.html.erb</p>
index ee2975d78194cae95b2509ff0fe4b1cd246f4f88..1599adb0cc371ff7dd1d8f0205811fa415c56999 100644 (file)
@@ -1,8 +1,12 @@
 <html>
   <head>
     <title>Feedcatcher</title>
+    <%= stylesheet_link_tag "feedcatcher", :media => :all %>
   </head>
   <body>
+    <% if flash[:notice] -%>
+      <div id="notice"><%= flash[:notice] %></div>
+    <% end -%>
     <%= yield :layout %>
   </body>
 </html>
diff --git a/heaaders.txt b/heaaders.txt
new file mode 100644 (file)
index 0000000..bd7c636
--- /dev/null
@@ -0,0 +1,10 @@
+HTTP/1.1 200 OK \r
+Cache-Control: private, max-age=0, must-revalidate\r
+Connection: Keep-Alive\r
+Date: Wed, 22 Jul 2009 12:44:26 GMT\r
+Content-Type: application/rss+xml; charset=utf-8\r
+Etag: "e7d0d2fad560d3d17bc9fa379dff26d7"\r
+Server: WEBrick/1.3.1 (Ruby/1.8.6/2007-09-24)\r
+X-Runtime: 33\r
+Content-Length: 710\r
+\r
diff --git a/headers.txt b/headers.txt
new file mode 100644 (file)
index 0000000..8ff770c
--- /dev/null
@@ -0,0 +1,10 @@
+HTTP/1.1 200 OK \r
+Cache-Control: private, max-age=0, must-revalidate\r
+Connection: Keep-Alive\r
+Date: Wed, 22 Jul 2009 12:46:46 GMT\r
+Content-Type: application/rss+xml; charset=utf-8\r
+Etag: "0fcd2f586381d34c341be820ea75fa9a"\r
+Server: WEBrick/1.3.1 (Ruby/1.8.6/2007-09-24)\r
+X-Runtime: 67\r
+Content-Length: 2528\r
+\r
diff --git a/public/stylesheets/feedcatcher.css b/public/stylesheets/feedcatcher.css
new file mode 100644 (file)
index 0000000..7623e22
--- /dev/null
@@ -0,0 +1,9 @@
+/* START:notice */
+#notice {
+  border: 2px solid red;
+  padding: 1em;
+  margin-bottom: 2em;
+  background-color: #f0f0f0;
+  font: bold smaller sans-serif;
+}
+/* END:notice */