Skip to content

Commit 737e382

Browse files
committed
moved user ping regular expression to a USER_PING_REG_EXP constant to avoid fragmentation
1 parent 7994836 commit 737e382

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def check_if_target_post_locked
457457
# @return [Array<Integer>] list of pinged user ids
458458
def check_for_pings(thread, content)
459459
pingable = thread.pingable
460-
matches = content.scan(/@#(\d+)/)
460+
matches = content.scan(Comment::USER_PING_REG_EXP)
461461
matches.flatten.select { |m| pingable.include?(m.to_i) }.map(&:to_i)
462462
end
463463

app/helpers/comments_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def comment_user_link(comment)
3737
# @param content [String] content to get pinged users from
3838
# @return [Hash{String => User}] list of pinged users
3939
def pinged_users(content)
40-
user_ids = content.scan(/@#(\d+)/).map { |g| g[0].to_i }
40+
user_ids = content.scan(Comment::USER_PING_REG_EXP).map { |g| g[0].to_i }
4141
User.where(id: user_ids).to_a.to_h { |u| [u.id, u] }
4242
end
4343

@@ -49,7 +49,7 @@ def pinged_users(content)
4949
def render_pings(content, pingable: nil, host: nil)
5050
users = pinged_users(content)
5151

52-
content.gsub(/@#(\d+)/) do |ping|
52+
content.gsub(Comment::USER_PING_REG_EXP) do |ping|
5353
user = users[Regexp.last_match(1).to_i]
5454
if user.nil?
5555
ping
@@ -70,7 +70,7 @@ def render_pings(content, pingable: nil, host: nil)
7070
def render_pings_text(content)
7171
users = pinged_users(content)
7272

73-
content.gsub(/@#(\d+)/) do |ping|
73+
content.gsub(Comment::USER_PING_REG_EXP) do |ping|
7474
user = users[Regexp.last_match(1).to_i]
7575
user.nil? ? ping : "@#{rtl_safe_username(user)}"
7676
end

app/models/comment.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class Comment < ApplicationRecord
44
include SoftDeletable
55
include Timestamped
66

7+
USER_PING_REG_EXP = /@#(\d+)/
8+
79
scope :by, ->(user) { where(user: user) }
810

911
belongs_to :user
@@ -41,7 +43,7 @@ def content_length
4143

4244
def pings
4345
pingable = thread.pingable
44-
matches = content.scan(/@#(\d+)/)
46+
matches = content.scan(USER_PING_REG_EXP)
4547
matches.flatten.select { |m| pingable.include?(m.to_i) }.map(&:to_i)
4648
end
4749

0 commit comments

Comments
 (0)