Permalink
Browse files

Extract Class Step Four

* Move another method
* Wrap up compound condition
* Privatize methods only used internally in new class
  • Loading branch information...
jferris committed Feb 14, 2013
1 parent 0fefb96 commit b434954dbe4b054c8f909057d9167d8bb8c15d80
Showing with 19 additions and 12 deletions.
  1. +3 −11 example_app/app/controllers/invitations_controller.rb
  2. +16 −1 example_app/app/models/survey_inviter.rb
@@ -1,13 +1,13 @@
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(recipients)
if valid_recipients? && valid_message?
@survey_inviter = SurveyInviter.new(message, recipients)
if @survey_inviter.valid?
recipient_list.each do |email|
invitation = Invitation.create(
survey: @survey,
@@ -27,14 +27,6 @@ def create

private

def valid_recipients?
invalid_recipients.empty?
end

def valid_message?
message.present?
end

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

def initialize(recipients)
def initialize(message, recipients)
@message = message
@recipients = recipients
end

def valid?
valid_message? && valid_recipients?
end

def invalid_recipients
@invalid_recipients ||= recipient_list.map do |item|
unless item.match(EMAIL_REGEX)
@@ -16,4 +21,14 @@ def invalid_recipients
def recipient_list
@recipient_list ||= @recipients.gsub(/\s+/, '').split(/[\n,;]+/)
end

private

def valid_message?
@message.present?
end

def valid_recipients?
invalid_recipients.empty?
end
end

0 comments on commit b434954

Please sign in to comment.