Auto-add automatic tags whenever post is updated

Instead of being limited to when it's first uploaded.
This commit is contained in:
Toks
2013-12-15 19:39:10 -05:00
parent 7e79509b98
commit afa4e83d9b
5 changed files with 61 additions and 60 deletions

View File

@@ -424,6 +424,7 @@ class Post < ActiveRecord::Base
normalized_tags = TagAlias.to_aliased(normalized_tags)
normalized_tags = TagImplication.with_descendants(normalized_tags)
normalized_tags = %w(tagme) if normalized_tags.empty?
normalized_tags = add_automatic_tags(normalized_tags)
normalized_tags.sort!
set_tag_string(normalized_tags.uniq.sort.join(" "))
end
@@ -434,6 +435,37 @@ class Post < ActiveRecord::Base
return tags - negated_tags
end
def add_automatic_tags(tags)
return tags if !Danbooru.config.enable_dimension_autotagging
tags -= %w(incredibly_absurdres absurdres highres lowres huge_filesize)
if image_width >= 10_000 || image_height >= 10_000
tags << "incredibly_absurdres"
end
if image_width >= 3200 || image_height >= 2400
tags << "absurdres"
end
if image_width >= 1600 || image_height >= 1200
tags << "highres"
end
if image_width <= 500 && image_height <= 500
tags << "lowres"
end
if file_size >= 10.megabytes
tags << "huge_filesize"
end
if image_width >= 1024 && image_width.to_f / image_height >= 4
tags << "wide_image"
elsif image_height >= 1024 && image_height.to_f / image_width >= 4
tags << "tag_image"
end
return tags
end
def filter_metatags(tags)
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent):/i}
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|child):/i}

View File

@@ -77,10 +77,8 @@ class Upload < ActiveRecord::Base
validate_md5_uniqueness
validate_md5_confirmation
calculate_file_size(file_path)
add_file_size_tags!(file_path)
if has_dimensions?
calculate_dimensions(file_path)
add_dimension_tags!
end
generate_resizes(file_path)
move_file
@@ -174,31 +172,6 @@ class Upload < ActiveRecord::Base
end
end
def add_dimension_tags!
return if !Danbooru.config.enable_dimension_autotagging
%w(incredibly_absurdres absurdres highres lowres).each do |tag|
escaped_tag = Regexp.escape(tag)
self.tag_string = tag_string.gsub(/(?:\A| )#{escaped_tag}(?:\Z| )/, " ").strip
end
if image_width >= 10_000 || image_height >= 10_000
self.tag_string = "#{tag_string} incredibly_absurdres".strip
elsif image_width >= 3200 || image_height >= 2400
self.tag_string = "#{tag_string} absurdres".strip
elsif image_width >= 1600 || image_height >= 1200
self.tag_string = "#{tag_string} highres".strip
elsif image_width <= 500 && image_height <= 500
self.tag_string = "#{tag_string} lowres".strip
end
if image_width >= 1024 && image_width.to_f / image_height >= 4
self.tag_string = "#{tag_string} wide_image".strip
elsif image_height >= 1024 && image_height.to_f / image_width >= 4
self.tag_string = "#{tag_string} tall_image".strip
end
end
# Does this file have image dimensions?
def has_dimensions?
%w(jpg gif png swf).include?(file_ext)
@@ -406,12 +379,6 @@ class Upload < ActiveRecord::Base
extend SearchMethods
include ApiMethods
def add_file_size_tags!(file_path)
if file_size >= 10.megabytes
self.tag_string = "#{tag_string} huge_filesize".strip
end
end
def uploader_name
User.id_to_name(uploader_id)
end

View File

@@ -7,8 +7,8 @@ FactoryGirl.define do
tag_count 2
tag_count_general 2
file_ext "jpg"
image_width 100
image_height 200
image_width 1500
image_height 1000
file_size 2000
rating "q"
end

View File

@@ -608,6 +608,33 @@ class PostTest < ActiveSupport::TestCase
assert_equal(%w(tag1 tag2), post.tag_array_was)
end
context "with large dimensions" do
setup do
@post.image_width = 10_000
@post.image_height = 10
@post.tag_string = ""
@post.save
end
should "have the appropriate dimension tags added automatically" do
assert_match(/incredibly_absurdres/, @post.tag_string)
assert_match(/absurdres/, @post.tag_string)
assert_match(/highres/, @post.tag_string)
end
end
context "with a large file size" do
setup do
@post.file_size = 11.megabytes
@post.tag_string = ""
@post.save
end
should "have the appropriate file size tags added automatically" do
assert_match(/huge_filesize/, @post.tag_string)
end
end
context "that has been updated" do
should "create a new version if it's the first version" do
assert_difference("PostVersion.count", 1) do

View File

@@ -36,31 +36,6 @@ class UploadTest < ActiveSupport::TestCase
end
end
context "that has incredibly absurd res dimensions" do
setup do
@upload = FactoryGirl.build(:jpg_upload, :tag_string => "")
@upload.image_width = 10_000
@upload.image_height = 10
@upload.add_dimension_tags!
end
should "have the incredibly_absurdres tag" do
assert_match(/incredibly_absurdres/, @upload.tag_string)
end
end
context "that has a large flie size" do
setup do
@upload = FactoryGirl.build(:jpg_upload, :tag_string => "")
@upload.file_size = 11.megabytes
@upload.add_file_size_tags!(@upload.file_path)
end
should "have the huge_filesize tag" do
assert_match(/huge_filesize/, @upload.tag_string)
end
end
context "image size calculator" do
should "discover the dimensions for a compressed SWF" do
@upload = FactoryGirl.create(:upload, :file_path => "#{Rails.root}/test/files/compressed.swf")