This could be a HappyMapper specific question, but I don't think so.
In my app, users can upload their b开发者_Python百科log 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?)
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.
精彩评论