-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathadmin_post.rb
More file actions
171 lines (131 loc) · 4.89 KB
/
Copy pathadmin_post.rb
File metadata and controls
171 lines (131 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class AdminPost < ApplicationRecord
self.per_page = 8 # option for WillPaginate
acts_as_commentable
enum :comment_permissions, {
enable_all: 0,
disable_anon: 1,
disable_all: 2
}, default: :disable_anon, suffix: :comments, validate: { message: :invalid_permissions }
belongs_to :language
belongs_to :translated_post, class_name: "AdminPost"
has_many :translations, class_name: "AdminPost", foreign_key: "translated_post_id", dependent: :destroy
has_many :admin_post_taggings
has_many :tags, through: :admin_post_taggings, source: :admin_post_tag
validates_presence_of :title
validates_length_of :title,
minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN)
validates_length_of :title,
maximum: ArchiveConfig.TITLE_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.TITLE_MAX)
validates_presence_of :content
validates_length_of :content, minimum: ArchiveConfig.CONTENT_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.CONTENT_MIN)
validates_length_of :content, maximum: ArchiveConfig.CONTENT_MAX,
too_long: ts("cannot be more than %{max} characters long.", max: ArchiveConfig.CONTENT_MAX)
validate :translated_post_must_exist
validate :translated_post_language_must_differ
validate :translated_post_must_be_posted_first
scope :non_translated, -> { where("translated_post_id IS NULL") }
scope :for_homepage, -> { posted.order(published_at: :desc).limit(ArchiveConfig.NUMBER_OF_ITEMS_VISIBLE_ON_HOMEPAGE) }
scope :unposted, -> { where(posted: false) }
scope :posted, -> { where(posted: true) }
before_save :inherit_translated_post_comment_permissions, :inherit_translated_post_tags
before_save :set_published_at, if: :posted_changed?
after_destroy :expire_cached_home_admin_posts
after_save :expire_cached_home_admin_posts, :update_translation_comment_permissions, :update_translation_tags
after_save :post_translations, if: :saved_change_to_posted?
# Return the name to link comments to for this object
def commentable_name
self.title
end
def commentable_owners
begin
[Admin.find(self.admin_id)]
rescue
[]
end
end
def draft?
!self.posted?
end
def tag_list
tags.map{ |t| t.name }.join(", ")
end
def tag_list=(list)
return if translated_post_id.present?
self.tags = list.split(",").uniq.collect { |t|
AdminPostTag.fetch(name: t.strip, language_id: self.language_id, post: self)
}.compact
end
def translated_post_must_exist
if translated_post_id.present? && AdminPost.find_by(id: translated_post_id).nil?
errors.add(:translated_post_id, "does not exist")
end
end
def translated_post_language_must_differ
return if translated_post.blank?
return unless translated_post.language == language
errors.add(:translated_post_id, "cannot be same language as original post")
end
def translated_post_must_be_posted_first
return if translated_post.blank?
errors.add(:translated_post_id, :must_be_posted_first) if translated_post.draft? && self.posted?
end
####################
# DELAYED JOBS
####################
include AsyncWithResque
@queue = :utilities
# Turns off comments for all posts that are older than the configured time period.
# If the configured period is nil or less than 1 day, no action is taken.
def self.disable_old_post_comments
return unless ArchiveConfig.ADMIN_POST_COMMENTING_EXPIRATION_DAYS&.positive?
where.not(comment_permissions: :disable_all)
.where(created_at: ..ArchiveConfig.ADMIN_POST_COMMENTING_EXPIRATION_DAYS.days.ago)
.update_all(comment_permissions: :disable_all)
end
private
def expire_cached_home_admin_posts
unless Rails.env.development?
Rails.cache.delete("v1/home/index/home_admin_posts")
end
end
def inherit_translated_post_comment_permissions
return if translated_post.blank?
self.comment_permissions = translated_post.comment_permissions
end
def inherit_translated_post_tags
return if translated_post.blank?
self.tags = translated_post.tags
end
def update_translation_comment_permissions
return if translations.blank?
transaction do
translations.find_each do |post|
post.comment_permissions = self.comment_permissions
post.save
end
end
end
def update_translation_tags
return if translations.blank?
transaction do
translations.find_each do |post|
post.tags = self.tags
post.save
end
end
end
def set_published_at
self.published_at = Time.current if self.posted && !self.published_at
end
def post_translations
return if translations.blank? || !self.posted
transaction do
translations.find_each do |post|
post.update(posted: true, published_at: self.published_at)
end
end
end
end