-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathcollection.rb
More file actions
executable file
·467 lines (374 loc) · 17.5 KB
/
Copy pathcollection.rb
File metadata and controls
executable file
·467 lines (374 loc) · 17.5 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
class Collection < ApplicationRecord
include Filterable
include WorksOwner
include Searchable
has_one_attached :icon do |attachable|
attachable.variant(:standard, resize_to_limit: [100, 100], loader: { n: -1 })
end
# i18n-tasks-use t("errors.attributes.icon.invalid_format")
# i18n-tasks-use t("errors.attributes.icon.too_large")
validates :icon, attachment: {
allowed_formats: %w[image/gif image/jpeg image/png],
maximum_size: ArchiveConfig.ICON_SIZE_KB_MAX.kilobytes
}
belongs_to :parent, class_name: "Collection", inverse_of: :children
has_many :children, class_name: "Collection", foreign_key: "parent_id", inverse_of: :parent
has_one :collection_profile, dependent: :destroy
accepts_nested_attributes_for :collection_profile
has_one :collection_preference, dependent: :destroy
accepts_nested_attributes_for :collection_preference
before_validation :clear_icon
before_validation :cleanup_url
before_create :ensure_associated
def ensure_associated
self.collection_preference = CollectionPreference.new unless self.collection_preference
self.collection_profile = CollectionProfile.new unless self.collection_profile
end
belongs_to :challenge, dependent: :destroy, polymorphic: true
has_many :prompts, dependent: :destroy
has_many :signups, class_name: "ChallengeSignup", dependent: :destroy
has_many :potential_matches, dependent: :destroy
has_many :assignments, class_name: "ChallengeAssignment", dependent: :destroy
has_many :claims, class_name: "ChallengeClaim", dependent: :destroy
has_many :collection_items, dependent: :destroy
accepts_nested_attributes_for :collection_items, allow_destroy: true
has_many :approved_collection_items, -> { approved_by_both }, class_name: "CollectionItem"
has_many :works, through: :collection_items, source: :item, source_type: "Work"
has_many :approved_works, -> { posted }, through: :approved_collection_items, source: :item, source_type: "Work"
has_many :bookmarks, through: :collection_items, source: :item, source_type: "Bookmark"
has_many :approved_bookmarks, through: :approved_collection_items, source: :item, source_type: "Bookmark"
has_many :collection_participants, dependent: :destroy
accepts_nested_attributes_for :collection_participants, allow_destroy: true
has_many :participants, through: :collection_participants, source: :pseud
has_many :users, through: :participants, source: :user
has_many :invited, -> { where(collection_participants: { participant_role: CollectionParticipant::INVITED }) }, through: :collection_participants, source: :pseud
has_many :owners, -> { where(collection_participants: { participant_role: CollectionParticipant::OWNER }) }, through: :collection_participants, source: :pseud
has_many :moderators, -> { where(collection_participants: { participant_role: CollectionParticipant::MODERATOR }) }, through: :collection_participants, source: :pseud
has_many :members, -> { where(collection_participants: { participant_role: CollectionParticipant::MEMBER }) }, through: :collection_participants, source: :pseud
has_many :posting_participants, -> { where(collection_participants: { participant_role: [CollectionParticipant::MEMBER, CollectionParticipant::MODERATOR, CollectionParticipant::OWNER] }) }, through: :collection_participants, source: :pseud
CHALLENGE_TYPE_OPTIONS = [
["", ""],
[ts("Gift Exchange"), "GiftExchange"],
[ts("Prompt Meme"), "PromptMeme"]
].freeze
validate :must_have_owners
def must_have_owners
# we have to use collection participants because the association may not exist until after
# the collection is saved
errors.add(:base, ts("Collection has no valid owners.")) if (self.collection_participants + (self.parent ? self.parent.collection_participants : [])).select(&:is_owner?)
.empty?
end
validate :collection_depth
def collection_depth
errors.add(:base, ts("Sorry, but %{name} is a subcollection, so it can't also be a parent collection.", name: parent.name)) if self.parent&.parent || (self.parent && !self.children.empty?) || (!self.children.empty? && !self.children.collect(&:children).flatten.empty?)
end
validate :parent_exists
def parent_exists
errors.add(:base, ts("We couldn't find a collection with name %{name}.", name: parent_name)) unless parent_name.blank? || Collection.find_by(name: parent_name)
end
validate :parent_is_allowed
def parent_is_allowed
if parent
if parent == self
errors.add(:base, ts("You can't make a collection its own parent."))
elsif parent_id_changed? && !parent.user_is_maintainer?(User.current_user)
errors.add(:base, ts("You have to be a maintainer of %{name} to make a subcollection.", name: parent.name))
end
end
end
validates :name, presence: { message: ts("Please enter a name for your collection.") }
validates :name, uniqueness: { message: ts("Sorry, that name is already taken. Try again, please!") }
validates :name,
length: { minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN) }
validates :name,
length: { maximum: ArchiveConfig.TITLE_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.TITLE_MAX) }
validates :name,
format: { message: ts("must begin and end with a letter or number; it may also contain underscores. It may not contain any other characters, including spaces."),
with: /\A[A-Za-z0-9]\w*[A-Za-z0-9]\Z/ }
validates :icon_alt_text, length: { allow_blank: true, maximum: ArchiveConfig.ICON_ALT_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.ICON_ALT_MAX) }
validates :icon_comment_text, length: { allow_blank: true, maximum: ArchiveConfig.ICON_COMMENT_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.ICON_COMMENT_MAX) }
validates :email, email_format: { allow_blank: true }
validates :title, presence: { message: ts("Please enter a title to be displayed for your collection.") }
validates :title,
length: { minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN) }
validates :title,
length: { maximum: ArchiveConfig.TITLE_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.TITLE_MAX) }
validate :no_reserved_strings
def no_reserved_strings
errors.add(:title, ts("^Sorry, the ',' character cannot be in a collection Display Title.")) if
title.match(/,/)
end
# return title.html_safe to overcome escaping done by sanitiser
def title
self[:title].try(:html_safe)
end
validates :description,
length: { allow_blank: true,
maximum: ArchiveConfig.SUMMARY_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.SUMMARY_MAX) }
validates :header_image_url, url_format: { allow_blank: true, message: ts("is not a valid URL.") }
validates :header_image_url, format: { allow_blank: true, with: /\A\S+\.(png|gif|jpe?g)\z/, message: :file_format }
validates :tags_after_saving,
length: { maximum: ArchiveConfig.COLLECTION_TAGS_MAX,
message: "^Sorry, a collection can only have %{count} tags." }
scope :top_level, -> { where(parent_id: nil) }
scope :closed, -> { joins(:collection_preference).where(collection_preferences: { closed: true }) }
scope :not_closed, -> { joins(:collection_preference).where(collection_preferences: { closed: false }) }
scope :moderated, -> { joins(:collection_preference).where(collection_preferences: { moderated: true }) }
scope :unmoderated, -> { joins(:collection_preference).where(collection_preferences: { moderated: false }) }
scope :unrevealed, -> { joins(:collection_preference).where(collection_preferences: { unrevealed: true }) }
scope :anonymous, -> { joins(:collection_preference).where(collection_preferences: { anonymous: true }) }
scope :no_challenge, -> { where(challenge_type: nil) }
scope :gift_exchange, -> { where(challenge_type: "GiftExchange") }
scope :prompt_meme, -> { where(challenge_type: "PromptMeme") }
scope :name_only, -> { select("collections.name") }
scope :by_title, -> { order(:title) }
scope :for_blurb, -> { includes(:parent, :moderators, :children, :collection_preference, owners: [:user]).with_attached_icon }
scope :for_search, -> { includes(:parent, :children, :tags, :challenge, moderators: [user: :pseuds], owners: [user: :pseuds]).with_attached_icon }
def cleanup_url
self.header_image_url = begin
Addressable::URI.heuristic_parse(self.header_image_url) if self.header_image_url
rescue Addressable::URI::InvalidURIError
self.header_image_url
end
end
scope :with_name_like, lambda { |name|
where("collections.name LIKE ?", "%#{name}%")
.limit(10)
}
scope :with_title_like, lambda { |title|
where("collections.title LIKE ?", "%#{title}%")
}
scope :with_item_count, lambda {
select("collections.*, count(distinct collection_items.id) as item_count")
.joins("left join collections child_collections on child_collections.parent_id = collections.id
left join collection_items on ( (collection_items.collection_id = child_collections.id OR collection_items.collection_id = collections.id)
AND collection_items.user_approval_status = 1
AND collection_items.collection_approval_status = 1)")
.group("collections.id")
}
def to_param
name_was
end
# Change membership of collection(s) from a particular pseud to the orphan account
def self.orphan(pseuds, collections, default: true)
pseuds.each do |pseud|
collections.each do |collection|
if pseud && collection && collection.owners.include?(pseud)
orphan_pseud = default ? User.orphan_account.default_pseud : User.orphan_account.pseuds.find_or_create_by(name: pseud.name)
pseud.change_membership(collection, orphan_pseud)
end
end
end
end
## AUTOCOMPLETE
# set up autocomplete and override some methods
include AutocompleteSource
def autocomplete_search_string
"#{name} #{title}"
end
def autocomplete_search_string_before_last_save
"#{name_before_last_save} #{title_before_last_save}"
end
def autocomplete_prefixes
["autocomplete_collection_all",
"autocomplete_collection_#{closed? ? 'closed' : 'open'}"]
end
def autocomplete_score
all_items.approved_by_collection.approved_by_user.count
end
## END AUTOCOMPLETE
def parent_name=(name)
@parent_name = name
self.parent = Collection.find_by(name: name)
end
def parent_name
@parent_name || (self.parent ? self.parent.name : "")
end
def all_owners
(self.owners + (self.parent ? self.parent.owners : [])).uniq
end
def all_moderators
(self.moderators + (self.parent ? self.parent.moderators : [])).uniq
end
def all_members
(self.members + (self.parent ? self.parent.members : [])).uniq
end
def all_posting_participants
(self.posting_participants + (self.parent ? self.parent.posting_participants : [])).uniq
end
def all_participants
(self.participants + (self.parent ? self.parent.participants : [])).uniq
end
def all_items
CollectionItem.where(collection_id: ([self.id] + self.children.pluck(:id)))
end
def maintainers
self.all_owners + self.all_moderators
end
def user_is_owner?(user)
user && user != false && !(user.pseuds & self.all_owners).empty?
end
def user_is_moderator?(user)
user && user != false && !(user.pseuds & self.all_moderators).empty?
end
def user_is_maintainer?(user)
user && user != false && !(user.pseuds & (self.all_moderators + self.all_owners)).empty?
end
def user_is_participant?(user)
user && user != false && !get_participating_pseuds_for_user(user).empty?
end
def user_is_posting_participant?(user)
user && user != false && !(user.pseuds & self.all_posting_participants).empty?
end
def get_participating_pseuds_for_user(user)
(user && user != false) ? user.pseuds & self.all_participants : []
end
def get_participants_for_user(user)
return [] unless user
CollectionParticipant.in_collection(self).for_user(user)
end
def assignment_notification
self.collection_profile.assignment_notification || (parent ? parent.collection_profile.assignment_notification : "")
end
def gift_notification
self.collection_profile.gift_notification || (parent ? parent.collection_profile.gift_notification : "")
end
def moderated?() = self.collection_preference.moderated
def closed?() = self.collection_preference.closed
def unrevealed?() = self.collection_preference.unrevealed
def anonymous?() = self.collection_preference.anonymous
def challenge?() = !self.challenge.nil?
def gift_exchange?
self.challenge_type == "GiftExchange"
end
def prompt_meme?
self.challenge_type == "PromptMeme"
end
def maintainers_list
self.maintainers.collect(&:user).flatten.uniq
end
def collection_email
return self.email if self.email.present?
return parent.email if parent && parent.email.present?
end
def notify_maintainers_assignments_sent
if self.collection_email.present?
UserMailer.assignments_sent_notification(self.id, self.collection_email).deliver_later
else
# if collection email is not set and collection parent email is not set, loop through maintainers and send each a notice via email
self.maintainers_list.each do |user|
I18n.with_locale(user.preference.locale_for_mails) do
UserMailer.assignments_sent_notification(self.id, user.email).deliver_later
end
end
end
end
def notify_maintainers_assignment_default(challenge_assignment)
if self.collection_email.present?
UserMailer.assignment_default_notification(self.id, challenge_assignment.id, self.collection_email).deliver_later
else
# if collection email is not set and collection parent email is not set, loop through maintainers and send each a notice via email
self.maintainers_list.each do |user|
I18n.with_locale(user.preference.locale_for_mails) do
UserMailer.assignment_default_notification(self.id, challenge_assignment.id, user.email).deliver_later
end
end
end
end
include AsyncWithResque
@queue = :collection
def reveal!
async_after_commit(:reveal_collection_items)
end
def reveal_authors!
async_after_commit(:reveal_collection_item_authors)
end
def reveal_collection_items
return if unrevealed?
approved_collection_items.each { |collection_item| collection_item.update_attribute(:unrevealed, false) }
send_reveal_notifications
end
def reveal_collection_item_authors
return if anonymous?
approved_collection_items.each { |collection_item| collection_item.update_attribute(:anonymous, false) }
end
def send_reveal_notifications
approved_collection_items.each(&:notify_of_reveal)
end
# Delete current icon (thus reverting to archive default icon)
def delete_icon=(value)
@delete_icon = !value.to_i.zero?
end
def delete_icon
!!@delete_icon
end
alias delete_icon? delete_icon
def clear_icon
return unless delete_icon?
self.icon.purge
self.icon_alt_text = nil
self.icon_comment_text = nil
end
def self.expire_blurb_cache(id)
# Expire both versions of the blurb, whether the user is logged in or not.
%w[logged-in logged-out].each do |logged_in|
[
"collection-blurb-#{logged_in}-#{id}-v5-header",
"collection-blurb-#{logged_in}-#{id}-v5-summary",
"collection-blurb-#{logged_in}-#{id}-v5-body"
].each do |cache_key|
ActionController::Base.new.expire_fragment(cache_key)
end
end
end
def self.expire_profile_cache(id)
ActionController::Base.new.expire_fragment("collection-profile-#{id}")
end
def self.expire_caches(id)
Collection.expire_blurb_cache(id)
Collection.expire_profile_cache(id)
end
# Blurbs use data from Elasticsearch via CollectionDecorator, so we need to refresh the blurb cache on reindex
def self.successful_reindex(ids)
ids.each { |id| Collection.expire_blurb_cache(id) }
end
# Work and bookmark counts for indexing; these come from the database.
def general_works_count
Work.visible_to_registered_user.in_collection(self).count
end
def public_works_count
Work.visible_to_all.in_collection(self).count
end
def general_bookmarked_items_count
bookmarks = Bookmark.is_public.in_collection(self)
[
Work.visible_to_registered_user,
Series.visible_to_registered_user,
ExternalWork.visible_to_registered_user
].map do |relation|
relation.joins(:bookmarks).merge(bookmarks).distinct.count("bookmarks.bookmarkable_id")
end.sum
end
def public_bookmarked_items_count
bookmarks = Bookmark.is_public.in_collection(self)
[Work.visible_to_all, Series.visible_to_all, ExternalWork.visible_to_all].map do |relation|
relation.joins(:bookmarks).merge(bookmarks).distinct.count("bookmarks.bookmarkable_id")
end.sum
end
# Work and bookmark counts; these come from Elasticsearch via the
# SearchCounts helper. It already checks for visibility, so nothing
# extra needs to be done here.
def approved_works_count
SearchCounts.work_count_for_collection(self)
end
def approved_bookmarked_items_count
SearchCounts.bookmarkable_count_for_collection(self)
end
end