X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;ds=sidebyside;f=app%2Fcontrollers%2Ffeed_controller.rb;h=2d6fee9786f62166fb4414cef8fa241c67c7f96c;hb=acd80c1fc3212b41c0b05a8f5ac2ad7edc990751;hp=71ab1df4f829d2530acf4163143ab6a042481099;hpb=0f4b492932a509694b7621c1198da336af75e582;p=feedcatcher.git diff --git a/app/controllers/feed_controller.rb b/app/controllers/feed_controller.rb index 71ab1df..2d6fee9 100644 --- a/app/controllers/feed_controller.rb +++ b/app/controllers/feed_controller.rb @@ -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