mirror of
https://github.com/danbee/danbarberphoto
synced 2025-03-04 08:49:07 +00:00
Refactor contact and contact controller
This commit is contained in:
parent
c9321e8f5b
commit
41de7e736c
@ -6,7 +6,8 @@ class ContactsController < ApplicationController
|
||||
|
||||
def create
|
||||
@contact = Contact.new(params[:contact])
|
||||
if @contact.save
|
||||
if @contact.valid?
|
||||
Notifier.contact_notification(@contact).deliver
|
||||
redirect_to(:new_contact, notice: t('contact.thanks'))
|
||||
else
|
||||
flash[:alert] = t('contact.invalid')
|
||||
|
||||
@ -16,12 +16,4 @@ class Contact
|
||||
def read_attribute_for_validation(key)
|
||||
send(key)
|
||||
end
|
||||
|
||||
def save
|
||||
if self.valid?
|
||||
Notifier.contact_notification(self).deliver
|
||||
return true
|
||||
end
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@ -16,13 +16,18 @@ describe ContactsController, type: :controller do
|
||||
|
||||
describe 'POST create' do
|
||||
it 'saves a new contact' do
|
||||
expect_any_instance_of(Contact).to receive(:save).once.and_return(true)
|
||||
valid_contact = double(valid?: true)
|
||||
allow(Contact).to receive(:new).and_return(valid_contact)
|
||||
allow(Notifier).to receive(:contact_notification).and_return(double(deliver: true))
|
||||
post :create, contact: contact_params
|
||||
|
||||
expect(Notifier).to have_received(:contact_notification).with(valid_contact)
|
||||
expect(flash[:notice]).to eql(I18n.t('contact.thanks'))
|
||||
expect(response).to redirect_to(:new_contact)
|
||||
end
|
||||
|
||||
it 're-renders the form if params are missing' do
|
||||
allow(Contact).to receive(:new).and_return(double(valid?: false))
|
||||
post :create, contact: {}
|
||||
expect(flash[:alert]).to eql(I18n.t('contact.invalid'))
|
||||
expect(response).to render_template(:new)
|
||||
|
||||
@ -9,10 +9,4 @@ describe Contact do
|
||||
it { should_not allow_value('test@test').for(:email) }
|
||||
|
||||
let(:contact) { build(:contact) }
|
||||
|
||||
it 'should send an email' do
|
||||
contact.save
|
||||
expect(ActionMailer::Base.deliveries.last.from).to eql([contact.email])
|
||||
expect(ActionMailer::Base.deliveries.last.to).to eql(['enquiries@danbarberphoto.com'])
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user