Created
February 24, 2014 21:34
-
-
Save jamie/9197650 to your computer and use it in GitHub Desktop.
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 SurveyInviter | |
def initialize(attributes = {}) | |
@survey_manager = SurveyManager.new( | |
Survey.new(attributes[:survey]), | |
Sender.new(attributes[:sender]) | |
) | |
@message_manager = MessageManager.new( | |
Message.new(attributes[:message]), | |
Recipients.new(attributes[:recipients]) | |
) | |
end | |
def valid? | |
message_manager.valid? | |
end | |
def deliver | |
message_manager.all_recipients do |recipient| | |
Mailer.invitation_notification(invitation(recipient), message_manager.message) | |
end | |
end | |
private | |
def invitation(email) | |
Invitation.create(invitation_params(email)) | |
end | |
def invitation_params(email) | |
@survey_manager.params.merge({ | |
recipient_email: email, | |
status: 'pending' | |
}) | |
end | |
end | |
class MessageManager | |
def initialize(message, recipients) | |
@message = message | |
@recipients = recipients | |
freeze | |
end | |
def all_recipients(&block) | |
@recipients.each(&block) | |
end | |
def valid? | |
is_message_valid? && are_recipients_valid? | |
end | |
def is_message_valid? | |
@message.valid? | |
end | |
def are_recipients_valid? | |
@recipients.valid? | |
end | |
def message | |
@message.to_s | |
end | |
end | |
class SurveyManager | |
def initialize(survey, sender) | |
@survey = survey | |
@sender = sender | |
freeze | |
end | |
def survey | |
@survey.to_s | |
end | |
def sender | |
@sender.to_s | |
end | |
def params | |
{ | |
survey: @survey, | |
sender: @sender | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment