File tree Expand file tree Collapse file tree 1 file changed +34
-15
lines changed
example_app/app/controllers Expand file tree Collapse file tree 1 file changed +34
-15
lines changed Original file line number Diff line number Diff line change @@ -7,32 +7,51 @@ def new
7
7
8
8
def create
9
9
@survey = Survey . find ( params [ :survey_id ] )
10
-
11
- @recipients = params [ :invitation ] [ :recipients ]
12
- recipient_list = @recipients . gsub ( /\s +/ , '' ) . split ( /[\n ,;]+/ )
13
-
14
- @invalid_recipients = recipient_list . map do |item |
15
- unless item . match ( EMAIL_REGEX )
16
- item
17
- end
18
- end . compact
19
-
20
- @message = params [ :invitation ] [ :message ]
21
-
22
- if @invalid_recipients . empty? && @message . present?
10
+ if valid_recipients? && valid_message?
23
11
recipient_list . each do |email |
24
12
invitation = Invitation . create (
25
13
survey : @survey ,
26
14
sender : current_user ,
27
15
recipient_email : email ,
28
16
status : 'pending'
29
17
)
30
- Mailer . invitation_notification ( invitation , @ message)
18
+ Mailer . invitation_notification ( invitation , message )
31
19
end
32
-
33
20
redirect_to survey_path ( @survey ) , notice : 'Invitation successfully sent'
34
21
else
22
+ @recipients = recipients
23
+ @message = message
35
24
render 'new'
36
25
end
37
26
end
27
+
28
+ private
29
+
30
+ def valid_recipients?
31
+ invalid_recipients . empty?
32
+ end
33
+
34
+ def valid_message?
35
+ message . present?
36
+ end
37
+
38
+ def invalid_recipients
39
+ @invalid_recipients ||= recipient_list . map do |item |
40
+ unless item . match ( EMAIL_REGEX )
41
+ item
42
+ end
43
+ end . compact
44
+ end
45
+
46
+ def recipient_list
47
+ @recipient_list ||= recipients . gsub ( /\s +/ , '' ) . split ( /[\n ,;]+/ )
48
+ end
49
+
50
+ def recipients
51
+ params [ :invitation ] [ :recipients ]
52
+ end
53
+
54
+ def message
55
+ params [ :invitation ] [ :message ]
56
+ end
38
57
end
You can’t perform that action at this time.
0 commit comments