-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathinteractor.rb
57 lines (43 loc) · 1.33 KB
/
interactor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class CreateResponse
include Interactor
def call
responder = context.responder
survey_response = responder.survey_responses.build(
response_text: context.answers[:text],
rating: answers[:rating],
survey: context.survey
)
if survey_response.save
context.survey_response = survey_response
else
context.fail!(errors: survey_response.errors)
end
end
end
class AddRewardPoints
include Interactor
def call
reward_account = context.responder.reward_account
reward_account.balance += context.survey.reward_points
unless reward_account.save
context.fail!(errors: reward_account.errors)
end
end
end
class SendNotifications
include Interactor
def call
sender = context.survey.sender
SurveyMailer.delay.notify_responder(context.responder.id)
SurveyMailer.delay.notify_sender(sender.id)
unless sender.add_survey_response_notification
context.fail!(errors: sender.errors)
end
end
end
class ReplyToSurvey
include Interactor::Organizer
organize CreateResponse, AddRewardPoints, SendNotifications
end
# https://gist.githubusercontent.com/raderj89/cbb84b1f75e67087388bc4cdbe617138/raw/a39c3ba6b416ac3919cc7d32bfa58e82211f24ef/interactor_example.rb
# https://medium.com/reflektive-engineering/from-service-objects-to-interactors-db7d2bb7dfd9