File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11class InvitationsController < ApplicationController
22 def new
33 @survey = Survey . find ( params [ :survey_id ] )
4- @survey_inviter = SurveyInviter . new ( '' , '' )
4+ @survey_inviter = SurveyInviter . new
55 end
66
77 def create
88 @survey = Survey . find ( params [ :survey_id ] )
9- @survey_inviter = SurveyInviter . new ( message , recipients )
9+ @survey_inviter = SurveyInviter . new ( survey_inviter_attributes )
1010 if @survey_inviter . valid?
11- recipient_list . each do |email |
12- invitation = Invitation . create (
13- survey : @survey ,
14- sender : current_user ,
15- recipient_email : email ,
16- status : 'pending'
17- )
18- Mailer . invitation_notification ( invitation , message )
19- end
11+ @survey_inviter . deliver
2012 redirect_to survey_path ( @survey ) , notice : 'Invitation successfully sent'
2113 else
2214 @recipients = recipients
@@ -27,12 +19,8 @@ def create
2719
2820 private
2921
30- def invalid_recipients
31- @survey_inviter . invalid_recipients
32- end
33-
34- def recipient_list
35- @survey_inviter . recipient_list
22+ def survey_inviter_attributes
23+ params [ :invitation ] . merge ( survey : @survey , sender : current_user )
3624 end
3725
3826 def recipients
Original file line number Diff line number Diff line change 11class SurveyInviter
22 EMAIL_REGEX = /\A ([^@\s ]+)@((?:[-a-z0-9]+\. )+[a-z]{2,})\z /
33
4- def initialize ( message , recipients )
5- @message = message
6- @recipients = recipients
4+ def initialize ( attributes = { } )
5+ @survey = attributes [ :survey ]
6+ @message = attributes [ :message ] || ''
7+ @recipients = attributes [ :recipients ] || ''
8+ @sender = attributes [ :sender ]
79 end
810
911 def valid?
1012 valid_message? && valid_recipients?
1113 end
1214
15+ def deliver
16+ recipient_list . each do |email |
17+ invitation = Invitation . create (
18+ survey : @survey ,
19+ sender : @sender ,
20+ recipient_email : email ,
21+ status : 'pending'
22+ )
23+ Mailer . invitation_notification ( invitation , @message )
24+ end
25+ end
26+
1327 def invalid_recipients
1428 @invalid_recipients ||= recipient_list . map do |item |
1529 unless item . match ( EMAIL_REGEX )
@@ -18,10 +32,6 @@ def invalid_recipients
1832 end . compact
1933 end
2034
21- def recipient_list
22- @recipient_list ||= @recipients . gsub ( /\s +/ , '' ) . split ( /[\n ,;]+/ )
23- end
24-
2535 private
2636
2737 def valid_message?
@@ -31,4 +41,8 @@ def valid_message?
3141 def valid_recipients?
3242 invalid_recipients . empty?
3343 end
44+
45+ def recipient_list
46+ @recipient_list ||= @recipients . gsub ( /\s +/ , '' ) . split ( /[\n ,;]+/ )
47+ end
3448end
You can’t perform that action at this time.
0 commit comments