Fix featured tags not saving preferred casing (#19732)

This commit is contained in:
Eugen Rochko 2022-11-04 16:08:29 +01:00 committed by GitHub
parent f002878c95
commit b1a219552e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 17 deletions

View File

@ -10,13 +10,16 @@
# last_status_at :datetime # last_status_at :datetime
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# name :string
# #
class FeaturedTag < ApplicationRecord class FeaturedTag < ApplicationRecord
belongs_to :account, inverse_of: :featured_tags belongs_to :account, inverse_of: :featured_tags
belongs_to :tag, inverse_of: :featured_tags, optional: true # Set after validation 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 validate :validate_featured_tags_limit, on: :create
before_validation :strip_name before_validation :strip_name
@ -26,18 +29,14 @@ class FeaturedTag < ApplicationRecord
scope :by_name, ->(name) { joins(:tag).where(tag: { name: HashtagNormalizer.new.normalize(name) }) } scope :by_name, ->(name) { joins(:tag).where(tag: { name: HashtagNormalizer.new.normalize(name) }) }
delegate :display_name, to: :tag
attr_writer :name
LIMIT = 10 LIMIT = 10
def sign? def sign?
true true
end end
def name def display_name
tag_id.present? ? tag.name : @name attributes['name'] || tag.display_name
end end
def increment(timestamp) def increment(timestamp)
@ -51,13 +50,11 @@ class FeaturedTag < ApplicationRecord
private private
def strip_name def strip_name
return unless defined?(@name) self.name = name&.strip&.gsub(/\A#/, '')
@name = @name&.strip&.gsub(/\A#/, '')
end end
def set_tag def set_tag
self.tag = Tag.find_or_create_by_names(@name)&.first self.tag = Tag.find_or_create_by_names(name)&.first
end end
def reset_data 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 errors.add(:base, I18n.t('featured_tags.errors.limit')) if account.featured_tags.count >= LIMIT
end end
def validate_tag_name def validate_tag_uniqueness
errors.add(:name, :blank) if @name.blank? errors.add(:name, :taken) if FeaturedTag.by_name(name).where(account_id: account_id).exists?
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?
end end
end end

View File

@ -67,7 +67,7 @@ en:
domain: This can be the domain name that shows up in the e-mail address or the MX record it uses. They will be checked upon sign-up. domain: This can be the domain name that shows up in the e-mail address or the MX record it uses. They will be checked upon sign-up.
with_dns_records: An attempt to resolve the given domain's DNS records will be made and the results will also be blocked with_dns_records: An attempt to resolve the given domain's DNS records will be made and the results will also be blocked
featured_tag: featured_tag:
name: 'You might want to use one of these:' name: 'Here are some of the hashtags you used the most recently:'
filters: filters:
action: Chose which action to perform when a post matches the filter action: Chose which action to perform when a post matches the filter
actions: actions:

View File

@ -0,0 +1,5 @@
class AddNameToFeaturedTags < ActiveRecord::Migration[6.1]
def change
add_column :featured_tags, :name, :string
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_11_01_190723) do ActiveRecord::Schema.define(version: 2022_11_04_133904) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -442,6 +442,7 @@ ActiveRecord::Schema.define(version: 2022_11_01_190723) do
t.datetime "last_status_at" t.datetime "last_status_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "name"
t.index ["account_id", "tag_id"], name: "index_featured_tags_on_account_id_and_tag_id", unique: true t.index ["account_id", "tag_id"], name: "index_featured_tags_on_account_id_and_tag_id", unique: true
t.index ["tag_id"], name: "index_featured_tags_on_tag_id" t.index ["tag_id"], name: "index_featured_tags_on_tag_id"
end end