Skip to content

Commit

Permalink
GREEN keeps all entered information after invalid form submission
Browse files Browse the repository at this point in the history
* Also keeps submitted incorrect quantity
  • Loading branch information
jimmyli97 committed Jul 12, 2024
1 parent 672e9b6 commit 708bbbb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
17 changes: 15 additions & 2 deletions app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,27 @@ def show

def update
@donation = Donation.find(params[:id])
@original_source = @donation.source
ItemizableUpdateService.call(itemizable: @donation,
params: donation_params,
type: :increase,
event_class: DonationEvent)
flash.clear
flash[:notice] = "Donation updated!"
redirect_to donations_path
rescue => e
flash[:alert] = "Error updating donation: #{e.message}"
redirect_back(fallback_location: edit_donation_path)
load_form_collections
# calling new(donation_params) triggers a validation error if line_item quantity is invalid
@previous_input = Donation.new(donation_params.except(:line_items_attributes))
line_items = []
donation_params[:line_items_attributes].values.each { |attr|
attr.delete(:_destroy)
line_items.push(attr)
}
@previous_input.line_items.build(line_items)

render "edit", status: :conflict
end

def destroy
Expand Down Expand Up @@ -116,7 +129,7 @@ def clean_donation_money_raised
params[:donation][:money_raised] = money_raised.gsub(/[$,.]/, "") if money_raised

money_raised_in_dollars = params[:donation][:money_raised_in_dollars]
params[:donation][:money_raised] = money_raised_in_dollars.gsub(/[$,]/, "").to_d * 100 if money_raised_in_dollars
params[:donation][:money_raised] = (money_raised_in_dollars.gsub(/[$,]/, "").to_d * 100).to_s if money_raised_in_dollars
end

def donation_params
Expand Down
27 changes: 20 additions & 7 deletions app/views/donations/_donation_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= f.input :source,
collection: Donation::SOURCES.values,
selected: donation_form.source,
include_blank: true,
label: "Source",
error: "What effort or initiative did this donation come from?",
wrapper: :input_group %>
Expand All @@ -16,6 +18,8 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= f.association :donation_site,
collection: @donation_sites,
selected: donation_form.donation_site_id,
include_blank: true,
label: "Donation Site",
error: "Where was this donation dropped off?",
wrapper: :input_group %>
Expand All @@ -25,6 +29,8 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= f.association :product_drive,
collection: @product_drives,
selected: donation_form.product_drive_id,
include_blank: true,
label_method: lambda { |x| "#{x.try(:name) }" },
label: "Product Drive",
error: "Which product drive was this from?",
Expand All @@ -33,6 +39,8 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= f.association :product_drive_participant,
collection: @product_drive_participants,
selected: donation_form.product_drive_participant_id,
include_blank: true,
label_method: lambda { |x| "#{x.try(:business_name) }" },
label: "Product Drive Participant",
error: "Which product drive participant was this from?",
Expand All @@ -44,7 +52,8 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= f.association :manufacturer,
collection: @manufacturers,
prompt: "Choose one...",
selected: donation_form.manufacturer_id,
include_blank: true,
label_method: lambda { |x| "#{x.try(:name) }" },
label: "Manufacturer",
error: "Which Manufacturer was this from?",
Expand All @@ -58,7 +67,7 @@
collection: @storage_locations,
label: "Storage Location",
error: "Where is it being stored?",
selected: f.object.storage_location&.id || current_organization.intake_location,
selected: donation_form.storage_location&.id || current_organization.intake_location,
include_blank: true,
wrapper: :input_group %>
</div>
Expand All @@ -68,15 +77,18 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= f.input :money_raised_in_dollars, as: :money_raised_in_dollars, wrapper: :input_group do %>
<span class="input-group-text"><i class="fa fa-usd"></i></span>
<%= f.input_field :money_raised_in_dollars, class: "form-control" %>
<%= f.input_field :money_raised_in_dollars,
class: "form-control",
value: donation_form.money_raised_in_dollars %>
<% end %>
</div>
</div>

<div class="row">
<div class="col-xs-8 col-md-6">
<%= f.input :comment,
wrapper: :input_group %>
<%= f.input :comment, wrapper: :input_group do %>
<%= f.text_area :comment, value: donation_form.comment, cols: 90, rows: 2, class: "text optional form-control" %>
<% end %>
</div>
</div>

Expand All @@ -86,15 +98,16 @@
label: "Issued on",
as: :date,
html5: true,
wrapper: :input_group %>
wrapper: :input_group,
input_html: { value: donation_form.issued_at } %>
</div>
</div>

<fieldset style="margin-bottom: 2rem;" class='w-70'>
<legend>Items in this donation</legend>
<div id="donation_line_items" data-capture-barcode="true">

<%= render 'line_items/line_item_fields', form: f %>
<%= render 'line_items/line_item_fields', form: f, object: donation_form.line_items %>
</div>

<div class="row links justify-content-end">
Expand Down
8 changes: 4 additions & 4 deletions app/views/donations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<% content_for :title, "Edit - Donations - #{@donation.source} - #{current_organization.name}" %>
<% content_for :title, "Edit - Donations - #{@original_source ? @original_source : @donation.source} - #{current_organization.name}" %>
<h1>
Editing Donation
<small>from <%= @donation.source %></small>
<small>from <%= @original_source ? @original_source : @donation.source %></small>
</h1>
</div>
<div class="col-sm-6">
Expand All @@ -16,7 +16,7 @@
</li>
<li class="breadcrumb-item"><%= link_to "Donations", donations_path %></li>
<li class="breadcrumb-item">
<a href="#">Editing <%= @donation.source %> on <%= @donation.created_at.to_fs(:distribution_date) %></a></li>
<a href="#">Editing <%= @original_source ? @original_source : @donation.source %> on <%= @donation.created_at.to_fs(:distribution_date) %></a></li>
</ol>
</div>
</div>
Expand All @@ -39,7 +39,7 @@
<div class="card card-primary">
<!-- /.card-header -->
<div class="card-body">
<%= render partial: "donation_form", object: @donation %>
<%= render partial: "donation_form", object: @previous_input ? @previous_input : @donation %>
<%= link_to "Add vendor", new_vendor_path, {:remote => true, "data-bs-toggle" => "modal", "data-bs-target" => "#modal-window", id: "new_vendor", "style" => "display: none;"} %>
</div><!-- /.box -->
</div>
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/donations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
}
donation_params = { source: donation.source, storage_location: new_storage_location, line_items_attributes: line_item_params }
put :update, params: { id: donation.id, donation: donation_params }
expect(response).to redirect_to(edit_donation_path(donation))
expect(response).not_to redirect_to(edit_donation_path(donation))
expect(original_storage_location.size).to eq 5
expect(new_storage_location.size).to eq 0
expect(donation.reload.line_items.first.quantity).to eq 10
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/donations_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
put donation_path(id: donation.id, donation: edited_donation)

if Event.read_events?(organization)
expect(flash[:alert]).to include("Error updating donation: Could not reduce quantity by 99 - current quantity is 10 for Brightbloom Seed")
expect(flash[:alert]).to include("Error updating donation: Could not reduce quantity")
else # TODO remove this branch when switching to events
expect(flash[:alert]).to include("Error updating donation: Requested items exceed the available inventory")
end
Expand Down

0 comments on commit 708bbbb

Please sign in to comment.