chore: refactor
This commit is contained in:
parent
c4682eb57c
commit
ea25651bc6
4 changed files with 76 additions and 44 deletions
30
app/models/concerns/liquidable.rb
Normal file
30
app/models/concerns/liquidable.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
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),
|
||||
'agent' => UserDrop.new(Current.user),
|
||||
'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?
|
||||
|
||||
template = Liquid::Template.parse(content)
|
||||
self.content = template.render(message_drops)
|
||||
end
|
||||
end
|
|
@ -32,10 +32,10 @@
|
|||
|
||||
class Message < ApplicationRecord
|
||||
include MessageFilterHelpers
|
||||
include Liquidable
|
||||
NUMBER_OF_PERMITTED_ATTACHMENTS = 15
|
||||
|
||||
before_validation :ensure_content_type
|
||||
before_create :process_liquid_in_content
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
|
@ -148,19 +148,6 @@ class Message < ApplicationRecord
|
|||
data
|
||||
end
|
||||
|
||||
def process_liquid_in_content
|
||||
return if content.blank? || message_type != 'outgoing'
|
||||
|
||||
template = Liquid::Template.parse(content)
|
||||
|
||||
self.content = template.render({
|
||||
'contact' => ContactDrop.new(conversation.contact),
|
||||
'agent' => UserDrop.new(Current.user),
|
||||
'conversation' => ConversationDrop.new(conversation),
|
||||
'inbox' => InboxDrop.new(inbox)
|
||||
})
|
||||
end
|
||||
|
||||
def content
|
||||
# move this to a presenter
|
||||
return self[:content] if !input_csat? || inbox.web_widget?
|
||||
|
|
40
spec/models/concerns/liquidable_shared.rb
Normal file
40
spec/models/concerns/liquidable_shared.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require 'rails_helper'
|
||||
|
||||
shared_examples_for 'liqudable' do
|
||||
context 'when liquid is present in content' do
|
||||
let(:contact) { create(:contact, name: 'john', phone_number: '+912883') }
|
||||
let(:conversation) { create(:conversation, id: 1, contact: contact) }
|
||||
|
||||
context 'when message is incoming' do
|
||||
let(:message) { build(:message, conversation: conversation, message_type: 'incoming') }
|
||||
|
||||
it 'will not process liquid in content' do
|
||||
message.content = 'hey {{contact.name}} how are you?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'hey {{contact.name}} how are you?'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when message is outgoing' do
|
||||
let(:message) { build(:message, conversation: conversation, message_type: 'outgoing') }
|
||||
|
||||
it 'set replaces liquid variables in message' do
|
||||
message.content = 'hey {{contact.name}} how are you?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'hey john how are you?'
|
||||
end
|
||||
|
||||
it 'process liquid operators like default value' do
|
||||
message.content = 'Can we send you an email at {{ contact.email | default: "default" }} ?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'Can we send you an email at default ?'
|
||||
end
|
||||
|
||||
it 'return empty string when value is not available' do
|
||||
message.content = 'Can we send you an email at {{contact.email}}?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'Can we send you an email at ?'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require Rails.root.join 'spec/models/concerns/liquidable_shared.rb'
|
||||
|
||||
RSpec.describe Message, type: :model do
|
||||
context 'with validations' do
|
||||
|
@ -9,6 +10,10 @@ RSpec.describe Message, type: :model do
|
|||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
end
|
||||
|
||||
describe 'concerns' do
|
||||
it_behaves_like 'liqudable'
|
||||
end
|
||||
|
||||
describe '#reopen_conversation' do
|
||||
let(:conversation) { create(:conversation) }
|
||||
let(:message) { build(:message, message_type: :incoming, conversation: conversation) }
|
||||
|
@ -210,34 +215,4 @@ RSpec.describe Message, type: :model do
|
|||
expect(instagram_message.reload.attachments.count).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
context 'when message is created with variables' do
|
||||
let(:contact) { create(:contact, name: 'john', phone_number: '+912883') }
|
||||
let(:conversation) { create(:conversation, id: 1, contact: contact) }
|
||||
let(:message) { build(:message, conversation: conversation, message_type: 'outgoing') }
|
||||
|
||||
it 'set contact name variable in message' do
|
||||
message.content = 'hey {{contact.name}} how are you?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'hey john how are you?'
|
||||
end
|
||||
|
||||
it 'set contact phone number variable in message' do
|
||||
message.content = 'Can we call you at {{contact.phone_number}}?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'Can we call you at +912883?'
|
||||
end
|
||||
|
||||
it 'set contact email variable in message' do
|
||||
message.content = 'Can we send you an email at {{contact.email}}?'
|
||||
message.save!
|
||||
expect(message.content).to eq 'Can we send you an email at ?'
|
||||
end
|
||||
|
||||
it 'set conversation id in message' do
|
||||
message.content = 'We are happy to help you. Your conversation id is {{conversation.id}}'
|
||||
message.save!
|
||||
expect(message.content).to eq 'We are happy to help you. Your conversation id is 1'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue