From 0261bc131805933e3f1bd1015060a42f583347d6 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Tue, 19 Mar 2013 15:01:15 +0000 Subject: [PATCH] Create new feeds in the context of a specified layer --- app/controllers/feeds_controller.rb | 20 ++++++++++++++++++-- app/models/feed.rb | 1 + app/views/feeds/_form.html.haml | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/controllers/feeds_controller.rb b/app/controllers/feeds_controller.rb index 2f6720d..28cf6fc 100644 --- a/app/controllers/feeds_controller.rb +++ b/app/controllers/feeds_controller.rb @@ -44,11 +44,27 @@ class FeedsController < ApplicationController # POST /feeds # POST /feeds.json def create - @feed = Feed.new(params[:feed]) + + + Rails.logger.debug "Feed URL: %s" % params['feed']['feed_url'] + + if Feed.where(:feed_url => params['feed']['feed_url']).size == 1 # ensure this test returns the values we expect + Rails.logger.debug "Adding existing feed to a new layer" + @feed = Feed.where(:feed_url => params['feed']['feed_url']).first + @layer = Layer.find(params['feed']['new_layer_id']) # assumes that the specified layer exists + # Attach the existing feed to the specified layer (making sure we only add each one once) + @feed.layers << @layer unless @feed.layers.include?(@layer) + else + # Create a new feed + Rails.logger.debug "Creating a new feed" + @feed = Feed.new(params[:feed]) + @layer = Layer.find(params['feed']['new_layer_id']) # assumes that the specified layer exists + @feed.layers << @layer unless @feed.layers.include?(@layer) + end respond_to do |format| if @feed.save - format.html { redirect_to '/', notice: 'Feed added OK' } + format.html { redirect_to @layer, notice: 'Feed added OK' } format.json { render json: @feed, status: :created, location: @feed } else format.html { render action: "new" } diff --git a/app/models/feed.rb b/app/models/feed.rb index 755bb04..e07fc45 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -2,6 +2,7 @@ class Feed < ActiveRecord::Base has_many :posts, :dependent => :destroy has_and_belongs_to_many :layers attr_accessible :title, :url, :description, :generator, :last_fetched, :feed_url + attr_accessor :new_layer_id # non model attribute used when creating new feeds from within a layer validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL" diff --git a/app/views/feeds/_form.html.haml b/app/views/feeds/_form.html.haml index 70a4b73..60e59dc 100644 --- a/app/views/feeds/_form.html.haml +++ b/app/views/feeds/_form.html.haml @@ -15,6 +15,7 @@ .field = f.label "URL" = f.text_field :feed_url, :size => 120 + = f.hidden_field :new_layer_id, :value => @feed.new_layer_id = f.submit 'Save', :id => 'submit' -# .field