diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index 041abf9fa0..a925ab6c41 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -90,14 +90,22 @@ 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}" - render "edit" + 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)) + @previous_input.line_items.build(donation_params[:line_items_attributes].values) + + render "edit", status: :conflict end def destroy @@ -129,14 +137,14 @@ 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 strip_unnecessary_params clean_donation_money_raised params = compact_line_items - params.require(:donation).permit(:source, :comment, :storage_location_id, :money_raised, :issued_at, :donation_site_id, :product_drive_id, :product_drive_participant_id, :manufacturer_id, line_items_attributes: %i(id item_id quantity _destroy)).merge(organization: current_organization) + params.require(:donation).permit(:source, :comment, :storage_location_id, :money_raised, :issued_at, :donation_site_id, :product_drive_id, :product_drive_participant_id, :manufacturer_id, line_items_attributes: %i(id item_id quantity)).merge(organization: current_organization) end def donation_item_params diff --git a/app/views/donations/_donation_form.html.erb b/app/views/donations/_donation_form.html.erb index 295be85271..c7de6c4921 100644 --- a/app/views/donations/_donation_form.html.erb +++ b/app/views/donations/_donation_form.html.erb @@ -6,6 +6,8 @@
<%= 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 %> @@ -16,6 +18,8 @@
<%= 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 %> @@ -25,6 +29,8 @@
<%= 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?", @@ -33,6 +39,8 @@
<%= 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?", @@ -44,7 +52,8 @@
<%= 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?", @@ -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 %>
@@ -68,15 +77,18 @@
<%= f.input :money_raised_in_dollars, as: :money_raised_in_dollars, wrapper: :input_group do %> - <%= 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 %>
- <%= 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 %>
@@ -86,7 +98,8 @@ label: "Issued on", as: :date, html5: true, - wrapper: :input_group %> + wrapper: :input_group, + input_html: { value: donation_form.issued_at } %>
@@ -94,7 +107,7 @@ Items in this donation
- <%= render 'line_items/line_item_fields', form: f %> + <%= render 'line_items/line_item_fields', form: f, object: donation_form.line_items %>