From c818f2c11440877dac0bfb191d6d9c92bb48e23a Mon Sep 17 00:00:00 2001 From: albert Date: Tue, 13 Sep 2011 19:08:39 -0400 Subject: [PATCH] fixed pixiv tests, added metatag support when tagging fixes #23: Unable to add to pool using "pool:XXX" syntax --- app/models/post.rb | 27 +++++++++++- test/unit/pixiv_proxy_test.rb | 8 +--- test/unit/post_test.rb | 79 ++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 10 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 4bead0eb5..263d7eff1 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -414,7 +414,32 @@ class Post < ActiveRecord::Base end def filter_metatags(tags) - tags.reject {|tag| tag =~ /\A(?:pool|rating|fav):/} + metatags, tags = tags.partition {|x| x =~ /\A(?:pool|rating|fav):/} + apply_metatags(metatags) + return tags + end + + def apply_metatags(tags) + tags.each do |tag| + case tag + when /^pool:(\d+)$/ + pool = Pool.find_by_id($1.to_i) + add_pool!(pool) if pool + + when /^pool:(.+)$/ + pool = Pool.find_by_name($1) + if pool.nil? + pool = Pool.create(:name => $1, :description => "This pool was automatically generated") + end + add_pool!(pool) + + when /^rating:([qse])/i + self.rating = $1.downcase + + when /^fav:(.+)$/ + add_favorite!(CurrentUser.user) + end + end end def has_tag?(tag) diff --git a/test/unit/pixiv_proxy_test.rb b/test/unit/pixiv_proxy_test.rb index eb592dff0..1e5452a24 100644 --- a/test/unit/pixiv_proxy_test.rb +++ b/test/unit/pixiv_proxy_test.rb @@ -4,15 +4,9 @@ require 'test_helper' class PixivProxyTest < ActiveSupport::TestCase context "The proxy" do - should "get the profile" do - results = PixivProxy.get_profile("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484") - assert_equal("シビレ\347\275\240", results[:artist]) - assert_equal("/member_illust.php?id=9646484", results[:listing_url]) - end - should "get a single post" do results = PixivProxy.get_single("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484") - assert_equal("/member.php?id=4015", results[:profile_url]) + assert_equal("member.php?id=4015", results[:profile_url]) assert(results[:jp_tags].size > 0) first_tag = results[:jp_tags][0] assert_equal(2, first_tag.size) diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 9654feada..f27b608eb 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -2,8 +2,8 @@ require 'test_helper' class PostTest < ActiveSupport::TestCase setup do - user = Factory.create(:user) - CurrentUser.user = user + @user = Factory.create(:user) + CurrentUser.user = @user CurrentUser.ip_addr = "127.0.0.1" MEMCACHE.flush_all end @@ -210,6 +210,81 @@ class PostTest < ActiveSupport::TestCase context "Tagging:" do context "A post" do + setup do + @post = Factory.create(:post) + end + + context "tagged with a metatag" do + context "for a pool" do + context "id" do + setup do + @pool = Factory.create(:pool) + @post.update_attributes(:tag_string => "aaa pool:#{@pool.id}") + end + + should "add the post to the pool" do + @post.reload + @pool.reload + assert_equal("#{@post.id}", @pool.post_ids) + assert_equal("pool:#{@pool.id}", @post.pool_string) + end + end + + context "name" do + context "that exists" do + setup do + @pool = Factory.create(:pool, :name => "abc") + @post.update_attributes(:tag_string => "aaa pool:abc") + end + + should "add the post to the pool" do + @post.reload + @pool.reload + assert_equal("#{@post.id}", @pool.post_ids) + assert_equal("pool:#{@pool.id}", @post.pool_string) + end + end + + context "that doesn't exist" do + should "create a new pool and add the post to that pool" do + @post.update_attributes(:tag_string => "aaa pool:abc") + @pool = Pool.find_by_name("abc") + @post.reload + assert_not_nil(@pool) + assert_equal("#{@post.id}", @pool.post_ids) + assert_equal("pool:#{@pool.id}", @post.pool_string) + end + end + end + end + + context "for a rating" do + context "that is valid" do + should "update the rating" do + @post.update_attributes(:tag_string => "aaa rating:e") + @post.reload + assert_equal("e", @post.rating) + end + end + + context "that is invalid" do + should "not update the rating" do + @post.update_attributes(:tag_string => "aaa rating:z") + @post.reload + assert_equal("q", @post.rating) + end + end + end + + context "for a fav" do + should "add the current user to the post's favorite listing" do + @post.update_attributes(:tag_string => "aaa fav:self") + @post.reload + assert_equal("fav:#{@user.id}", @post.fav_string) + end + end + end + should "have an array representation of its tags" do post = Factory.create(:post) post.set_tag_string("aaa bbb")