Skip to content

Commit a050592

Browse files
committed
Extract Class Step Six
* Expose state from extracted class * Assign only one instance variable for view
1 parent 000babe commit a050592

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
11
class InvitationsController < ApplicationController
22
def new
3-
@survey = Survey.find(params[:survey_id])
4-
@survey_inviter = SurveyInviter.new
3+
@survey_inviter = SurveyInviter.new(survey: survey)
54
end
65

76
def create
8-
@survey = Survey.find(params[:survey_id])
97
@survey_inviter = SurveyInviter.new(survey_inviter_attributes)
108
if @survey_inviter.valid?
119
@survey_inviter.deliver
12-
redirect_to survey_path(@survey), notice: 'Invitation successfully sent'
10+
redirect_to survey_path(survey), notice: 'Invitation successfully sent'
1311
else
14-
@recipients = recipients
15-
@message = message
1612
render 'new'
1713
end
1814
end
1915

2016
private
2117

2218
def survey_inviter_attributes
23-
params[:invitation].merge(survey: @survey, sender: current_user)
19+
params[:invitation].merge(survey: survey, sender: current_user)
2420
end
2521

26-
def recipients
27-
params[:invitation][:recipients]
28-
end
29-
30-
def message
31-
params[:invitation][:message]
22+
def survey
23+
Survey.find(params[:survey_id])
3224
end
3325
end

example_app/app/models/survey_inviter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def initialize(attributes = {})
88
@sender = attributes[:sender]
99
end
1010

11+
attr_reader :message, :recipients, :survey
12+
1113
def valid?
1214
valid_message? && valid_recipients?
1315
end

example_app/app/views/invitations/new.html.erb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
<%= simple_form_for :invitation, url: survey_invitations_path(@survey) do |f| %>
2-
<%= f.input :message, as: :text, input_html: { value: @message } %>
1+
<%= simple_form_for(
2+
:invitation,
3+
url: survey_invitations_path(@survey_inviter.survey)
4+
) do |f| %>
5+
<%= f.input(
6+
:message,
7+
as: :text,
8+
input_html: { value: @survey_inviter.message }
9+
) %>
310
<% if @invlid_message %>
411
<div class="error">Please provide a message</div>
512
<% end %>
6-
<%= f.input :recipients, as: :text, input_html: { value: @recipients } %>
13+
<%= f.input(
14+
:recipients,
15+
as: :text,
16+
input_html: { value: @survey_inviter.recipients }
17+
) %>
718
<% if @survey_inviter.invalid_recipients %>
819
<div class="error">
920
Invalid email addresses:

0 commit comments

Comments
 (0)