-
Notifications
You must be signed in to change notification settings - Fork 0
/
propose_edit_contribution.rb
111 lines (92 loc) · 3.35 KB
/
propose_edit_contribution.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true
require 'forwardable'
%w(attributes
collaborators
transfer_errors
transfer_errors
update_article_with_marked_body
validate_attributes).each do |fname|
require_relative "./propose_edit_contribution/#{fname}"
end
# "Propose Edit Contribution" use case.
#
# A full writeup of the use case, its inputs, success and failure scenarios, and
# other closely-related items, now lives in the project's GitHub repo Wiki page
# entitled `Use Case Description: "Propose Edit Contribution"`. If you can read
# this, you should have access to that Wiki page, which is hereafter the Single
# Source of Truth for this class' requirements.
module Prolog
module UseCases
# Use case encapsulating all domain logic involved in submitting a proposal
# for an Edit Contribution.
class ProposeEditContribution
extend Forwardable
attr_reader :contribution
def initialize(authoriser:, contribution_repo:, article_repo:,
ui_gateway:)
params = { authoriser: authoriser, contribution_repo: contribution_repo,
article_repo: article_repo, ui_gateway: ui_gateway }
@collaborators = Collaborators.new params
self
end
def call(article:, endpoints:, proposed_content:, justification: '')
build_attributes article, endpoints, proposed_content, justification
run_steps
self
end
private
def_delegators :@collaborators, :article_repo, :authoriser,
:contribution_repo, :ui_gateway, :user_name
def_delegators :@attributes, :article, :article_id, :endpoints,
:justification, :proposed_content, :status
def build_attributes(article, endpoints, proposed_content, justification)
@attributes = Attributes.new article: article, endpoints: endpoints,
justification: justification,
proposed_content: proposed_content,
proposed_at: nil, proposed_by: user_name
self
end
def run_steps
steps_in_process if validator_valid?
transfer_errors
self
end
def validator_valid?
ValidateAttributes.new.call(@attributes).valid?
end
def steps_in_process
update_article_with_marked_body
persist_contribution
persist_article
notify_success
self
end
def notify_success
ui_gateway.success success_payload.to_json
end
def persist_article
article_repo.add article
end
def persist_contribution
contribution_repo.add updated_contribution
end
def success_payload
{ member: user_name, article_id: @attributes.article_id,
contribution_count: contribution_repo.count }
end
def transfer_errors
TransferErrors.call attributes: @attributes, ui_gateway: ui_gateway
self
end
def update_article_with_marked_body
params = { attributes: @attributes,
contribution_repo: contribution_repo }
@attributes = UpdateAttributesWithMarkedBody.call params
self
end
def updated_contribution
@contribution ||= contribution_repo.create @attributes
end
end # class Prolog::UseCases::ProposeEditContribution
end
end