Class: PostRevision

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/post_revision.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.copy(original_post, target_post) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'app/models/post_revision.rb', line 43

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

.ensure_consistency!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/post_revision.rb', line 11

def self.ensure_consistency!
  # 1 - fix the numbers
  DB.exec <<-SQL
    UPDATE post_revisions
       SET number = pr.rank
      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
     WHERE post_revisions.id = pr.id
       AND post_revisions.number <> pr.rank
  SQL

  # 2 - fix the versions on the posts
  DB.exec <<-SQL
    UPDATE posts
       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')
  SQL
end

Instance Method Details

#create_notificationObject



39
40
41
# File 'app/models/post_revision.rb', line 39

def create_notification
  PostActionNotifier.after_create_post_revision(self)
end

#hide!Object



31
32
33
# File 'app/models/post_revision.rb', line 31

def hide!
  update_column(:hidden, true)
end

#show!Object



35
36
37
# File 'app/models/post_revision.rb', line 35

def show!
  update_column(:hidden, false)
end