From 714e3ae5b5c4b6cfb3d06d3e4db96577f181b612 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 24 Oct 2023 06:06:10 -0400 Subject: [PATCH] Use Rails 7.1 `normalizes` feature (#27521) --- app/models/account_alias.rb | 5 +---- app/models/account_migration.rb | 6 ++---- app/models/account_warning.rb | 8 +------- app/models/featured_tag.rb | 6 +----- app/models/relay.rb | 7 ++----- 5 files changed, 7 insertions(+), 25 deletions(-) diff --git a/app/models/account_alias.rb b/app/models/account_alias.rb index f859344fa2..3b75919afe 100644 --- a/app/models/account_alias.rb +++ b/app/models/account_alias.rb @@ -23,10 +23,7 @@ class AccountAlias < ApplicationRecord after_create :add_to_account after_destroy :remove_from_account - def acct=(val) - val = val.to_s.strip - super(val.start_with?('@') ? val[1..] : val) - end + normalizes :acct, with: ->(acct) { acct.strip.delete_prefix('@') } def pretty_acct username, domain = acct.split('@', 2) diff --git a/app/models/account_migration.rb b/app/models/account_migration.rb index b9da596172..dc22e32942 100644 --- a/app/models/account_migration.rb +++ b/app/models/account_migration.rb @@ -25,6 +25,8 @@ class AccountMigration < ApplicationRecord before_validation :set_target_account before_validation :set_followers_count + normalizes :acct, with: ->(acct) { acct.strip.delete_prefix('@') } + validates :acct, presence: true, domain: { acct: true } validate :validate_migration_cooldown validate :validate_target_account @@ -51,10 +53,6 @@ class AccountMigration < ApplicationRecord created_at + COOLDOWN_PERIOD end - def acct=(val) - super(val.to_s.strip.gsub(/\A@/, '')) - end - private def set_target_account diff --git a/app/models/account_warning.rb b/app/models/account_warning.rb index 4f8cc53200..9286577f51 100644 --- a/app/models/account_warning.rb +++ b/app/models/account_warning.rb @@ -27,7 +27,7 @@ class AccountWarning < ApplicationRecord suspend: 4_000, }, _suffix: :action - before_validation :before_validate + normalizes :text, with: ->(text) { text.to_s }, apply_to_nil: true belongs_to :account, inverse_of: :account_warnings belongs_to :target_account, class_name: 'Account', inverse_of: :strikes @@ -50,10 +50,4 @@ class AccountWarning < ApplicationRecord def to_log_human_identifier target_account.acct end - - private - - def before_validate - self.text = '' if text.blank? - end end diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb index df23114a3e..7c36aa8b0b 100644 --- a/app/models/featured_tag.rb +++ b/app/models/featured_tag.rb @@ -23,7 +23,7 @@ class FeaturedTag < ApplicationRecord validate :validate_tag_uniqueness, on: :create validate :validate_featured_tags_limit, on: :create - before_validation :strip_name + normalizes :name, with: ->(name) { name.strip.delete_prefix('#') } before_create :set_tag before_create :reset_data @@ -50,10 +50,6 @@ class FeaturedTag < ApplicationRecord private - def strip_name - self.name = name&.strip&.delete_prefix('#') - end - def set_tag self.tag = Tag.find_or_create_by_names(name)&.first end diff --git a/app/models/relay.rb b/app/models/relay.rb index a5fa03a99c..8d697b891f 100644 --- a/app/models/relay.rb +++ b/app/models/relay.rb @@ -19,7 +19,8 @@ class Relay < ApplicationRecord scope :enabled, -> { accepted } - before_validation :strip_url + normalizes :inbox_url, with: ->(inbox_url) { inbox_url.strip } + before_destroy :ensure_disabled alias enabled? accepted? @@ -76,8 +77,4 @@ class Relay < ApplicationRecord def ensure_disabled disable! if enabled? end - - def strip_url - inbox_url&.strip! - end end