2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-12 03:41:34 +01:00
|
|
|
class PostRevision < ActiveRecord::Base
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :user
|
|
|
|
|
2024-07-11 17:32:33 +02:00
|
|
|
serialize :modifications, type: Hash, coder: YAML
|
2014-03-07 18:59:47 +11:00
|
|
|
|
2016-12-22 16:46:22 +11:00
|
|
|
after_create :create_notification
|
|
|
|
|
2014-10-15 21:09:08 +02:00
|
|
|
def self.ensure_consistency!
|
|
|
|
# 1 - fix the numbers
|
2018-06-19 16:13:14 +10:00
|
|
|
DB.exec <<-SQL
|
2014-10-15 21:09:08 +02:00
|
|
|
UPDATE post_revisions
|
|
|
|
SET number = pr.rank
|
2014-10-27 22:06:43 +01: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-15 21:09:08 +02:00
|
|
|
WHERE post_revisions.id = pr.id
|
|
|
|
AND post_revisions.number <> pr.rank
|
|
|
|
SQL
|
|
|
|
|
|
|
|
# 2 - fix the versions on the posts
|
2018-06-19 16:13:14 +10:00
|
|
|
DB.exec <<-SQL
|
2014-10-15 21:09:08 +02:00
|
|
|
UPDATE posts
|
2014-10-27 22:06:43 +01: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-15 21:09:08 +02:00
|
|
|
SQL
|
2014-03-07 18:59:47 +11:00
|
|
|
end
|
|
|
|
|
2024-04-08 11:33:33 +03:00
|
|
|
def categories
|
|
|
|
return [] if modifications["category_id"].blank?
|
|
|
|
|
2024-04-10 17:35:42 +03:00
|
|
|
@categories ||= Category.with_parents(modifications["category_id"])
|
2024-04-08 11:33:33 +03:00
|
|
|
end
|
|
|
|
|
2014-10-13 01:18:49 -07:00
|
|
|
def hide!
|
2014-10-27 22:06:43 +01:00
|
|
|
update_column(:hidden, true)
|
2014-10-13 01:18:49 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def show!
|
2014-10-27 22:06:43 +01:00
|
|
|
update_column(:hidden, false)
|
2014-10-13 01:18:49 -07:00
|
|
|
end
|
|
|
|
|
2016-12-22 16:46:22 +11:00
|
|
|
def create_notification
|
|
|
|
PostActionNotifier.after_create_post_revision(self)
|
|
|
|
end
|
2023-08-03 22:04:35 -03: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 03:41:34 +01:00
|
|
|
end
|
2014-02-07 11:07:36 +11: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 15:19:25 +10:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-11-20 14:53:15 +11:00
|
|
|
# hidden :boolean default(FALSE), not null
|
2014-02-07 11:07:36 +11:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_post_revisions_on_post_id (post_id)
|
|
|
|
# index_post_revisions_on_post_id_and_number (post_id,number)
|
2021-08-02 10:15:53 -04:00
|
|
|
# index_post_revisions_on_user_id (user_id)
|