diff options
author | James Tucker <jftucker@gmail.com> | 2022-11-09 20:49:30 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 05:49:30 +0100 |
commit | 78a6b871fe3dae308380ea88132ddadc86a1431e (patch) | |
tree | 355c20f14f76d034c9e2b9e90d2e9c86745d4b87 /app/models/custom_emoji.rb | |
parent | 0cd0786aef140ea41aa229cd52ac67867259a3f5 (diff) |
Improve performance by avoiding regex construction (#20215)
```ruby 10.times { p /#{FOO}/.object_id } 10.times { p FOO_RE.object_id } ```
Diffstat (limited to 'app/models/custom_emoji.rb')
-rw-r--r-- | app/models/custom_emoji.rb | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index 077ce559a..7b19cd2ac 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -30,6 +30,7 @@ class CustomEmoji < ApplicationRecord SCAN_RE = /(?<=[^[:alnum:]:]|\n|^) :(#{SHORTCODE_RE_FRAGMENT}): (?=[^[:alnum:]:]|$)/x + SHORTCODE_ONLY_RE = /\A#{SHORTCODE_RE_FRAGMENT}\z/ IMAGE_MIME_TYPES = %w(image/png image/gif image/webp).freeze @@ -41,7 +42,7 @@ class CustomEmoji < ApplicationRecord before_validation :downcase_domain validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true, size: { less_than: LIMIT } - validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 } + validates :shortcode, uniqueness: { scope: :domain }, format: { with: SHORTCODE_ONLY_RE }, length: { minimum: 2 } scope :local, -> { where(domain: nil) } scope :remote, -> { where.not(domain: nil) } |