* Reworked how post versioning works, now more closely resembles the 1.18 strategy

This commit is contained in:
albert
2011-01-26 18:10:49 -05:00
parent 683d4583ac
commit f7e2344b9f
21 changed files with 292 additions and 308 deletions

View File

@@ -4,6 +4,11 @@ class JanitorTrial < ActiveRecord::Base
after_create :promote_user
after_destroy :create_feedback
validates_presence_of :user
before_validation :initialize_creator
def initialize_creator
self.creator_id = CurrentUser.id
end
def send_dmail
body = "You have been selected as a test janitor. You can now approve pending posts and have access to the moderation interface.\n\nOver the next several weeks your approvals will be monitored. If the majority of them are quality uploads, then you will be promoted to full janitor status which grants you the ability to delete and undelete posts, ban users, and revert tag changes from vandals. If you fail the trial period, you will be demoted back to your original level and you'll receive a negative user record indicating you previously attempted and failed a test janitor trial.\n\nThere is a minimum quota of 5 approvals a week to indicate that you are being active. Remember, the goal isn't to approve as much as possible. It's to filter out borderline-quality art.\n\nIf you have any questions please respond to this message."

View File

@@ -3,7 +3,7 @@ class Post < ActiveRecord::Base
attr_accessor :old_tag_string, :old_parent_id
after_destroy :delete_files
after_save :update_history
after_save :create_version
after_save :update_parent_on_save
before_save :merge_old_tags
before_save :normalize_tags
@@ -16,8 +16,7 @@ class Post < ActiveRecord::Base
belongs_to :parent, :class_name => "Post"
has_one :unapproval, :dependent => :destroy
has_one :upload, :dependent => :destroy
has_one :moderation_detail, :class_name => "PostModerationDetail", :dependent => :destroy
has_one :history, :class_name => "PostHistory"
has_many :versions, :class_name => "PostVersion", :dependent => :destroy
has_many :votes, :class_name => "PostVote", :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :comments
@@ -240,24 +239,6 @@ class Post < ActiveRecord::Base
end
end
module HistoryMethods
def revisions
if history.nil?
update_history
end
history.revisions
end
def update_history
if history.nil?
create_history
end
history << self
end
end
module TagMethods
def tag_array
@tag_array ||= Tag.scan_tags(tag_string)
@@ -788,11 +769,31 @@ class Post < ActiveRecord::Base
end
end
module VersionMethods
def create_version
if created_at == updated_at
versions.create(
:rating => rating,
:source => source,
:add_tags => tag_string,
:parent_id => parent_id
)
else
versions.create(
:rating => rating_changed? ? rating : nil,
:source => source_changed? ? source : nil,
:add_tags => (tag_array - tag_array_was).join(" "),
:del_tags => (tag_array_was - tag_array).join(" "),
:parent_id => parent_id_changed? ? parent_id : nil
)
end
end
end
include FileMethods
include ImageMethods
include ApprovalMethods
include PresenterMethods
include HistoryMethods
include TagMethods
include FavoriteMethods
include UploaderMethods
@@ -803,6 +804,7 @@ class Post < ActiveRecord::Base
include CacheMethods
include ParentMethods
include RemovalMethods
include VersionMethods
def reload(options = nil)
super

View File

@@ -1,114 +0,0 @@
class PostHistory < ActiveRecord::Base
class Error < Exception ; end
class Revision
attr_accessor :prev, :hash, :diff, :tag_array
def initialize(hash)
@hash = hash
@diff = {}
@tag_array = Tag.scan_tags(@hash["tag_string"])
end
def calculate_diff
if prev.nil?
diff[:add] = tag_array
diff[:del] = []
diff[:rating] = rating
diff[:source] = source
diff[:parent_id] = parent_id
else
diff[:del] = prev.tag_array - tag_array
diff[:add] = tag_array - prev.tag_array
if prev.rating != rating
diff[:rating] = rating
end
if prev.source != source
diff[:source] = source
end
if prev.parent_id != parent_id
diff[:parent_id]= parent_id
end
end
end
def rating
hash["rating"]
end
def source
hash["source"]
end
def parent_id
hash["parent_id"]
end
def updated_at
hash["updated_at"]
end
def user_id
hash["user_id"]
end
def presenter
@presenter ||= PostHistoryRevisionPresenter.new(self)
end
end
before_validation :initialize_revisions, :on => :create
belongs_to :post
def self.build_revision_for_post(post)
hash = {
:source => post.source,
:rating => post.rating,
:tag_string => post.tag_string,
:parent_id => post.parent_id,
:user_id => CurrentUser.id,
:ip_addr => CurrentUser.ip_addr,
:updated_at => revision_time
}
end
def self.revision_time
Time.now
end
def initialize_revisions
write_attribute(:revisions, "[]")
end
def revisions
if read_attribute(:revisions).blank?
[]
else
JSON.parse(read_attribute(:revisions))
end
end
def <<(post)
revision = self.class.build_revision_for_post(post)
write_attribute(:revisions, (revisions << revision).to_json)
save
end
def each_revision(&block)
array = revisions.map {|x| Revision.new(x)}
link_revisions(array)
array.each {|x| x.calculate_diff}
array.each(&block)
end
private
def link_revisions(array)
1.upto(array.size - 1) do |i|
array[i].prev = array[i - 1]
end
end
end

View File

@@ -0,0 +1,36 @@
class PostVersion < ActiveRecord::Base
belongs_to :post
belongs_to :updater, :class_name => "User"
before_validation :initialize_updater
def self.create_from_post(post)
if post.created_at == post.updated_at
create_from_created_post(post)
else
create_from_updated_post(post)
end
end
def initialize_updater
self.updater_id = CurrentUser.id
self.updater_ip_addr = CurrentUser.ip_addr
end
def add_tag_array
@add_tag_array ||= add_tags.scan(/\S+/)
end
def del_tag_array
@del_tag_array ||= del_tags.scan(/\S+/)
end
def presenter
PostVersionPresenter.new(self)
end
def reload
@add_tag_array = nil
@del_tag_array = nil
super
end
end