Permalink
Browse files

Extract Class Step Five

* Move another method
* Pass in controller state
  • Loading branch information...
jferris committed Feb 14, 2013
1 parent b434954 commit 000babe182a15e24821683babc93a51314977c8f
Showing with 26 additions and 24 deletions.
  1. +5 −17 example_app/app/controllers/invitations_controller.rb
  2. +21 −7 example_app/app/models/survey_inviter.rb
@@ -1,22 +1,14 @@
class InvitationsController < ApplicationController
def new
@survey = Survey.find(params[:survey_id])
@survey_inviter = SurveyInviter.new('', '')
@survey_inviter = SurveyInviter.new
end

def create
@survey = Survey.find(params[:survey_id])
@survey_inviter = SurveyInviter.new(message, recipients)
@survey_inviter = SurveyInviter.new(survey_inviter_attributes)
if @survey_inviter.valid?
recipient_list.each do |email|
invitation = Invitation.create(
survey: @survey,
sender: current_user,
recipient_email: email,
status: 'pending'
)
Mailer.invitation_notification(invitation, message)
end
@survey_inviter.deliver
redirect_to survey_path(@survey), notice: 'Invitation successfully sent'
else
@recipients = recipients
@@ -27,12 +19,8 @@ def create

private

def invalid_recipients
@survey_inviter.invalid_recipients
end

def recipient_list
@survey_inviter.recipient_list
def survey_inviter_attributes
params[:invitation].merge(survey: @survey, sender: current_user)
end

def recipients
@@ -1,15 +1,29 @@
class SurveyInviter
EMAIL_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/

def initialize(message, recipients)
@message = message
@recipients = recipients
def initialize(attributes = {})
@survey = attributes[:survey]
@message = attributes[:message] || ''
@recipients = attributes[:recipients] || ''
@sender = attributes[:sender]
end

def valid?
valid_message? && valid_recipients?
end

def deliver
recipient_list.each do |email|
invitation = Invitation.create(
survey: @survey,
sender: @sender,
recipient_email: email,
status: 'pending'
)
Mailer.invitation_notification(invitation, @message)
end
end

def invalid_recipients
@invalid_recipients ||= recipient_list.map do |item|
unless item.match(EMAIL_REGEX)
@@ -18,10 +32,6 @@ def invalid_recipients
end.compact
end

def recipient_list
@recipient_list ||= @recipients.gsub(/\s+/, '').split(/[\n,;]+/)
end

private

def valid_message?
@@ -31,4 +41,8 @@ def valid_message?
def valid_recipients?
invalid_recipients.empty?
end

def recipient_list
@recipient_list ||= @recipients.gsub(/\s+/, '').split(/[\n,;]+/)
end
end

0 comments on commit 000babe

Please sign in to comment.