Skip to content

Commit

Permalink
Address comments from #730 (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
acherukuri committed Sep 25, 2020
1 parent 0ca77df commit 26b0e59
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
20 changes: 9 additions & 11 deletions app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,39 @@ def new
contribution = Listing.find(params[:contribution_id])
if contribution.person&.email.blank?
flash.now[:alert] = "We are sorry, this contributor hasn't provided
an email address yet and can't communicate this way".squish
an email address yet and can't communicate this way".squish
end
render locals: { contribution: contribution }
end

# FIXME: extract into an interactor
def create
# TODO: Need to handle race conditions to prevent creating multiple matches for same contribution.

contribution = Listing.find(params[:contribution_id])
ActiveRecord::Base.transaction do
if current_user.person.blank?
Person.create_from_peer_to_peer_params!(current_user, name: peer_to_peer_match_params[:peer_alias],
preferred_contact_method_id: peer_to_peer_match_params[:preferred_contact_method_id],
contact_info: peer_to_peer_match_params[:preferred_contact_details])
Person.create_from_peer_to_peer_params!(current_user, name: claim_params[:peer_alias],
preferred_contact_method_id: claim_params[:preferred_contact_method_id],
contact_info: claim_params[:preferred_contact_details])
end
Match.create_match_for_contribution!(contribution, current_user)
contribution.matched!
end
notify_peer_and_log_communication!(contribution)
redirect_to contribution_path(params[:contribution_id]), notice: 'Claim was successful!'
rescue
flash.now[:alert] = 'There was an error. Please try again.'
render :new, locals: { contribution: contribution }
end

private

def peer_to_peer_match_params
params.require(:peer_to_peer_match).permit(:peer_alias, :preferred_contact_method_id, :preferred_contact_details, :message)
def claim_params
params.require(:claim).permit(:peer_alias, :preferred_contact_method_id, :preferred_contact_details, :message)
end

def notify_peer_and_log_communication!(contribution)
peer_to_peer_email = PeerToPeerMatchMailer.peer_to_peer_email(contribution,
peer_alias: peer_to_peer_match_params[:peer_alias],
message: peer_to_peer_match_params[:message])
peer_alias: claim_params[:peer_alias],
message: claim_params[:message])
delivery_status = deliver_now_with_error_handling(peer_to_peer_email, "peer_to_peer_email")
CommunicationLog.log_email(peer_to_peer_email, delivery_status, current_user.person, nil, current_user)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/claims/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
strangers.
</p>

<%= simple_form_for :peer_to_peer_match, url: contribution_claims_path(contribution) do |f| %>
<%= simple_form_for :claim, url: contribution_claims_path(contribution) do |f| %>
<%= f.input :peer_alias, as: :string, label: false, placeholder: "Alias or handle (do not use your real name here)", required: true %>
<%= f.input :preferred_contact_method_id, collection: ContactMethod.pluck(:name, :id), label: "Preferred contact method", selected: peer_to_peer_preferred_contact_method&.id, required: true %>
<%= f.input :preferred_contact_details, as: :string, placeholder: "Add your phone number or email address", input_html: { value: peer_to_peer_preferred_contact_details }, required: true %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/contributions/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section class="section">
<h2>Request for Contribution</h2>
<h1 class="title"><%= contribution.type %>: <%= contribution.all_tags_unique.map(&:humanize).join(", ") %></h1>
<h1>Request for Contribution</h1>
<h2 class="title"><%= contribution.type %>: <%= contribution.all_tags_unique.map(&:humanize).join(", ") %></h2>
<br>
<div class="box">
<p><strong><%= contribution.person.anonymized_name_and_email %></strong></p>
Expand Down

0 comments on commit 26b0e59

Please sign in to comment.