chore: Use "create!" and "save!" bang methods when not checking the result (#5358)

* Use "create!" when not checking for errors on the result
* Use "save!" when not checking the result
This commit is contained in:
Jordan Brough 2022-09-13 08:10:06 -04:00 committed by GitHub
parent 44f498be6d
commit 59b31615ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 47 additions and 47 deletions

View file

@ -25,7 +25,7 @@ class NotificationSubscriptionBuilder
end end
def build_identifier_subscription def build_identifier_subscription
@identifier_subscription = user.notification_subscriptions.create(params.merge(identifier: identifier)) @identifier_subscription = user.notification_subscriptions.create!(params.merge(identifier: identifier))
end end
def update_identifier_subscription def update_identifier_subscription

View file

@ -49,7 +49,7 @@ class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseCont
def clone def clone
automation_rule = Current.account.automation_rules.find_by(id: params[:automation_rule_id]) automation_rule = Current.account.automation_rules.find_by(id: params[:automation_rule_id])
new_rule = automation_rule.dup new_rule = automation_rule.dup
new_rule.save new_rule.save!
@automation_rule = new_rule @automation_rule = new_rule
end end

View file

@ -44,7 +44,7 @@ class Api::V1::Accounts::Channels::TwilioChannelsController < Api::V1::Accounts:
phone_number: phone_number, phone_number: phone_number,
medium: medium medium: medium
) )
@inbox = Current.account.inboxes.create( @inbox = Current.account.inboxes.create!(
name: permitted_params[:name], name: permitted_params[:name],
channel: @twilio_channel channel: @twilio_channel
) )

View file

@ -135,7 +135,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
inbox = Current.account.inboxes.find(params[:inbox_id]) inbox = Current.account.inboxes.find(params[:inbox_id])
source_id = params[:source_id] || SecureRandom.uuid source_id = params[:source_id] || SecureRandom.uuid
ContactInbox.create(contact: @contact, inbox: inbox, source_id: source_id) ContactInbox.create!(contact: @contact, inbox: inbox, source_id: source_id)
end end
def permitted_params def permitted_params

View file

@ -9,7 +9,7 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
process_update_contact process_update_contact
@conversation = create_conversation @conversation = create_conversation
conversation.messages.create(message_params) conversation.messages.create!(message_params)
end end
end end
@ -59,7 +59,7 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController
unless conversation.resolved? unless conversation.resolved?
conversation.status = :resolved conversation.status = :resolved
conversation.save conversation.save!
end end
head :ok head :ok
end end

View file

@ -23,7 +23,7 @@ class DeviseOverrides::SessionsController < ::DeviseTokenAuth::SessionsControlle
def authenticate_resource_with_sso_token def authenticate_resource_with_sso_token
@token = @resource.create_token @token = @resource.create_token
@resource.save @resource.save!
sign_in(:user, @resource, store: false, bypass: false) sign_in(:user, @resource, store: false, bypass: false)
# invalidate the token after the user is signed in # invalidate the token after the user is signed in

View file

@ -44,12 +44,12 @@ class Twitter::CallbacksController < Twitter::BaseController
end end
def create_inbox def create_inbox
twitter_profile = account.twitter_profiles.create( twitter_profile = account.twitter_profiles.create!(
twitter_access_token: parsed_body['oauth_token'], twitter_access_token: parsed_body['oauth_token'],
twitter_access_token_secret: parsed_body['oauth_token_secret'], twitter_access_token_secret: parsed_body['oauth_token_secret'],
profile_id: parsed_body['user_id'] profile_id: parsed_body['user_id']
) )
account.inboxes.create( account.inboxes.create!(
name: parsed_body['screen_name'], name: parsed_body['screen_name'],
channel: twitter_profile channel: twitter_profile
) )

View file

@ -2,6 +2,6 @@ class Conversations::ActivityMessageJob < ApplicationJob
queue_as :default queue_as :default
def perform(conversation, message_params) def perform(conversation, message_params)
conversation.messages.create(message_params) conversation.messages.create!(message_params)
end end
end end

View file

@ -10,7 +10,7 @@ class Conversations::UserMentionJob < ApplicationJob
) )
if mention.nil? if mention.nil?
Mention.create( Mention.create!(
user_id: mentioned_user_id, user_id: mentioned_user_id,
conversation_id: conversation_id, conversation_id: conversation_id,
mentioned_at: Time.zone.now, mentioned_at: Time.zone.now,

View file

@ -16,7 +16,7 @@ class ReportingEventListener < BaseListener
event_start_time: conversation.created_at, event_start_time: conversation.created_at,
event_end_time: conversation.updated_at event_end_time: conversation.updated_at
) )
reporting_event.save reporting_event.save!
end end
def first_reply_created(event) def first_reply_created(event)
@ -39,6 +39,6 @@ class ReportingEventListener < BaseListener
# rubocop:disable Rails/SkipsModelValidations # rubocop:disable Rails/SkipsModelValidations
conversation.update_columns(first_reply_created_at: message.created_at) conversation.update_columns(first_reply_created_at: message.created_at)
# rubocop:enable Rails/SkipsModelValidations # rubocop:enable Rails/SkipsModelValidations
reporting_event.save reporting_event.save!
end end
end end

View file

@ -4,7 +4,7 @@ module MailboxHelper
def create_message def create_message
return if @conversation.messages.find_by(source_id: processed_mail.message_id).present? return if @conversation.messages.find_by(source_id: processed_mail.message_id).present?
@message = @conversation.messages.create( @message = @conversation.messages.create!(
account_id: @conversation.account_id, account_id: @conversation.account_id,
sender: @conversation.contact, sender: @conversation.contact,
content: mail_content&.truncate(150_000), content: mail_content&.truncate(150_000),

View file

@ -39,7 +39,7 @@ class Channel::FacebookPage < ApplicationRecord
def create_contact_inbox(instagram_id, name) def create_contact_inbox(instagram_id, name)
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
contact = inbox.account.contacts.create!(name: name) contact = inbox.account.contacts.create!(name: name)
::ContactInbox.create( ::ContactInbox.create!(
contact_id: contact.id, contact_id: contact.id,
inbox_id: inbox.id, inbox_id: inbox.id,
source_id: instagram_id source_id: instagram_id

View file

@ -30,7 +30,7 @@ class Line::IncomingMessageService
def message_created?(event) def message_created?(event)
return unless event_type_message?(event) return unless event_type_message?(event)
@message = @conversation.messages.create( @message = @conversation.messages.create!(
content: event['message']['text'], content: event['message']['text'],
account_id: @inbox.account_id, account_id: @inbox.account_id,
inbox_id: @inbox.id, inbox_id: @inbox.id,

View file

@ -6,7 +6,7 @@ class Sms::IncomingMessageService
def perform def perform
set_contact set_contact
set_conversation set_conversation
@message = @conversation.messages.create( @message = @conversation.messages.create!(
content: params[:text], content: params[:text],
account_id: @inbox.account_id, account_id: @inbox.account_id,
inbox_id: @inbox.id, inbox_id: @inbox.id,

View file

@ -12,7 +12,7 @@ class Telegram::IncomingMessageService
set_contact set_contact
update_contact_avatar update_contact_avatar
set_conversation set_conversation
@message = @conversation.messages.create( @message = @conversation.messages.create!(
content: params[:message][:text].presence || params[:message][:caption], content: params[:message][:text].presence || params[:message][:caption],
account_id: @inbox.account_id, account_id: @inbox.account_id,
inbox_id: @inbox.id, inbox_id: @inbox.id,

View file

@ -8,7 +8,7 @@ class Twilio::IncomingMessageService
set_contact set_contact
set_conversation set_conversation
@message = @conversation.messages.create( @message = @conversation.messages.create!(
content: params[:Body], content: params[:Body],
account_id: @inbox.account_id, account_id: @inbox.account_id,
inbox_id: @inbox.id, inbox_id: @inbox.id,

View file

@ -7,7 +7,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
set_inbox set_inbox
ensure_contacts ensure_contacts
set_conversation set_conversation
@message = @conversation.messages.create( @message = @conversation.messages.create!(
content: message_create_data['message_data']['text'], content: message_create_data['message_data']['text'],
account_id: @inbox.account_id, account_id: @inbox.account_id,
inbox_id: @inbox.id, inbox_id: @inbox.id,
@ -30,7 +30,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
def save_media_urls(file) def save_media_urls(file)
@message.content_attributes[:media_url] = file['media_url'] @message.content_attributes[:media_url] = file['media_url']
@message.content_attributes[:display_url] = file['display_url'] @message.content_attributes[:display_url] = file['display_url']
@message.save @message.save!
end end
def direct_message_events_params def direct_message_events_params
@ -121,6 +121,6 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
content_type: media['type'] content_type: media['type']
} }
) )
@message.save @message.save!
end end
end end

View file

@ -80,7 +80,7 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
def create_message def create_message
find_or_create_contact(user) find_or_create_contact(user)
set_conversation set_conversation
@conversation.messages.create( @conversation.messages.create!(
account_id: @inbox.account_id, account_id: @inbox.account_id,
sender: @contact, sender: @contact,
content: tweet_text, content: tweet_text,

View file

@ -7,7 +7,7 @@ class ChatwootHub
def self.installation_identifier def self.installation_identifier
identifier = InstallationConfig.find_by(name: 'INSTALLATION_IDENTIFIER')&.value identifier = InstallationConfig.find_by(name: 'INSTALLATION_IDENTIFIER')&.value
identifier ||= InstallationConfig.create(name: 'INSTALLATION_IDENTIFIER', value: SecureRandom.uuid).value identifier ||= InstallationConfig.create!(name: 'INSTALLATION_IDENTIFIER', value: SecureRandom.uuid).value
identifier identifier
end end

View file

@ -84,7 +84,7 @@ class Integrations::Csml::ProcessorService < Integrations::BotProcessorService
end end
def process_text_messages(message_payload, conversation) def process_text_messages(message_payload, conversation)
conversation.messages.create( conversation.messages.create!(
{ {
message_type: :outgoing, message_type: :outgoing,
account_id: conversation.account_id, account_id: conversation.account_id,
@ -99,7 +99,7 @@ class Integrations::Csml::ProcessorService < Integrations::BotProcessorService
buttons = message_payload['content']['buttons'].map do |button| buttons = message_payload['content']['buttons'].map do |button|
{ title: button['content']['title'], value: button['content']['payload'] } { title: button['content']['title'], value: button['content']['payload'] }
end end
conversation.messages.create( conversation.messages.create!(
{ {
message_type: :outgoing, message_type: :outgoing,
account_id: conversation.account_id, account_id: conversation.account_id,

View file

@ -43,10 +43,10 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer
return if content_params.blank? return if content_params.blank?
conversation = message.conversation conversation = message.conversation
conversation.messages.create(content_params.merge({ conversation.messages.create!(content_params.merge({
message_type: :outgoing, message_type: :outgoing,
account_id: conversation.account_id, account_id: conversation.account_id,
inbox_id: conversation.inbox_id inbox_id: conversation.inbox_id
})) }))
end end
end end

View file

@ -83,7 +83,7 @@ class Integrations::Slack::IncomingMessageBuilder
def create_message def create_message
return unless conversation return unless conversation
@message = conversation.messages.create( @message = conversation.messages.create!(
message_type: :outgoing, message_type: :outgoing,
account_id: conversation.account_id, account_id: conversation.account_id,
inbox_id: conversation.inbox_id, inbox_id: conversation.inbox_id,

View file

@ -26,7 +26,7 @@ RSpec.describe '/api/v1/widget/labels', type: :request do
context 'with correct website token and a defined label' do context 'with correct website token and a defined label' do
before do before do
account.labels.create(title: 'customer-support') account.labels.create!(title: 'customer-support')
end end
it 'add the label to the conversation' do it 'add the label to the conversation' do

View file

@ -43,7 +43,7 @@ describe AutomationRuleListener do
]) ])
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
automation_rule.files.attach(file) automation_rule.files.attach(file)
automation_rule.save automation_rule.save!
end end
describe '#conversation_updated with contacts attributes' do describe '#conversation_updated with contacts attributes' do

View file

@ -19,7 +19,7 @@ RSpec.describe ReplyMailbox, type: :mailbox do
before do before do
# this UUID is hardcoded in the reply.eml, that's why we are updating this # this UUID is hardcoded in the reply.eml, that's why we are updating this
conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489' conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489'
conversation.save conversation.save!
described_subject described_subject
end end
@ -129,7 +129,7 @@ RSpec.describe ReplyMailbox, type: :mailbox do
before do before do
# this UUID is hardcoded in the reply.eml, that's why we are updating this # this UUID is hardcoded in the reply.eml, that's why we are updating this
conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489' conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489'
conversation.save conversation.save!
end end
it 'add the mail content as new message on the conversation' do it 'add the mail content as new message on the conversation' do

View file

@ -32,7 +32,7 @@ RSpec.describe SupportMailbox, type: :mailbox do
before do before do
# this email is hardcoded in the support.eml, that's why we are updating this # this email is hardcoded in the support.eml, that's why we are updating this
channel_email.email = 'care@example.com' channel_email.email = 'care@example.com'
channel_email.save channel_email.save!
end end
describe 'covers email address format' do describe 'covers email address format' do
@ -121,7 +121,7 @@ RSpec.describe SupportMailbox, type: :mailbox do
before do before do
# this email is hardcoded eml fixture file that's why we are updating this # this email is hardcoded eml fixture file that's why we are updating this
channel_email.email = 'support@chatwoot.com' channel_email.email = 'support@chatwoot.com'
channel_email.save channel_email.save!
end end
it 'create new contact with original sender' do it 'create new contact with original sender' do

View file

@ -54,15 +54,15 @@ RSpec.describe ConversationReplyMailer, type: :mailer do
it 'renders the subject in conversation as reply' do it 'renders the subject in conversation as reply' do
conversation.additional_attributes = { 'mail_subject': 'Mail Subject' } conversation.additional_attributes = { 'mail_subject': 'Mail Subject' }
conversation.save conversation.save!
new_message.save new_message.save!
expect(mail.subject).to eq('Re: Mail Subject') expect(mail.subject).to eq('Re: Mail Subject')
end end
it 'not have private notes' do it 'not have private notes' do
# make the message private # make the message private
private_message.private = true private_message.private = true
private_message.save private_message.save!
expect(mail.body.decoded).not_to include(private_message.content) expect(mail.body.decoded).not_to include(private_message.content)
expect(mail.body.decoded).to include(message.content) expect(mail.body.decoded).to include(message.content)
@ -104,7 +104,7 @@ RSpec.describe ConversationReplyMailer, type: :mailer do
let(:mail) { described_class.reply_without_summary(message_2.conversation, message_2.id).deliver_now } let(:mail) { described_class.reply_without_summary(message_2.conversation, message_2.id).deliver_now }
before do before do
message_2.save message_2.save!
end end
it 'renders the default subject' do it 'renders the default subject' do
@ -113,14 +113,14 @@ RSpec.describe ConversationReplyMailer, type: :mailer do
it 'renders the subject in conversation' do it 'renders the subject in conversation' do
conversation.additional_attributes = { 'mail_subject': 'Mail Subject' } conversation.additional_attributes = { 'mail_subject': 'Mail Subject' }
conversation.save conversation.save!
expect(mail.subject).to eq('Mail Subject') expect(mail.subject).to eq('Mail Subject')
end end
it 'not have private notes' do it 'not have private notes' do
# make the message private # make the message private
private_message.private = true private_message.private = true
private_message.save private_message.save!
expect(mail.body.decoded).not_to include(private_message.content) expect(mail.body.decoded).not_to include(private_message.content)
end end

View file

@ -15,7 +15,7 @@ RSpec.describe Campaign, type: :model do
let(:campaign) { build(:campaign, inbox: website_inbox, display_id: nil, trigger_rules: { url: 'https://test.com' }) } let(:campaign) { build(:campaign, inbox: website_inbox, display_id: nil, trigger_rules: { url: 'https://test.com' }) }
before do before do
campaign.save campaign.save!
campaign.reload campaign.reload
end end

View file

@ -19,7 +19,7 @@ RSpec.describe Conversation, type: :model do
let(:conversation) { build(:conversation, display_id: nil) } let(:conversation) { build(:conversation, display_id: nil) }
before do before do
conversation.save conversation.save!
conversation.reload conversation.reload
end end

View file

@ -37,7 +37,7 @@ RSpec.describe ConversationReplyEmailWorker, type: :worker do
end end
it 'calls ConversationSummaryMailer#reply_without_summary when last incoming message was from email' do it 'calls ConversationSummaryMailer#reply_without_summary when last incoming message was from email' do
message.save message.save!
described_class.new.perform(1, message.id) described_class.new.perform(1, message.id)
expect(mailer).to have_received(:reply_without_summary) expect(mailer).to have_received(:reply_without_summary)
end end