From c95e8678a96de6922b6e82ae7f0522cd123874fd Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Mon, 11 Aug 2025 17:33:24 +0200 Subject: [PATCH] Extract #all_emoji? to a Rails ext --- app/models/conversation/message.rb | 5 ++--- app/models/reaction.rb | 4 +--- lib/rails_ext/string.rb | 5 +++++ test/lib/rails_ext/string_test.rb | 11 +++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 lib/rails_ext/string.rb create mode 100644 test/lib/rails_ext/string_test.rb diff --git a/app/models/conversation/message.rb b/app/models/conversation/message.rb index 26a662e30..dc817c500 100644 --- a/app/models/conversation/message.rb +++ b/app/models/conversation/message.rb @@ -1,8 +1,6 @@ class Conversation::Message < ApplicationRecord include Pagination, Broadcastable, ClientIdentifiable, Promptable, Respondable - ALL_EMOJI_REGEX = /\A(\p{Emoji_Presentation}|\p{Extended_Pictographic}|\uFE0F)+\z/u - has_rich_text :content belongs_to :conversation, inverse_of: :messages @@ -10,12 +8,13 @@ class Conversation::Message < ApplicationRecord enum :role, %w[ user assistant ].index_by(&:itself) + validates :content, presence: true validates :client_message_id, presence: true scope :ordered, -> { order(created_at: :asc, id: :asc) } def all_emoji? - content.to_plain_text.match?(ALL_EMOJI_REGEX) + content.to_plain_text.all_emoji? end def to_partial_path diff --git a/app/models/reaction.rb b/app/models/reaction.rb index 6ee88ca9a..d70f8333b 100644 --- a/app/models/reaction.rb +++ b/app/models/reaction.rb @@ -6,9 +6,7 @@ class Reaction < ApplicationRecord after_create :register_card_activity - def all_emoji? - content.match? /\A(\p{Emoji_Presentation}|\p{Extended_Pictographic}|\uFE0F)+\z/u - end + delegate :all_emoji?, to: :content private def register_card_activity diff --git a/lib/rails_ext/string.rb b/lib/rails_ext/string.rb new file mode 100644 index 000000000..e430126d2 --- /dev/null +++ b/lib/rails_ext/string.rb @@ -0,0 +1,5 @@ +class String + def all_emoji? + self.match?(/\A(\p{Emoji_Presentation}|\p{Extended_Pictographic}|\uFE0F)+\z/u) + end +end diff --git a/test/lib/rails_ext/string_test.rb b/test/lib/rails_ext/string_test.rb new file mode 100644 index 000000000..ab6dd2f83 --- /dev/null +++ b/test/lib/rails_ext/string_test.rb @@ -0,0 +1,11 @@ +require "test_helper" + +class StringTest < ActiveSupport::TestCase + test "#all_emoji?" do + assert "😊".all_emoji? + assert "😊😊😊".all_emoji? + + assert_not "Hello 😊".all_emoji? + assert_not "Hello".all_emoji? + end +end