Extract #all_emoji? to a Rails ext

This commit is contained in:
Stanko K.R.
2025-08-11 17:33:24 +02:00
parent 67c9bdc3d9
commit c95e8678a9
4 changed files with 19 additions and 6 deletions
+2 -3
View File
@@ -1,8 +1,6 @@
class Conversation::Message < ApplicationRecord class Conversation::Message < ApplicationRecord
include Pagination, Broadcastable, ClientIdentifiable, Promptable, Respondable include Pagination, Broadcastable, ClientIdentifiable, Promptable, Respondable
ALL_EMOJI_REGEX = /\A(\p{Emoji_Presentation}|\p{Extended_Pictographic}|\uFE0F)+\z/u
has_rich_text :content has_rich_text :content
belongs_to :conversation, inverse_of: :messages belongs_to :conversation, inverse_of: :messages
@@ -10,12 +8,13 @@ class Conversation::Message < ApplicationRecord
enum :role, %w[ user assistant ].index_by(&:itself) enum :role, %w[ user assistant ].index_by(&:itself)
validates :content, presence: true
validates :client_message_id, presence: true validates :client_message_id, presence: true
scope :ordered, -> { order(created_at: :asc, id: :asc) } scope :ordered, -> { order(created_at: :asc, id: :asc) }
def all_emoji? def all_emoji?
content.to_plain_text.match?(ALL_EMOJI_REGEX) content.to_plain_text.all_emoji?
end end
def to_partial_path def to_partial_path
+1 -3
View File
@@ -6,9 +6,7 @@ class Reaction < ApplicationRecord
after_create :register_card_activity after_create :register_card_activity
def all_emoji? delegate :all_emoji?, to: :content
content.match? /\A(\p{Emoji_Presentation}|\p{Extended_Pictographic}|\uFE0F)+\z/u
end
private private
def register_card_activity def register_card_activity
+5
View File
@@ -0,0 +1,5 @@
class String
def all_emoji?
self.match?(/\A(\p{Emoji_Presentation}|\p{Extended_Pictographic}|\uFE0F)+\z/u)
end
end
+11
View File
@@ -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