From 7e8f34e0cc015d3996267c9be081a6c17efe4922 Mon Sep 17 00:00:00 2001 From: Caleb Thompson Date: Wed, 18 Jun 2014 16:16:20 -0500 Subject: [PATCH] Use instances rather than class methods Rather than sending Griddler.configuration.processor_method to Griddler.configuration.processor_class with a Griddler::Email as an argument (i.e., EmailProcessor.process(email)), instantiate Griddler.configuration.processor_class with a Griddler::Email and call Griddler.configuration.processor_method on that object (i.e., EmailProcessor.new(email).process). This encourages the behavior of separating logic into methods within an EmailProcessor, which is better than having a single class method with many responsibilities or passing state around between class methods. --- NEWS.md | 4 ++++ app/controllers/griddler/emails_controller.rb | 12 ++++++++---- spec/controllers/griddler/emails_controller_spec.rb | 12 ++++++------ spec/dummy/app/models/email_processor.rb | 7 ++++--- 4 files changed, 22 insertions(+), 13 deletions(-) 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