diff --git a/Gemfile b/Gemfile index a3bd122c4..800b5dc6c 100644 --- a/Gemfile +++ b/Gemfile @@ -20,3 +20,4 @@ gem "mechanize" gem "nokogiri" gem "meta_search", :git => "git://github.com/ernie/meta_search.git" gem "silent-postgres" +gem "whenever", :require => false \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index c564232ab..c7b2c5032 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,6 +16,7 @@ GIT GEM remote: http://gemcutter.org/ specs: + aaronh-chronic (0.3.9) actionmailer (3.1.0.rc1) actionpack (= 3.1.0.rc1) mail (~> 2.3.0) @@ -124,6 +125,9 @@ GEM tzinfo (0.3.29) webrobots (0.0.10) nokogiri (>= 1.4.4) + whenever (0.6.8) + aaronh-chronic (>= 0.3.9) + activesupport (>= 2.3.4) PLATFORMS ruby @@ -146,3 +150,4 @@ DEPENDENCIES simple_form simplecov super_exception_notifier + whenever diff --git a/app/models/post.rb b/app/models/post.rb index 2c9807bcc..78610f88e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -488,16 +488,20 @@ class Post < ActiveRecord::Base def add_tag_subscription_relation(subscriptions, relation) subscriptions.each do |subscription| - subscription =~ /^(.+?):(.+)$/ - user_name = $1 || subscription - subscription_name = $2 - - user = User.find_by_name(user_name) - - if user + if subscription =~ /^(.+?):(.+)$/ + user_name = $1 + subscription_name = $2 + user = User.find_by_name(user_name) + return relation if user.nil? post_ids = TagSubscription.find_post_ids(user.id, subscription_name) - relation = relation.where(["posts.id IN (?)", post_ids]) + else + user = User.find_by_name(subscription) + return relation if user.nil? + post_ids = TagSubscription.find_post_ids(user.id) end + + post_ids = [0] if post_ids.empty? + relation = relation.where(["posts.id IN (?)", post_ids]) end relation diff --git a/app/models/tag.rb b/app/models/tag.rb index 2df386603..610715317 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -310,11 +310,11 @@ class Tag < ActiveRecord::Base module RelationMethods def update_related counts = RelatedTagCalculator.calculate_from_sample(Danbooru.config.post_sample_size, name) - self.related_tags = RelatedTagCalculator.convert_hash_to_string(counts) + update_attributes(:related_tags => RelatedTagCalculator.convert_hash_to_string(counts), :related_tags_updated_at => Time.now) end def update_related_if_outdated - updated_related if should_update_related? + delay.update_related if should_update_related? end def related_cache_expiry diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 000000000..53fdc0a39 --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,5 @@ +set :output, "/var/log/whenever.log" + +every 1.hour do + TagSubscription.process_all +end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 7303b3d31..dfb18646f 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -525,7 +525,13 @@ class PostTest < ActiveSupport::TestCase assert_equal(post2.id, relation.first.id) end - should "return posts for a tag subscription search" + should "return posts for a tag subscription search" do + post1 = Factory.create(:post, :tag_string => "aaa") + sub = Factory.create(:tag_subscription, :tag_query => "aaa", :name => "zzz") + TagSubscription.process_all + relation = Post.tag_match("sub:#{CurrentUser.name}") + assert_equal(1, relation.count) + end should "return posts for a particular rating" do post1 = Factory.create(:post, :rating => "s") diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index 0fecf7e52..bf6941788 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -6,6 +6,7 @@ class TagTest < ActiveSupport::TestCase CurrentUser.user = user CurrentUser.ip_addr = "127.0.0.1" MEMCACHE.flush_all + Delayed::Worker.delay_jobs = false end teardown do diff --git a/tmp/test.jpg b/tmp/test.jpg new file mode 100644 index 000000000..253c508d9 Binary files /dev/null and b/tmp/test.jpg differ