2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-12 10:41:34 +08:00
|
|
|
class PostRevision < ActiveRecord::Base
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :user
|
|
|
|
|
2024-06-27 00:55:05 +08:00
|
|
|
serialize :modifications, Hash
|
2014-03-07 15:59:47 +08:00
|
|
|
|
2016-12-22 13:46:22 +08:00
|
|
|
after_create :create_notification
|
|
|
|
|
2014-10-16 03:09:08 +08:00
|
|
|
def self.ensure_consistency!
|
|
|
|
# 1 - fix the numbers
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<-SQL
|
2014-10-16 03:09:08 +08:00
|
|
|
UPDATE post_revisions
|
|
|
|
SET number = pr.rank
|
2014-10-28 05:06:43 +08:00
|
|
|
FROM (SELECT id, 1 + ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY number, created_at, updated_at) AS rank FROM post_revisions) AS pr
|
2014-10-16 03:09:08 +08:00
|
|
|
WHERE post_revisions.id = pr.id
|
|
|
|
AND post_revisions.number <> pr.rank
|
|
|
|
SQL
|
|
|
|
|
|
|
|
# 2 - fix the versions on the posts
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<-SQL
|
2014-10-16 03:09:08 +08:00
|
|
|
UPDATE posts
|
2014-10-28 05:06:43 +08:00
|
|
|
SET version = 1 + (SELECT COUNT(*) FROM post_revisions WHERE post_id = posts.id),
|
|
|
|
public_version = 1 + (SELECT COUNT(*) FROM post_revisions pr WHERE post_id = posts.id AND pr.hidden = 'f')
|
|
|
|
WHERE version <> 1 + (SELECT COUNT(*) FROM post_revisions WHERE post_id = posts.id)
|
|
|
|
OR public_version <> 1 + (SELECT COUNT(*) FROM post_revisions pr WHERE post_id = posts.id AND pr.hidden = 'f')
|
2014-10-16 03:09:08 +08:00
|
|
|
SQL
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
|
|
|
|
2024-04-08 16:33:33 +08:00
|
|
|
def categories
|
|
|
|
return [] if modifications["category_id"].blank?
|
|
|
|
|
2024-04-10 22:35:42 +08:00
|
|
|
@categories ||= Category.with_parents(modifications["category_id"])
|
2024-04-08 16:33:33 +08:00
|
|
|
end
|
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def hide!
|
2014-10-28 05:06:43 +08:00
|
|
|
update_column(:hidden, true)
|
2014-10-13 16:18:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show!
|
2014-10-28 05:06:43 +08:00
|
|
|
update_column(:hidden, false)
|
2014-10-13 16:18:49 +08:00
|
|
|
end
|
|
|
|
|
2016-12-22 13:46:22 +08:00
|
|
|
def create_notification
|
|
|
|
PostActionNotifier.after_create_post_revision(self)
|
|
|
|
end
|
2023-08-04 09:04:35 +08:00
|
|
|
|
|
|
|
def self.copy(original_post, target_post)
|
|
|
|
cols_to_copy = (column_names - %w[id post_id]).join(", ")
|
|
|
|
|
|
|
|
DB.exec <<~SQL
|
|
|
|
INSERT INTO post_revisions(post_id, #{cols_to_copy})
|
|
|
|
SELECT #{target_post.id}, #{cols_to_copy}
|
|
|
|
FROM post_revisions
|
|
|
|
WHERE post_id = #{original_post.id}
|
|
|
|
SQL
|
|
|
|
end
|
2013-12-12 10:41:34 +08:00
|
|
|
end
|
2014-02-07 08:07:36 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: post_revisions
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer
|
|
|
|
# post_id :integer
|
|
|
|
# modifications :text
|
|
|
|
# number :integer
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-11-20 11:53:15 +08:00
|
|
|
# hidden :boolean default(FALSE), not null
|
2014-02-07 08:07:36 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_post_revisions_on_post_id (post_id)
|
|
|
|
# index_post_revisions_on_post_id_and_number (post_id,number)
|
2021-08-02 22:15:53 +08:00
|
|
|
# index_post_revisions_on_user_id (user_id)
|