File tree 2 files changed +26
-24
lines changed
2 files changed +26
-24
lines changed Original file line number Diff line number Diff line change 1
1
class InvitationsController < ApplicationController
2
2
def new
3
3
@survey = Survey . find ( params [ :survey_id ] )
4
- @survey_inviter = SurveyInviter . new ( '' , '' )
4
+ @survey_inviter = SurveyInviter . new
5
5
end
6
6
7
7
def create
8
8
@survey = Survey . find ( params [ :survey_id ] )
9
- @survey_inviter = SurveyInviter . new ( message , recipients )
9
+ @survey_inviter = SurveyInviter . new ( survey_inviter_attributes )
10
10
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
20
12
redirect_to survey_path ( @survey ) , notice : 'Invitation successfully sent'
21
13
else
22
14
@recipients = recipients
@@ -27,12 +19,8 @@ def create
27
19
28
20
private
29
21
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 )
36
24
end
37
25
38
26
def recipients
Original file line number Diff line number Diff line change 1
1
class SurveyInviter
2
2
EMAIL_REGEX = /\A ([^@\s ]+)@((?:[-a-z0-9]+\. )+[a-z]{2,})\z /
3
3
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 ]
7
9
end
8
10
9
11
def valid?
10
12
valid_message? && valid_recipients?
11
13
end
12
14
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
+
13
27
def invalid_recipients
14
28
@invalid_recipients ||= recipient_list . map do |item |
15
29
unless item . match ( EMAIL_REGEX )
@@ -18,10 +32,6 @@ def invalid_recipients
18
32
end . compact
19
33
end
20
34
21
- def recipient_list
22
- @recipient_list ||= @recipients . gsub ( /\s +/ , '' ) . split ( /[\n ,;]+/ )
23
- end
24
-
25
35
private
26
36
27
37
def valid_message?
@@ -31,4 +41,8 @@ def valid_message?
31
41
def valid_recipients?
32
42
invalid_recipients . empty?
33
43
end
44
+
45
+ def recipient_list
46
+ @recipient_list ||= @recipients . gsub ( /\s +/ , '' ) . split ( /[\n ,;]+/ )
47
+ end
34
48
end
You can’t perform that action at this time.
0 commit comments