Chatwoot/app/models/concerns/liquidable.rb

37 lines
902 B
Ruby
Raw Normal View History

2022-12-16 12:04:52 +00:00
module Liquidable
extend ActiveSupport::Concern
included do
acts_as_taggable_on :labels
before_create :process_liquid_in_content
end
private
def message_drops
{
'contact' => ContactDrop.new(conversation.contact),
2022-12-16 13:01:24 +00:00
'agent' => UserDrop.new(sender),
2022-12-16 12:04:52 +00:00
'conversation' => ConversationDrop.new(conversation),
'inbox' => InboxDrop.new(inbox)
}
end
def liquid_processable_message?
content.present? && message_type == 'outgoing'
end
def process_liquid_in_content
return unless liquid_processable_message?
2022-12-16 12:59:26 +00:00
template = Liquid::Template.parse(modified_liquid_content)
2022-12-16 12:04:52 +00:00
self.content = template.render(message_drops)
end
2022-12-16 12:59:26 +00:00
def modified_liquid_content
# This regex is used to match the code blocks in the content
# We don't want to process liquid in code blocks
2022-12-23 11:04:06 +00:00
content.gsub(/`(.*?)`/m, '{% raw %}`\\1`{% endraw %}')
2022-12-16 12:59:26 +00:00
end
2022-12-16 12:04:52 +00:00
end