From ce10a72bb6f56d89e1245e58c1a7d1f01322f65e Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 3 May 2013 17:29:41 -0700 Subject: [PATCH] fixes #1396, fix tests --- app/models/note.rb | 8 ++++++++ app/models/tag.rb | 6 +----- script/fixes/013.rb | 16 ---------------- script/fixes/014_note_ranges.rb | 15 +++++++++++++++ test/unit/artist_url_test.rb | 4 ++-- test/unit/note_test.rb | 14 ++++++++++---- 6 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 script/fixes/014_note_ranges.rb diff --git a/app/models/note.rb b/app/models/note.rb index d7a75cea6..cf7c31b65 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -7,6 +7,7 @@ class Note < ActiveRecord::Base before_validation :initialize_updater before_validation :blank_body validates_presence_of :post_id, :creator_id, :updater_id, :x, :y, :width, :height + validate :coordinates_in_range, :message => "must be inside the image" has_many :versions, :class_name => "NoteVersion", :order => "note_versions.id ASC" after_save :update_post after_save :create_version @@ -95,6 +96,13 @@ class Note < ActiveRecord::Base self.updater_ip_addr = CurrentUser.ip_addr end + def coordinates_in_range + if x < 0 || y < 0 || (x > post.image_width) || (y > post.image_height) + self.errors.add(:coordinates, "must be inside the image") + return false + end + end + def post_must_not_be_note_locked if is_locked? errors.add :post, "is note locked" diff --git a/app/models/tag.rb b/app/models/tag.rb index 5a9a1a6ec..56868e142 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -104,11 +104,7 @@ class Tag < ActiveRecord::Base Post.raw_tag_match(name).find_each do |post| post.reload post.set_tag_counts - post.update_column(:tag_count, post.tag_count) - post.update_column(:tag_count_general, post.tag_count_general) - post.update_column(:tag_count_artist, post.tag_count_artist) - post.update_column(:tag_count_copyright, post.tag_count_copyright) - post.update_column(:tag_count_character, post.tag_count_character) + Post.update_all({:tag_count => post.tag_count, :tag_count_general => post.tag_count_general, :tag_count_artist => post.tag_count_artist, :tag_count_copyright => post.tag_count_copyright, :tag_count_character => post.tag_count_character}, {:id => post.id}) end end end diff --git a/script/fixes/013.rb b/script/fixes/013.rb index 660a8a81f..d68ef3c56 100644 --- a/script/fixes/013.rb +++ b/script/fixes/013.rb @@ -23,16 +23,6 @@ Artist.where("is_banned = true").find_each do |artist| artist.versions.last.update_column(:is_banned, true) end ; true -User.find_each do |user| - puts "updating user #{user.id}" - user.update_column(:favorite_count, Favorite.for_user(user).where("user_id = ?", user.id).count) -end ; true - -Post.find_each do |post| - puts "updating post #{post.id}" - post.update_column(:fav_count, Favorite.where("post_id = #{post.id}").count) -end ; true - danbooru_conn = PGconn.connect(dbname: 'danbooru') danbooru2_conn = PGconn.connect(dbname: "danbooru2") danbooru_conn.exec("set statement_timeout = 0") @@ -42,9 +32,3 @@ danbooru_conn.exec( "SELECT * FROM comments WHERE id < 29130" ) do |result| danbooru2_conn.exec "insert into comments (id, created_at, updated_at, post_id, creator_id, body, ip_addr, score, updater_id, updater_ip_addr) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", [row["id"], row["created_at"], row["created_at"], row["post_id"], row["user_id"], row["body"], row["ip_addr"], row["score"], row["user_id"], row["ip_addr"]] end end - - -# Post.select("id, score, up_score, down_score, fav_count").find_each do |post| -# post.update_column(:score, post.up_score + post.down_score) -# end ; true - diff --git a/script/fixes/014_note_ranges.rb b/script/fixes/014_note_ranges.rb new file mode 100644 index 000000000..2727b119a --- /dev/null +++ b/script/fixes/014_note_ranges.rb @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) + +ActiveRecord::Base.connection.execute("set statement_timeout = 0") + +Note.update_all("x = 0", "x < 0") +Note.update_all("y = 0", "y < 0") +Note.update_all("x = 0", "x > (select _.image_width from posts _ where _.id = notes.id limit 1)") +Note.update_all("y = 0", "y > (select _.image_height from posts _ where _.id = notes.id limit 1)") + +Post.where("created_at >= '2013-02-01'").select("id, score, up_score, down_score").find_each do |post| + fav_count = + post.update_column(:score, post.up_score + post.down_score) +end ; true diff --git a/test/unit/artist_url_test.rb b/test/unit/artist_url_test.rb index 5537ea906..aaeb62112 100644 --- a/test/unit/artist_url_test.rb +++ b/test/unit/artist_url_test.rb @@ -40,8 +40,8 @@ class ArtistUrlTest < ActiveSupport::TestCase end should "normalize pixiv urls" do - url = FactoryGirl.create(:artist_url, :url => "http://img55.pixiv.net/monet") - assert_equal("http://img55.pixiv.net/monet", url.url) + url = FactoryGirl.create(:artist_url, :url => "http://img55.pixiv.net/img/monet") + assert_equal("http://img55.pixiv.net/img/monet", url.url) assert_equal("http://img.pixiv.net/monet/", url.normalized_url) end end diff --git a/test/unit/note_test.rb b/test/unit/note_test.rb index da44e81bb..db0aad375 100644 --- a/test/unit/note_test.rb +++ b/test/unit/note_test.rb @@ -34,7 +34,13 @@ class NoteTest < ActiveSupport::TestCase context "creating a note" do setup do - @post = FactoryGirl.create(:post) + @post = FactoryGirl.create(:post, :image_width => 1000, :image_height => 1000) + end + + should "not validate if the note is outside the image" do + @note = FactoryGirl.build(:note, :x => 1001, :y => 500, :post => @post) + @note.save + assert_equal(["Coordinates must be inside the image"], @note.errors.full_messages) end should "create a version" do @@ -72,7 +78,7 @@ class NoteTest < ActiveSupport::TestCase context "updating a note" do setup do - @post = FactoryGirl.create(:post) + @post = FactoryGirl.create(:post, :image_width => 1000, :image_height => 1000) @note = FactoryGirl.create(:note, :post => @post) end @@ -84,7 +90,7 @@ class NoteTest < ActiveSupport::TestCase should "update the post's last_noted_at field" do assert_nil(@post.last_noted_at) - @note.update_attributes(:x => 1000) + @note.update_attributes(:x => 500) @post.reload assert_equal(@post.last_noted_at.to_i, @note.updated_at.to_i) end @@ -105,7 +111,7 @@ class NoteTest < ActiveSupport::TestCase end should "fail" do - @note.update_attributes(:x => 1000) + @note.update_attributes(:x => 500) assert_equal(["Post is note locked"], @note.errors.full_messages) end end