Browse Source

Create new feeds in the context of a specified layer

master
Adrian Short 11 years ago
parent
commit
0261bc1318
3 changed files with 20 additions and 2 deletions
  1. +18
    -2
      app/controllers/feeds_controller.rb
  2. +1
    -0
      app/models/feed.rb
  3. +1
    -0
      app/views/feeds/_form.html.haml

+ 18
- 2
app/controllers/feeds_controller.rb View File

@@ -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" }


+ 1
- 0
app/models/feed.rb View File

@@ -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"


+ 1
- 0
app/views/feeds/_form.html.haml View File

@@ -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


Loading…
Cancel
Save