Skip to content

Commit

Permalink
internationalize collection.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesium-Ice committed Sep 22, 2024
1 parent c28acc5 commit ec338b0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
52 changes: 26 additions & 26 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,67 +72,67 @@ def clean_up_challenge

CHALLENGE_TYPE_OPTIONS = [
["", ""],
[ts("Gift Exchange"), "GiftExchange"],
[ts("Prompt Meme"), "PromptMeme"]
[I18n.t("collection.gift_exchange"), "GiftExchange"],
[I18n.t("collection.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, I18n.t("collection.validation.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?)
errors.add(:base, I18n.t("collection.validation.collection_depth", 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, I18n.t("collection.validation.collection_not_found", 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
return unless parent

if parent == self
errors.add(:base, I18n.t("collection.validation.invalid_parent"))
elsif parent_id_changed? && !parent.user_is_maintainer?(User.current_user)
errors.add(:base, I18n.t("collection.validation.subcollection", name: parent.name))
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: I18n.t("collection.validation.name.presence") }
validates :name, uniqueness: { message: I18n.t("collection.validation.name.uniqueness") }
validates :name,
length: { minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN) }
too_short: I18n.t("collection.validation.length.min", 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: I18n.t("collection.validation.length.max", 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: I18n.t("collection.validation.name.format"),
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: I18n.t("collection.validation.length.max", 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: I18n.t("collection.validation.length.max", 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: I18n.t("collection.validation.title") }
validates :title,
length: { minimum: ArchiveConfig.TITLE_MIN,
too_short: ts("must be at least %{min} characters long.", min: ArchiveConfig.TITLE_MIN) }
too_short: I18n.t("collection.validation.length.min", 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: I18n.t("collection.validation.length.max", 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, I18n.t("collection.validation.no_reserved_strings")) if
title.match(/,/)
end

Expand All @@ -144,14 +144,14 @@ def title
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: I18n.t("collection.validation.length.max", max: ArchiveConfig.SUMMARY_MAX) }

validates :header_image_url, format: { allow_blank: true, with: URI::DEFAULT_PARSER.make_regexp(%w[http https]), message: I18n.t("collection.header_image.invalid_url") }
validates :header_image_url, format: { allow_blank: true, with: /\A\S+\.(png|gif|jpg)\z/, message: I18n.t("collection.header_image.invalid_file_type"), multiline: true }
validates :header_image_url, format: { allow_blank: true, with: URI::DEFAULT_PARSER.make_regexp(%w[http https]), message: I18n.t("collection.validation.header_image.invalid_url") }
validates :header_image_url, format: { allow_blank: true, with: /\A\S+\.(png|gif|jpg)\z/, message: I18n.t("collection.validation.header_image.invalid_file_type"), multiline: true }

validates :tags_after_saving,
length: { maximum: ArchiveConfig.COLLECTION_TAGS_MAX,
message: "^Sorry, a collection can only have %{count} tags." }
message: I18n.t("collection.validation.num_tags") }

scope :top_level, -> { where(parent_id: nil) }
scope :closed, -> { joins(:collection_preference).where(collection_preferences: { closed: true }) }
Expand Down
24 changes: 21 additions & 3 deletions config/locales/models/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,27 @@ en:
attributes:
ticket_number: Ticket ID
collection:
header_image:
invalid_file_type: can only point to a gif, jpg, or png file.
invalid_url: is not a valid URL.
gift_exchange: Gift Exchange
prompt_meme: Prompt Meme
validation:
collection_depth: Sorry, but %{name} is a subcollection, so it can't also be a parent collection.
collection_not_found: We couldn't find a collection with name %{name}.
header_image:
invalid_file_type: can only point to a gif, jpg, or png file.
invalid_url: is not a valid URL.
invalid_parent: You can't make a collection its own parent.
length:
max: must be less than %{max} characters long.
min: must be at least %{min} characters long.
name:
format: must begin and end with a letter or number; it may also contain underscores. It may not contain any other characters, including spaces.
presence: Please enter a name for your collection.
uniqueness: Sorry, that name is already taken. Try again, please!
no_reserved_strings: "^Sorry, the ',' character cannot be in a collection Display Title."
no_valid_owners: Collection has no valid owners.
num_tags: "^Sorry, a collection can only have %{count} tags."
subcollection: You have to be a maintainer of %{name} to make a subcollection.
title: Please enter a title to be displayed for your collection.
errors:
attributes:
ticket_number:
Expand Down

0 comments on commit ec338b0

Please sign in to comment.