diff --git a/app/models/block.rb b/app/models/block.rb index b0db40a..77541a3 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -13,6 +13,8 @@ class Block < ActiveRecord::Base acts_as_having_settings settings_items :visible, :type => :boolean, :default => true + named_scope :enabled, :conditions => { :enabled => true } + def visible? visible end diff --git a/app/models/external_feed.rb b/app/models/external_feed.rb index 9d8f8bb..6513346 100644 --- a/app/models/external_feed.rb +++ b/app/models/external_feed.rb @@ -5,6 +5,11 @@ class ExternalFeed < ActiveRecord::Base validates_presence_of :address, :if => lambda {|efeed| efeed.enabled} validates_uniqueness_of :blog_id + named_scope :enabled, :conditions => { :enabled => true } + named_scope :expired, lambda { + { :conditions => ['(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval] } + } + def add_item(title, link, date, content) article = TinyMceArticle.new(:name => title, :profile => blog.profile, :body => content, :published_at => date, :source => link, :profile => blog.profile, :parent => blog) unless blog.children.exists?(:slug => article.slug) @@ -16,9 +21,10 @@ class ExternalFeed < ActiveRecord::Base # do nothing end def finish_fetch - if self.only_once - self.enabled = 'false' + if self.only_once && self.update_errors.zero? + self.enabled = false end + self.fetched_at = Time.now self.save! end diff --git a/app/models/feed_reader_block.rb b/app/models/feed_reader_block.rb index 2d6e3bf..cc04bf8 100644 --- a/app/models/feed_reader_block.rb +++ b/app/models/feed_reader_block.rb @@ -1,14 +1,32 @@ class FeedReaderBlock < Block + def initialize(attributes = nil) + data = attributes || {} + super({ :enabled => !data[:address].blank? }.merge(data)) + end + include DatesHelper settings_items :address, :type => :string + alias :orig_set_address :address= + def address=(new_address) + old_address = address + orig_set_address(new_address) + self.enabled = (old_address.blank? && !new_address.blank?) || (new_address && new_address != old_address) || false + end + settings_items :limit, :type => :integer - settings_items :fetched_at, :type => :date settings_items :feed_title, :type => :string settings_items :feed_items, :type => :array + settings_items :update_errors, :type => :integer, :default => 0 + settings_items :error_message, :type => :string + + named_scope :expired, lambda { + { :conditions => [ '(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval] } + } + before_create do |block| block.limit = 5 block.feed_items = [] @@ -27,9 +45,13 @@ class FeedReaderBlock < Block end def formatted_feed_content - return "