diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index bd22e8f..d352acf 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -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') diff --git a/app/models/contact.rb b/app/models/contact.rb index b36d8d3..d58a3d2 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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 diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index 9a928ff..9fe91d0 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -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) diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 7d9f241..214a253 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -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