Created
April 30, 2016 21:46
-
-
Save nicholasjhenry/304fceba0c44fa3bd86714c2a10f9f37 to your computer and use it in GitHub Desktop.
Another pipeline example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DraftNewLetter | |
def self.build(current_user) | |
@create_letter = CreateLetter.new | |
@authorize = Authorize.new(current_user, @create_letter) | |
@error_handler = ActiveRecordErrorHandler.new(@authorize) | |
@assign_automatic_ccs = AssignAutomaticCCs.new(@create_letter) | |
@transaction = Transaction.new(@assign_automatic_ccs) | |
end | |
end | |
class Transaction | |
def initialize(processor) | |
@next_processor = processor | |
end | |
def call(params) | |
ActiveRecord.transaction do | |
@next_processor.call(params) | |
end | |
end | |
end | |
class ActiveRecordErrorHandler | |
def initialize(processor) | |
@next_processor = processor | |
end | |
def call(params) | |
@next_processor = processor.call(params) | |
rescue ActiveRecord::Invalid => e | |
e.record | |
end | |
end | |
class CreateLetter | |
def initialize(processor) | |
@next_processor = processor | |
end | |
def call(params) | |
letter = Letter.create!(params) | |
@next_processor.call(letter) | |
end | |
end | |
class AssignAutomaticRecipients | |
def initialize(processor) | |
@next_processor = processor | |
end | |
def call(letter) | |
remove_automatic_ccs(letter) | |
add_patient_as_cc(letter) | |
add_doctor_as_cc(letter) | |
@next_processor.call(letter) | |
end | |
private | |
def remove_automatic_ccs(letter) | |
letter.cc_recipients = letter.manual_cc_recipients | |
end | |
def add_patient_as_cc(letter) | |
return if letter.main_recipient.patient? | |
add_source_as_cc(letter.patient) if patient.cc_on_letter?(letter) | |
end | |
def add_doctor_as_cc(letter) | |
return if letter.main_recipient.doctor? | |
add_source_as_cc(letter.patient.doctor) | |
end | |
def add_source_as_cc(source) | |
recipient = letter.cc_recipients.build(source: source) | |
recipient.name = source.full_name | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment