X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=app%2Fcontrollers%2Ffeed_controller.rb;h=8e89663852aa502a7da8eead64a9726e22dc928c;hb=0c831f9107670526c653c82f182b0b6983f49c31;hp=7fa4e283185e163323ea5d4dbea1944cbd84ae28;hpb=87b29f4f401626c3b21df387cf7cd75677ea56cf;p=feedcatcher.git diff --git a/app/controllers/feed_controller.rb b/app/controllers/feed_controller.rb index 7fa4e28..8e89663 100644 --- a/app/controllers/feed_controller.rb +++ b/app/controllers/feed_controller.rb @@ -1,13 +1,116 @@ class FeedController < ApplicationController + + skip_before_filter :verify_authenticity_token + def index - @feeds = FeedItem.find(:all, :select => 'DISTINCT feed_name') + # @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 - @feed_items = FeedItem.find_all_by_feed_name(params[:feed_name]) + 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