diff --git a/NEWS.md b/NEWS.md index ec2b8fb5..24aa3d7b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ ## 1.0.0 +* Use instances of email processor classes rather than calling class methods. + https://github.com/thoughtbot/griddler/pull/153 + + *Caleb Thompson* * Remove configuration of email addresses for to, from, and cc. https://github.com/thoughtbot/griddler/pull/150 diff --git a/app/controllers/griddler/emails_controller.rb b/app/controllers/griddler/emails_controller.rb index 2a0b1d7b..85ebbc11 100644 --- a/app/controllers/griddler/emails_controller.rb +++ b/app/controllers/griddler/emails_controller.rb @@ -9,13 +9,17 @@ def create private + delegate :processor_class, :processor_method, :email_service, to: :griddler_configuration + def normalized_params - Array.wrap(Griddler.configuration.email_service.normalize_params(params)) + Array.wrap(email_service.normalize_params(params)) end def process_email(email) - processor_class = Griddler.configuration.processor_class - processor_method = Griddler.configuration.processor_method - processor_class.public_send(processor_method, email) + processor_class.new(email).public_send(processor_method) + end + + def griddler_configuration + Griddler.configuration end end diff --git a/spec/controllers/griddler/emails_controller_spec.rb b/spec/controllers/griddler/emails_controller_spec.rb index 99b58632..12e48563 100644 --- a/spec/controllers/griddler/emails_controller_spec.rb +++ b/spec/controllers/griddler/emails_controller_spec.rb @@ -24,8 +24,8 @@ end it 'calls process on the custom processor class' do - my_handler = double - my_handler.should_receive(:process) + my_handler = double(process: nil) + my_handler.should_receive(:new).and_return(my_handler) Griddler.configuration.stub(processor_class: my_handler) post :create, email_params @@ -33,12 +33,12 @@ it 'calls the custom processor method on the processor class' do Griddler.configuration.stub(processor_method: :perform) - griddler_email = double - Griddler::Email.should_receive(:new).and_return(griddler_email) + fake_processor = double(perform: nil) - EmailProcessor.should_receive(:perform).with(griddler_email) + EmailProcessor.should_receive(:new).and_return(fake_processor) + fake_processor.should_receive(:perform) - post :create, to: 'tb@example.com' + post :create, email_params end end diff --git a/spec/dummy/app/models/email_processor.rb b/spec/dummy/app/models/email_processor.rb index a5ad0825..76c92222 100644 --- a/spec/dummy/app/models/email_processor.rb +++ b/spec/dummy/app/models/email_processor.rb @@ -1,6 +1,7 @@ class EmailProcessor - cattr_accessor :email - def self.process(email) - @@email = email + def initialize(email) + @email = email end + + def process; end end