Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,67 +62,67 @@

CHALLENGE_TYPE_OPTIONS = [
["", ""],
[ts("Gift Exchange"), "GiftExchange"],
[ts("Prompt Meme"), "PromptMeme"]
[human_attribute_name("gift_exchange"), "GiftExchange"],
[human_attribute_name("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?)
errors.add(:base, :it_has_no_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?)
errors.add(:base, :subcollection_cant_be_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)
errors.add(:base, :parent_doesnt_exist, 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."))
errors.add(:base, :cant_be_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))
errors.add(:base, :not_a_maintainer_of_parent, 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, presence: { message: :no_name_entered }
validates :name, uniqueness: { message: :taken }

Check warning on line 99 in app/models/collection.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Uniqueness validation should have a unique index on the database column. Raw Output: app/models/collection.rb:99:3: C: Rails/UniqueValidationWithoutIndex: Uniqueness validation should have a unique index on the database column.
validates :name,
length: { minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN) }
too_short: :too_short, 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) }
too_long: :too_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."),
format: { message: :characters_used,
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) }
too_long: :too_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) }
too_long: :too_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, presence: { message: :no_title_entered }
validates :title,
length: { minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN) }
too_short: :too_short, 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) }
too_long: :too_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
errors.add(:title, :comma_used) if
title.match(/,/)
end

Expand All @@ -134,14 +134,14 @@
validates :description,
length: { allow_blank: true,
maximum: ArchiveConfig.SUMMARY_MAX,
too_long: ts("must be less than %{max} characters long.", max: ArchiveConfig.SUMMARY_MAX) }
too_long: :too_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, url_format: { allow_blank: true, message: :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." }
message: :too_many_tags, max: ArchiveConfig.COLLECTION_TAGS_MAX }

scope :top_level, -> { where(parent_id: nil) }
scope :closed, -> { joins(:collection_preference).where(collection_preferences: { closed: true }) }
Expand Down
36 changes: 36 additions & 0 deletions config/locales/models/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ en:
base: 'Invalid creator:'
pseud_id: Pseud
collection:
description: Description
gift_exchange: Gift Exchange
header_image_url: Header image URL
icon_alt_text: Icon alt text
icon_comment_text: Icon comment text
name: Name
prompt_meme: Prompt Meme
title: Title
creatorships:
base: 'Invalid creator:'
pseud_id: Pseud
Expand Down Expand Up @@ -120,8 +127,34 @@ en:
format: "%{message}"
collection:
attributes:
base:
cant_be_its_own_parent: You can't make a collection its own parent.
it_has_no_owners: Collection has no valid owners.
not_a_maintainer_of_parent: You have to be a maintainer of %{name} to make a subcollection.
parent_doesnt_exist: We couldn't find a collection with name %{name}.
subcollection_cant_be_parent_collection: Sorry, but %{name} is a subcollection, so it can't also be a parent collection.
description:
too_long: must be less than %{max} characters long.
header_image_url:
file_format: can only point to a gif, jpg, jpeg, or png file.
valid_url: is not a valid URL.
icon_alt_text:
too_long: must be less than %{max} characters long.
icon_comment_text:
too_long: must be less than %{max} characters long.
name:
characters_used: must begin and end with a letter or number; it may also contain underscores. It may not contain any other characters, including spaces.
no_name_entered: "^Please enter a name for your collection."
taken: Sorry, that name is already taken. Try again, please!
too_long: must be less than %{max} characters long.
too_short: must be at least %{min} characters long.
tags_after_saving:
too_many_tags: "^Sorry, a collection can only have %{max} tags."
title:
comma_used: "^Sorry, the ',' character cannot be in a collection Display Title."
no_title_entered: "^Please enter a title to be displayed for your collection."
too_long: must be less than %{max} characters long.
too_short: must be at least %{min} characters long.
collection_item:
attributes:
collection:
Expand Down Expand Up @@ -310,6 +343,9 @@ en:
character:
one: Character
other: Characters
collection:
one: Collection
other: Collections
comment: Comment
fandom:
one: Fandom
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddUniqueIndexToCollectionNames < ActiveRecord::Migration[8.1]
uses_departure! if Rails.env.staging? || Rails.env.production?

def change
remove_index :collections, :name
add_index :collections, :name, unique: true
end
end
Loading
Loading