about summary refs log tree commit diff
path: root/app/models/featured_tag.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-11-04 16:08:29 +0100
committerGitHub <noreply@github.com>2022-11-04 16:08:29 +0100
commitb1a219552e935d0c988e1f20b9fdceeb042d2c2d (patch)
treee35bd3b0d2f83cabdb5d5a615460fc081aef07cf /app/models/featured_tag.rb
parentf002878c95442bae71e64d45d2502e63efb4d468 (diff)
Fix featured tags not saving preferred casing (#19732)
Diffstat (limited to 'app/models/featured_tag.rb')
-rw-r--r--app/models/featured_tag.rb25
1 files changed, 10 insertions, 15 deletions
diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb
index d4ed74302..78185b2a9 100644
--- a/app/models/featured_tag.rb
+++ b/app/models/featured_tag.rb
@@ -10,13 +10,16 @@
 #  last_status_at :datetime
 #  created_at     :datetime         not null
 #  updated_at     :datetime         not null
+#  name           :string
 #
 
 class FeaturedTag < ApplicationRecord
   belongs_to :account, inverse_of: :featured_tags
   belongs_to :tag, inverse_of: :featured_tags, optional: true # Set after validation
 
-  validate :validate_tag_name, on: :create
+  validates :name, presence: true, format: { with: /\A(#{Tag::HASHTAG_NAME_RE})\z/i }, on: :create
+
+  validate :validate_tag_uniqueness, on: :create
   validate :validate_featured_tags_limit, on: :create
 
   before_validation :strip_name
@@ -26,18 +29,14 @@ class FeaturedTag < ApplicationRecord
 
   scope :by_name, ->(name) { joins(:tag).where(tag: { name: HashtagNormalizer.new.normalize(name) }) }
 
-  delegate :display_name, to: :tag
-
-  attr_writer :name
-
   LIMIT = 10
 
   def sign?
     true
   end
 
-  def name
-    tag_id.present? ? tag.name : @name
+  def display_name
+    attributes['name'] || tag.display_name
   end
 
   def increment(timestamp)
@@ -51,13 +50,11 @@ class FeaturedTag < ApplicationRecord
   private
 
   def strip_name
-    return unless defined?(@name)
-
-    @name = @name&.strip&.gsub(/\A#/, '')
+    self.name = name&.strip&.gsub(/\A#/, '')
   end
 
   def set_tag
-    self.tag = Tag.find_or_create_by_names(@name)&.first
+    self.tag = Tag.find_or_create_by_names(name)&.first
   end
 
   def reset_data
@@ -69,9 +66,7 @@ class FeaturedTag < ApplicationRecord
     errors.add(:base, I18n.t('featured_tags.errors.limit')) if account.featured_tags.count >= LIMIT
   end
 
-  def validate_tag_name
-    errors.add(:name, :blank) if @name.blank?
-    errors.add(:name, :invalid) unless @name.match?(/\A(#{Tag::HASHTAG_NAME_RE})\z/i)
-    errors.add(:name, :taken) if FeaturedTag.by_name(@name).where(account_id: account_id).exists?
+  def validate_tag_uniqueness
+    errors.add(:name, :taken) if FeaturedTag.by_name(name).where(account_id: account_id).exists?
   end
 end