From: Neil Smith Date: Mon, 6 Jan 2014 11:27:27 +0000 (+0000) Subject: Added routes, display seems to work so long as you remove the forms from the views. X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=0c831f9107670526c653c82f182b0b6983f49c31;p=feedcatcher.git Added routes, display seems to work so long as you remove the forms from the views. --- diff --git a/app/assets/javascripts/feed.js.coffee b/app/assets/javascripts/feed.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/feed.js.coffee @@ -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 index 0000000..075c754 --- /dev/null +++ b/app/assets/stylesheets/feed.css.scss @@ -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 index 0000000..8e89663 --- /dev/null +++ b/app/controllers/feed_controller.rb @@ -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 index 0000000..6709856 --- /dev/null +++ b/app/helpers/feed_helper.rb @@ -0,0 +1,2 @@ +module FeedHelper +end diff --git a/app/models/feed_item.rb b/app/models/feed_item.rb index db0613d..5b0e857 100644 --- a/app/models/feed_item.rb +++ b/app/models/feed_item.rb @@ -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 index 0000000..8ffe638 --- /dev/null +++ b/app/views/feed/index.html.erb @@ -0,0 +1,15 @@ +

Feeds available

+ +<% form_tag :action => 'update' do %> +

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' %>

+<% end %> + + + \ 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 index 0000000..c151b2b --- /dev/null +++ b/app/views/feed/index.rss.builder @@ -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 index 0000000..a491835 --- /dev/null +++ b/app/views/feed/show.html.erb @@ -0,0 +1,16 @@ +

Contents of feed <%= h params[:feed_name] %>

+

<%= link_to("List of all feeds", index_path) %>

+ +<% form_tag :action => 'update' do %> +

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' %>

+<% end %> + +
+ <% for item in @feed_items %> +
<%= h item.title %>
+
<%= h item.description %>
+ <% end %> +
diff --git a/app/views/feed/show.rss.builder b/app/views/feed/show.rss.builder new file mode 100644 index 0000000..88c0dd8 --- /dev/null +++ b/app/views/feed/show.rss.builder @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f11c29d..58c8d8c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,8 +7,11 @@ <%= csrf_meta_tags %> + <% if flash[:notice] -%> +
<%= h flash[:notice] %>
+ <% end -%> -<%= yield %> + <%= yield %> diff --git a/config/routes.rb b/config/routes.rb index 4a837c0..4b69b19 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 index 0000000..7d40b1a --- /dev/null +++ b/spec/controllers/feed_controller_spec.rb @@ -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 index 0000000..614ffda --- /dev/null +++ b/spec/helpers/feed_helper_spec.rb @@ -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