diff --git a/app/models/artist.rb b/app/models/artist.rb index 64186345d..126875ddd 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -1,5 +1,6 @@ class Artist < ActiveRecord::Base attr_accessor :updater_id, :updater_ip_addr + before_create :initialize_creator before_save :normalize_name after_save :create_version after_save :commit_url_string @@ -11,7 +12,7 @@ class Artist < ActiveRecord::Base has_one :wiki_page, :foreign_key => "title", :primary_key => "name" has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name" accepts_nested_attributes_for :wiki_page - attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes + attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :updater_id, :updater_ip_addr module UrlMethods module ClassMethods @@ -152,5 +153,11 @@ class Artist < ActiveRecord::Base include UpdaterMethods extend SearchMethods include VersionMethods + + def initialize_creator + if creator.nil? + self.creator_id = updater_id + end + end end diff --git a/app/models/pool.rb b/app/models/pool.rb index 9dff10c0b..62c1bec60 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -10,9 +10,15 @@ class Pool < ActiveRecord::Base attr_accessible :name, :description, :post_ids, :is_public, :is_active def self.create_anonymous(creator, creator_ip_addr) - pool = Pool.create(:name => "TEMP:#{Time.now.to_f}.#{rand(1_000_000)}", :creator => creator, :updater_id => creator.id, :updater_ip_addr => creator_ip_addr) - pool.update_attribute(:name, "anonymous:#{pool.id}") - pool + Pool.new do |pool| + pool.name = "TEMP:#{Time.now.to_f}.#{rand(1_000_000)}" + pool.creator = creator + pool.updater_id = creator.id + pool.updater_ip_addr = creator_ip_addr + pool.save + pool.name = "anonymous:#{pool.id}" + pool.save + end end def revert_to!(version) @@ -63,7 +69,7 @@ class Pool < ActiveRecord::Base def create_version last_version = versions.last - + if last_version && updater_ip_addr == last_version.updater_ip_addr && updater_id == last_version.updater_id last_version.update_attribute(:post_ids, post_ids) else diff --git a/app/models/post.rb b/app/models/post.rb index 53a20ce39..34b365888 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -13,7 +13,7 @@ class Post < ActiveRecord::Base has_one :upload, :dependent => :destroy has_many :versions, :class_name => "PostVersion", :dependent => :destroy has_many :votes, :class_name => "PostVote", :dependent => :destroy - attr_accessible :source, :rating, :tag_string, :old_tag_string + attr_accessible :source, :rating, :tag_string, :old_tag_string, :updater_id, :updater_ip_addr module FileMethods def delete_files diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index f27031fbe..83d2bf5ad 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -1,12 +1,13 @@ class WikiPage < ActiveRecord::Base attr_accessor :updater_id, :updater_ip_addr before_save :normalize_title + before_create :initialize_creator after_save :create_version belongs_to :creator, :class_name => "User" belongs_to :updater, :class_name => "User" validates_uniqueness_of :title, :case_sensitive => false validates_presence_of :body, :updater_id, :updater_ip_addr - attr_protected :text_search_index, :is_locked, :version + attr_accessible :title, :body, :updater_id, :updater_ip_addr scope :titled, lambda {|title| where(["title = ?", title.downcase.tr(" ", "_")])} has_one :tag, :foreign_key => "name", :primary_key => "title" has_one :artist, :foreign_key => "name", :primary_key => "title" @@ -58,4 +59,10 @@ class WikiPage < ActiveRecord::Base :is_locked => is_locked ) end + + def initialize_creator + if creator.nil? + self.creator_id = updater_id + end + end end diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 3d56c0bcc..35cc4541d 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -27,11 +27,10 @@ class ArtistTest < ActiveSupport::TestCase should "make sure old urls are deleted" do artist = Factory.create(:artist, :name => "rembrandt", :url_string => "http://rembrandt.com/test.jpg") - artist.update_attributes( - :updater_id => artist.creator_id, - :updater_ip_addr => "127.0.0.1", - :url_string => "http://not.rembrandt.com/test.jpg" - ) + artist.updater_id = artist.creator_id + artist.updater_ip_addr = "127.0.0.1" + artist.url_string = "http://not.rembrandt.com/test.jpg" + artist.save artist.reload assert_equal(["http://not.rembrandt.com/test.jpg"], artist.artist_urls.map(&:to_s).sort) end @@ -80,11 +79,11 @@ class ArtistTest < ActiveSupport::TestCase should "have an associated wiki" do user = Factory.create(:user) - artist = Factory.create(:artist, :name => "max", :wiki_page_attributes => {:body => "this is max", :creator_id => user.id, :updater_id => user.id, :updater_ip_addr => "127.0.0.1"}) + artist = Factory.create(:artist, :name => "max", :wiki_page_attributes => {:body => "this is max", :updater_id => user.id, :updater_ip_addr => "127.0.0.1"}) assert_not_nil(artist.wiki_page) assert_equal("this is max", artist.wiki_page.body) - artist.update_attributes(:wiki_page_attributes => {:id => artist.wiki_page.id, :body => "this is hoge mark ii", :updater_id => user.id, :updater_ip_addr => "127.0.0.1"}) + artist.update_attributes(:wiki_page_attributes => {:id => artist.wiki_page.id, :body => "this is hoge mark ii", :creator_id => user.id, :updater_id => user.id, :updater_ip_addr => "127.0.0.1"}) assert_equal("this is hoge mark ii", artist.wiki_page(true).body) end @@ -96,11 +95,10 @@ class ArtistTest < ActiveSupport::TestCase end assert_difference("ArtistVersion.count") do - artist.update_attributes( - :updater_id => user.id, - :updater_ip_addr => "127.0.0.1", - :other_names => "xxx" - ) + artist.updater_id = user.id + artist.updater_ip_addr = "127.0.0.1" + artist.other_names = "xxx" + artist.save end first_version = ArtistVersion.first diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index 973f163fd..37f3f0d92 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -2,13 +2,23 @@ require File.dirname(__FILE__) + '/../test_helper' class PoolTest < ActiveSupport::TestCase context "A pool" do + setup do + MEMCACHE.flush_all + end + should "create versions for each distinct user" do pool = Factory.create(:pool) user = Factory.create(:user) assert_equal(1, pool.versions(true).size) - pool.update_attributes(:post_ids => "1", :updater_id => user.id, :updater_ip_addr => "128.0.0.1") + pool.post_ids = "1" + pool.updater_id = user.id + pool.updater_ip_addr = "128.0.0.1" + pool.save assert_equal(2, pool.versions(true).size) - pool.update_attributes(:post_ids => "1 2", :updater_id => user.id, :updater_ip_addr => "128.0.0.1") + pool.post_ids = "1 2" + pool.updater_id = user.id + pool.updater_ip_addr = "128.0.0.1" + pool.save assert_equal(2, pool.versions(true).size) pool.revert_to!(PoolVersion.first) assert_equal("", pool.post_ids) @@ -59,7 +69,6 @@ class PoolTest < ActiveSupport::TestCase user = Factory.create(:user) pool = Pool.create_anonymous(user, "127.0.0.1") assert_match(/^anonymous:\d+$/, pool.name) - end end end diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index 483488ba5..7333cc98b 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -27,7 +27,10 @@ class WikiPageTest < ActiveSupport::TestCase end assert_difference("WikiPageVersion.count") do - wp.update_attributes(:title => "yyy", :updater_id => user.id, :updater_ip_addr => "127.0.0.1") + wp.title = "yyy" + wp.updater_id = user.id + wp.updater_ip_addr = "127.0.0.1" + wp.save end version = WikiPageVersion.first