Added routes, display seems to work so long as you remove the forms from the views.
authorNeil Smith <neil.git@njae.me.uk>
Mon, 6 Jan 2014 11:27:27 +0000 (11:27 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 6 Jan 2014 11:27:27 +0000 (11:27 +0000)
13 files changed:
app/assets/javascripts/feed.js.coffee [new file with mode: 0644]
app/assets/stylesheets/feed.css.scss [new file with mode: 0644]
app/controllers/feed_controller.rb [new file with mode: 0644]
app/helpers/feed_helper.rb [new file with mode: 0644]
app/models/feed_item.rb
app/views/feed/index.html.erb [new file with mode: 0644]
app/views/feed/index.rss.builder [new file with mode: 0644]
app/views/feed/show.html.erb [new file with mode: 0644]
app/views/feed/show.rss.builder [new file with mode: 0644]
app/views/layouts/application.html.erb
config/routes.rb
spec/controllers/feed_controller_spec.rb [new file with mode: 0644]
spec/helpers/feed_helper_spec.rb [new file with mode: 0644]

diff --git a/app/assets/javascripts/feed.js.coffee b/app/assets/javascripts/feed.js.coffee
new file mode 100644 (file)
index 0000000..24f83d1
--- /dev/null
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
diff --git a/app/assets/stylesheets/feed.css.scss b/app/assets/stylesheets/feed.css.scss
new file mode 100644 (file)
index 0000000..075c754
--- /dev/null
@@ -0,0 +1,3 @@
+// Place all the styles related to the feed controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/feed_controller.rb b/app/controllers/feed_controller.rb
new file mode 100644 (file)
index 0000000..8e89663
--- /dev/null
@@ -0,0 +1,116 @@
+class FeedController < ApplicationController
+  
+  skip_before_filter :verify_authenticity_token
+
+  def index
+    # @feeds = FeedItem.find(:all, :select => 'DISTINCT feed_name')
+    @feeds = FeedItem.select(:feed_name).distinct
+    respond_to do |format|
+      format.html
+      format.rss { render :layout => false }
+    end
+  end
+
+  
+  def show
+    if FeedItem::valid_feed_name?(params[:feed_name])
+      @feed_name = params[:feed_name]
+      @feed_items = FeedItem.in_feed(@feed_name)
+      respond_to do |format|
+        if @feed_items == []
+          flash[:notice] = "No items in feed #{@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
+    if FeedItem::valid_feed_name?(params[:feed_name])
+      item = FeedItem.in_feed(params[:feed_name]).entitled(params[:title]).take
+      if item
+        if params[:description].empty?
+          destroy_item(item)
+        else
+          update_item(item)
+        end
+      else
+        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 create_item
+    item = FeedItem.new(:feed_name => params[: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[: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[: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[:feed_name]) }
+        format.rss  { head :ok }
+      end
+    else
+      flash[:notice] = "Element #{params[:title]} could not be updated"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.rss  { head :unprocessable_entity }
+      end
+    end
+  end
+
+
+  def destroy_item(item)
+    if item.destroy
+      flash[:notice] = "Element #{params[:title]} deleted"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.rss  { head :ok }
+      end
+    else
+      flash[:notice] = "Element #{params[:title]} could not be deleted"
+      respond_to do |format|
+        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.rss  { head :unprocessable_entity }
+      end
+    end
+  end
+
+end
diff --git a/app/helpers/feed_helper.rb b/app/helpers/feed_helper.rb
new file mode 100644 (file)
index 0000000..6709856
--- /dev/null
@@ -0,0 +1,2 @@
+module FeedHelper
+end
index db0613da3827753d0ae08c5a77984926a8463974..5b0e857b32da0fe488fa444f9fa53816da28e493 100644 (file)
@@ -1,4 +1,13 @@
 class FeedItem < ActiveRecord::Base
+  
+  def self.in_feed(name)
+    where('feed_name = ?', name)
+  end
+  
+  def self.entitled(title)
+    where('title = ?', title)
+  end
+  
   validates_presence_of :feed_name, :title, :description
   validates_uniqueness_of :title, :scope => :feed_name
   validate :feed_name_must_be_legal
diff --git a/app/views/feed/index.html.erb b/app/views/feed/index.html.erb
new file mode 100644 (file)
index 0000000..8ffe638
--- /dev/null
@@ -0,0 +1,15 @@
+<h1>Feeds available</h1>
+
+<% form_tag :action => 'update' do %>
+  <p>Set feed <%= text_field_tag :feed_name, '', :size => 20 %>
+    to include <%= text_field_tag :title, '', :size => 30 %>
+    containing  <%= text_field_tag :description, '', :size => 50 %>
+    <%= submit_tag 'Update' %></p>
+<% end %>
+
+
+<ul>
+<% for feed in @feeds %>
+  <li><%= link_to( h(feed.feed_name), (feed_path(:feed_name => feed.feed_name))) %></li>
+<% end %>
+</ul>
\ No newline at end of file
diff --git a/app/views/feed/index.rss.builder b/app/views/feed/index.rss.builder
new file mode 100644 (file)
index 0000000..c151b2b
--- /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.link index_url(:rss)
+    xml.description "Feeds available"
+
+    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
\ No newline at end of file
diff --git a/app/views/feed/show.html.erb b/app/views/feed/show.html.erb
new file mode 100644 (file)
index 0000000..a491835
--- /dev/null
@@ -0,0 +1,16 @@
+<h1>Contents of feed <%= h params[:feed_name] %></h1>
+<p><%= link_to("List of all feeds", index_path) %></p>
+
+<% form_tag :action => 'update' do %>
+  <p>Set feed <%= text_field_tag :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>
+<% end %>
+
+<dl>
+  <% for item in @feed_items %>
+    <dt><%= h item.title %></dt>
+    <dd><em><%= h item.description %></em></dd>
+  <% end %>
+</dl>
diff --git a/app/views/feed/show.rss.builder b/app/views/feed/show.rss.builder
new file mode 100644 (file)
index 0000000..88c0dd8
--- /dev/null
@@ -0,0 +1,18 @@
+# show.rss.builder
+xml.instruct! :xml, :version => "1.0"
+xml.rss :version => "2.0" do
+  xml.channel do
+    xml.title @feed_name
+    xml.link feed_url(@feed_name, :rss)
+    xml.description "The #{h @feed_name} feed"
+
+    for item in @feed_items
+      xml.item do
+        xml.title item.title
+        xml.description item.description
+        xml.guid item.id, :isPermaLink => 'false'
+        xml.pubDate(item.updated_at.to_s(:rfc822))
+      end
+    end
+  end
+end
index f11c29ddfd40ca7be8be80d80dce8245d6237433..58c8d8c929c6f66e964a1435265e933d048bfb1d 100644 (file)
@@ -7,8 +7,11 @@
   <%= csrf_meta_tags %>
 </head>
 <body>
+  <% if flash[:notice] -%>
+    <div id="notice"><%= h flash[:notice] %></div>
+  <% end -%>
 
-<%= yield %>
+  <%= yield %>
 
 </body>
 </html>
index 4a837c0969024b8c26ce30d275a9ad026a41608d..4b69b1931435b94c709684c8ad730b920074ac89 100644 (file)
@@ -4,6 +4,11 @@ Feedcatcher::Application.routes.draw do
 
   # You can have the root of your site routed with "root"
   # root 'welcome#index'
+  root 'feed#index'
+  
+  get 'index', to: 'feed#index', as: :index
+  get ':feed_name', to: 'feed#show', as: :feed
+  post '*ignored', to: 'feed#update', as: :update
 
   # Example of regular route:
   #   get 'products/:id' => 'catalog#view'
diff --git a/spec/controllers/feed_controller_spec.rb b/spec/controllers/feed_controller_spec.rb
new file mode 100644 (file)
index 0000000..7d40b1a
--- /dev/null
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe FeedController do
+
+end
diff --git a/spec/helpers/feed_helper_spec.rb b/spec/helpers/feed_helper_spec.rb
new file mode 100644 (file)
index 0000000..614ffda
--- /dev/null
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the FeedHelper. For example:
+#
+# describe FeedHelper do
+#   describe "string concat" do
+#     it "concats two strings with spaces" do
+#       expect(helper.concat_strings("this","that")).to eq("this that")
+#     end
+#   end
+# end
+describe FeedHelper do
+  pending "add some examples to (or delete) #{__FILE__}"
+end