views:

27

answers:

1

This could be a HappyMapper specific question, but I don't think so.

In my app, users can upload their blog subscriptions (via an OPML file), which I parse and add to their profile. The only problem is during the parsing, or more specifically the creation of each subscription, I can't figure out how to skip over entries that are just "labels".

Since OPML files allow you to label your blogs, or organize them into folders, this is my problem. The actual blog subscriptions and their labels both have "outline" tags.

<outline text="Rails" >
<outline title="Katz Got Your Tongue?" text="Katz Got Your Tongue?" htmlUrl="http://yehudakatz.com" type="rss" xmlUrl="http://feeds.feedburner.com/KatzGotYourTongue" />

After parsing, I create each feed via a method call inside of the HappyMapper module

  def create_feed
    Feed.new( :feed_htmlUrl => self.htmlUrl, :feed_title => self.title, ...

But how do I prevent it from creating new "feeds" for those outline tags that are just tags? (i.e. those that don't have an htmlUrl?)

A: 

I will try this:

In the Feed model class, we use a before_create filter. As follows:

class Feed < ActiveRecord::Base
  before_create :validate_attribute

  private

  def validate_attribute
    return false if self.htmlUrl.blank?
    # place more validation here
  end
end

Doing this, only new record that satisfy validate_attribute will be created.

Hopefully it helps.

SamChandra
Hey thanks SamChandra! ...but I tried that to no avail. Might could get it working, but it sorta feels a bit too hackish. I still don't understand why I can't just catch and ignore the feed creation in the actual create_feed method if the htmlUrl is blank. It's honestly blowing my mind. Well, thank you again SamChandra. I'll keep playing around with it.
GoodGets