Skip to content
Browse files

Stale hidim removal

  • Loading branch information...
1 parent 36dc181 commit b180e9921a623039142643c9a85da40967c27a2b Michael Nutt committed Jul 8, 2009
Showing with 39 additions and 0 deletions.
  1. +7 −0 app/models/hidim.rb
  2. +1 −0 config/crontab.sample
  3. +31 −0 spec/models/hidim_spec.rb
View
7 app/models/hidim.rb
@@ -21,6 +21,13 @@ class Hidim < ActiveRecord::Base
before_create :convert_to_png
named_scope :featured, :conditions => {:featured => true}
+ named_scope :not_featured, :conditions => {:featured => nil}
+
+ named_scope :stale, lambda { { :conditions => ['created_at < ?', 5.minutes.ago] } }
+
+ def self.clear_stale
+ Hidim.not_featured.stale.destroy_all
+ end
def generate_token
self.token = rand(36**8).to_s(36) if self.new_record? and self.token.nil?
View
1 config/crontab.sample
@@ -0,0 +1 @@
+*/5 * * * * /var/www/hid.im/script/runner -e production 'Hidim.clear_stale; Rails.logger.flush'
View
31 spec/models/hidim_spec.rb
@@ -60,4 +60,35 @@
@hidim.errors[:torrent].should =~ /select a file/
end
end
+
+ describe "fetching" do
+ before(:each) do
+ Hidim.stub!(:save_attached_files).and_return(true)
+ Hidim.stub!(:delete_attached_files).and_return(true)
+
+ # fixtures/factories not worth it, yet...
+ @featured = Hidim.create!(:torrent => File.open("#{RAILS_ROOT}/spec/data/test.torrent"))
+ @featured.featured = true; @featured.save!
+ @not_featured = Hidim.create!(:torrent => File.open("#{RAILS_ROOT}/spec/data/test.torrent"))
+ @stale = Hidim.create!(:torrent => File.open("#{RAILS_ROOT}/spec/data/test.torrent"))
+ @stale.created_at = 1.day.ago; @stale.save!
+ end
+
+ it "should get featured hidims" do
+ Hidim.featured.should == [@featured]
+ end
+
+ it "should get non_featured hidims" do
+ Hidim.not_featured.should == [@not_featured, @stale]
+ end
+
+ it "should get stale hidims" do
+ Hidim.not_featured.stale.should == [@stale]
+ end
+
+ it "should remove stale hidims" do
+ Hidim.clear_stale
+ Hidim.all.should == [@featured, @not_featured]
+ end
+ end
end

0 comments on commit b180e99

Please sign in to comment.
Something went wrong with that request. Please try again.