Skip to content

Commit

Permalink
Extract Class Step Four
Browse files Browse the repository at this point in the history
* 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 b434954
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
14 changes: 3 additions & 11 deletions example_app/app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion example_app/app/models/survey_inviter.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.