Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/donations_helper.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
9 changes: 9 additions & 0 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion app/views/donations/_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@
<% @donations.each do |donation| %>
<tr <%= "data-href=\"#{sanitize donation_path(donation)}\"".html_safe unless local_assigns[:for_deleted] %>>
<td><%= donation.id %></td>
<td><%= donation.county&.name %></td>

<td>
<%= donation.county&.name %>

<% if donation.county.blank? && local_assigns[:for_closed] %>
<%= fix_donation_county_button(donation) %>
<% end %>
</td>

<td><%= truncate donation.donor.name, length: 50 %></td>
<td><%= donation.item_count %></td>
<td><%= number_to_currency(donation.value, unit: "$", precision: 2) %></td>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

member do
post :close
post :fix_county
patch :restore
post :sync
delete :destroy_closed
Expand Down
4 changes: 0 additions & 4 deletions spec/controllers/dontions_controller_spec.rb

This file was deleted.

63 changes: 63 additions & 0 deletions spec/requests/donations_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "rails_helper"

RSpec.describe DonationsController, type: :request do
let!(:user) { users(:root) }

before do
sign_in user
end

describe "#fix_county" do
context "when the donation already has a county (even though different from donor)" do
let(:donation) { donations(:donation_with_donor_with_different_county) }

before do
donation.update_column(:closed_at, Time.zone.now)
end

it "throws an error" do
expect(donation.county).not_to eq(donation.donor.county)
post "/donations/#{donation.id}/fix_county"
expect(flash[:error]).to be_present

donation.reload
expect(donation.county).not_to eq(donation.donor.county)
end
end

context "when the donation doesn't have a county but the donor does" do
let(:donation) { donations(:donation_with_donor_with_county) }

before do
donation.update_column(:closed_at, Time.zone.now)
end

it "updates the county" do
expect(donation.county).to be_blank
post "/donations/#{donation.id}/fix_county"
expect(flash[:error]).to be_blank

donation.reload
expect(donation.county).to eq(donation.donor.county)
end
end

context "when the donation doesn't have a county but neither does the donor" do
let(:donation) { donations(:trois_donation) }

before do
donation.update_column(:closed_at, Time.zone.now)
end

it "throws an error" do
expect(donation.county).to be_blank
expect(donation.donor.county).to be_blank
post "/donations/#{donation.id}/fix_county"
expect(flash[:error]).to be_present

donation.reload
expect(donation.county).to be_blank
end
end
end
end