diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index f9dc0a39..88f3963c 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -2,7 +2,7 @@ class DonationsController < ApplicationController require_permission :can_sync_donations?, only: %i[sync] require_permission :can_view_donations? require_permission :can_create_donations?, except: %i[index show] - require_permission :can_close_donations?, only: %i[close closed] + require_permission :can_close_donations?, only: %i[close closed fix_county] require_permission :can_delete_and_restore_donations?, only: %i[deleted destroy restore] require_permission :can_delete_closed_donations?, only: %i[destroy_closed] before_action :authenticate_user! @@ -106,6 +106,19 @@ def restore end end + def fix_county + donation = Donation.find(params[:id]) + + if donation.county_id.present? + redirect_to closed_donations_path, flash: { error: "Donation already has a county!" } + elsif donation.donor.county_id.blank? + redirect_to closed_donations_path, flash: { error: "Donor doesn't have a county!" } + else + donation.fix_county! + redirect_to closed_donations_path, flash: { success: "Donation county fixed." } + end + end + def sync donation = current_user.sync_donation(params) redirect_to donation_path(donation) diff --git a/app/helpers/donations_helper.rb b/app/helpers/donations_helper.rb index 0aa9f230..7e8cfe4a 100644 --- a/app/helpers/donations_helper.rb +++ b/app/helpers/donations_helper.rb @@ -1,4 +1,17 @@ module DonationsHelper + def fix_donation_county_button(donation) + css_class = "btn btn-danger" + css_class += " disabled" if donation.donor.county_id.blank? + + button = button_to "Fix", fix_county_donation_path(donation), class: css_class, method: :post, disabled: donation.donor.county_id.blank? + + if donation.donor.county_id.blank? + disabled_title_wrapper("Please update the donor with a county.") { button } + else + button + end + end + def sync_donation_button(donation) css_class = "btn btn-primary" diff --git a/app/models/donation.rb b/app/models/donation.rb index 92802537..34e9d399 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -177,6 +177,15 @@ def add_to_donation!(params, required: false) end end + def fix_county! + raise "County is already present!" if county_id.present? + + @allow_change_after_closed = true + update!(county_id: donor.county_id) + ensure + @allow_change_after_closed = false + end + private def skip_adding_donations?(params, required) diff --git a/app/views/donations/_table.html.erb b/app/views/donations/_table.html.erb index fe76b18c..3f834f73 100644 --- a/app/views/donations/_table.html.erb +++ b/app/views/donations/_table.html.erb @@ -26,7 +26,15 @@ <% @donations.each do |donation| %>