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