Skip to content

Commit

Permalink
Extract Class Step Three
Browse files Browse the repository at this point in the history
* Move another method
* This method requires moving a constant
* Also requires changing the view to reference to the new class
  • Loading branch information
jferris committed Feb 14, 2013
1 parent ac01475 commit 0fefb96
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 2 additions & 7 deletions example_app/app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class InvitationsController < ApplicationController
EMAIL_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/

def new
@survey = Survey.find(params[:survey_id])
@survey_inviter = SurveyInviter.new('')
end

def create
Expand Down Expand Up @@ -37,11 +36,7 @@ def valid_message?
end

def invalid_recipients
@invalid_recipients ||= recipient_list.map do |item|
unless item.match(EMAIL_REGEX)
item
end
end.compact
@survey_inviter.invalid_recipients
end

def recipient_list
Expand Down
10 changes: 10 additions & 0 deletions example_app/app/models/survey_inviter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
class SurveyInviter
EMAIL_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/

def initialize(recipients)
@recipients = recipients
end

def invalid_recipients
@invalid_recipients ||= recipient_list.map do |item|
unless item.match(EMAIL_REGEX)
item
end
end.compact
end

def recipient_list
@recipient_list ||= @recipients.gsub(/\s+/, '').split(/[\n,;]+/)
end
Expand Down
4 changes: 2 additions & 2 deletions example_app/app/views/invitations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<div class="error">Please provide a message</div>
<% end %>
<%= f.input :recipients, as: :text, input_html: { value: @recipients } %>
<% if @invalid_recipients %>
<% if @survey_inviter.invalid_recipients %>
<div class="error">
Invalid email addresses:
<%= @invalid_recipients.join(', ') %>
<%= @survey_inviter.invalid_recipients.join(', ') %>
</div>
<% end %>
<%= f.button :submit, value: 'Invite' %>
Expand Down

0 comments on commit 0fefb96

Please sign in to comment.