Skip to content

Commit 9e2c232

Browse files
authored
Merge pull request #5323 from hagiya0121/feature/donation-site-on-the-fly
4982 Enable on-the-fly creation of Donation Sites from donation form
2 parents 3742718 + e4e0b21 commit 9e2c232

10 files changed

Lines changed: 129 additions & 16 deletions

File tree

app/controllers/donation_sites_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ def create
1818
@donation_site = current_organization.donation_sites.new(donation_site_params)
1919
respond_to do |format|
2020
if @donation_site.save
21+
format.turbo_stream
2122
format.html do
2223
redirect_to donation_sites_path,
2324
notice: "Donation site #{@donation_site.name} added!"
2425
end
2526
else
27+
flash.now[:error] = "Something didn't work quite right -- try again?"
28+
if request.format.turbo_stream?
29+
format.html { render partial: "donation_sites/new_modal", status: :unprocessable_entity }
30+
end
31+
2632
format.html do
27-
flash.now[:error] = "Something didn't work quite right -- try again?"
2833
render action: :new
2934
end
3035
end
@@ -33,6 +38,11 @@ def create
3338

3439
def new
3540
@donation_site = current_organization.donation_sites.new
41+
if turbo_frame_request?
42+
render partial: "donation_sites/new_modal", layout: false
43+
else
44+
render :new
45+
end
3646
end
3747

3848
def edit

app/helpers/donations_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def total_number_of_drives(range = selected_range)
2121
current_organization.product_drives.within_date_range(formatted_range).count
2222
end
2323

24+
def options_with_new(records)
25+
model_class = records.klass
26+
label = "---Create New #{model_class.model_name.human}---"
27+
records.map { |record| [record.name, record.id] } << [label, "new"]
28+
end
29+
2430
private
2531

2632
def total_received_donations_unformatted(range = selected_range)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
connect() {
5+
document.addEventListener("turbo:frame-render", this.openModalHandler)
6+
document.addEventListener("turbo:submit-end", this.closeModalHandler)
7+
}
8+
9+
disconnect() {
10+
document.removeEventListener("turbo:frame-render", this.openModalHandler)
11+
document.removeEventListener("turbo:submit-end", this.closeModalHandler)
12+
}
13+
14+
handleNewSelect(event) {
15+
const value = event.target.value
16+
if (value === "new") {
17+
const url = event.target.dataset.url
18+
Turbo.visit(url, { frame: 'modal-new' })
19+
}
20+
}
21+
22+
openModalHandler = () => {
23+
const modal = document.getElementById("modal-new")
24+
const instance = bootstrap.Modal.getOrCreateInstance(modal)
25+
instance.show()
26+
}
27+
28+
closeModalHandler = (event) => {
29+
if (event.detail.success) {
30+
const modal = document.getElementById("modal-new")
31+
const instance = bootstrap.Modal.getOrCreateInstance(modal)
32+
instance.hide()
33+
}
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%= turbo_frame_tag "modal-new" do %>
2+
<div class="modal-dialog">
3+
4+
<div class="modal-content">
5+
<div class="modal-header">
6+
<h4 class="modal-title" id="mainModalLabel">New Donation Site</h4>
7+
<button type="button" class="close" data-bs-dismiss="modal">&times;</button>
8+
</div>
9+
<div class="modal-body">
10+
<% flash.each do |key, value| %>
11+
<div class="<%= flash_class(key) %> alert-dismissible">
12+
<a href="#" class="close btn-close" data-bs-dismiss="alert" aria-label="close" style="text-decoration: none;"><%= fa_icon('times') %></a>
13+
<%= value %>
14+
</div>
15+
<% end %>
16+
17+
<%= render partial: "form", object: @donation_site %>
18+
19+
</div>
20+
<div class="modal-footer">
21+
<button type="button" class="btn btn-default" data-bs-dismiss="modal">Close</button>
22+
</div>
23+
</div>
24+
25+
</div>
26+
<% end %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<%= turbo_stream.append "donation_donation_site_id" do %>
2+
<option value="<%= @donation_site.id %>" selected>
3+
<%= @donation_site.name %>
4+
</option>
5+
<% end %>

app/views/donations/_donation_form.html.erb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%= simple_form_for @donation, data: { controller: "form-input" } do |f| %>
22

3-
<div class="box-body">
3+
<div class="box-body" data-controller="donations-modal">
4+
<%= turbo_frame_tag "modal-new", class: "modal fade", role: 'dialog' %>
45

56
<div class="row">
67
<div class="col-xs-8 col-md-6 col-lg-3">
@@ -17,12 +18,13 @@
1718
<div class="row">
1819
<div class="col-xs-8 col-md-6 col-lg-3">
1920
<%= f.association :donation_site,
20-
collection: @donation_sites,
21-
selected: donation_form.donation_site_id,
22-
include_blank: true,
23-
label: "Donation Site",
24-
error: "Where was this donation dropped off?",
25-
wrapper: :input_group %>
21+
collection: options_with_new(@donation_sites),
22+
selected: donation_form.donation_site_id,
23+
include_blank: true,
24+
label: "Donation Site",
25+
error: "Where was this donation dropped off?",
26+
wrapper: :input_group,
27+
input_html: { data: { action: "change->donations-modal#handleNewSelect", url: new_donation_site_path } } %>
2628
</div>
2729
</div>
2830
<div class="row">

config/locales/en.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ en:
3939
distribution:
4040
issued_at: "Distribution date and time"
4141
models:
42-
product_drive: "Product Drive"
42+
product_drive: "Product Drive"
43+
donation_site: "Donation Site"

docs/user_guide/bank/essentials_donations.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ Here you can enter all the information for your Donation:
5050
- Storage Location
5151
- Money raised in dollars (optional)
5252
- Comment (optional)
53-
- Issued On
53+
- Issued On
5454
- This is the date that the Donation was made. Note that inventory changes happen as of when you enter the information. They are not back-dated.
5555
- Items in this Donation
5656
- If you have set up [barcodes ](inventory_barcodes.md) for the Items you are receiving, you can use your barcode reader to enter your Items. Otherwise, pick the Item from "Choose an item", and enter the number of that Item in "Quantity".
5757
- Click "Add Another Item" as needed to get room for more Items.
5858

59-
When you are finished entering your information, click 'Save'.
59+
When you are finished entering your information, click 'Save'.
6060

6161
### Information entered in Donations
6262
#### Source
@@ -67,7 +67,7 @@ For Product Drive, Manufacturer, or Donation Site, you will further specify the
6767
If you specify Product drive, you'll also need to specify the particular drive and participant for the Donation -- but you can enter them "on the fly", here. You can view summaries for the Product Drives in [Product Drives](product_drives.md), and manage the contact info for a Product Drive Participant under [Community -- Product Drive Participants](community_product_drive_participants.md).
6868
##### *Donation Site
6969
Donation Site is meant to capture the concept of any place you have a more-or-less permanent place people can drop off donations at, such as your main office, or community locations such as fire halls, etc.
70-
You can see the Donations for each Donation Site and manage their contact information under [Community -- Donation Sites](community_donation_sites.md). Unlike Product Drives, you can't create a new Donation Site 'on the fly' through the fields here
70+
You can see the Donations for each Donation Site and manage their contact information under [Community -- Donation Sites](community_donation_sites.md). Like Product Drives, you can create a new Donation Site 'on the fly' through the fields here too.
7171
##### *Manufacturer
7272
This is for the donations that come straight from a Manufacturer. You can view a breakdown of the Donations for each Manufacturer under [Community -- Manufacturers](community_manufacturers.md)
7373
##### *Misc. Donation
@@ -103,8 +103,8 @@ To view the details of a Donation, click on the "View" button beside the donatio
103103
![Navigation to New Donations](images/essentials/donations/essentials_donations_4.png)
104104

105105
Here you'll see the Donation, including:
106-
- Date
107-
- Source
106+
- Date
107+
- Source
108108
- Donation Site
109109
- Storage Location
110110
- for each Item in the donation
@@ -126,7 +126,7 @@ Donations shouldn't need to be updated very often -- you usually have all the in
126126

127127
![Navigation to Edit a Donation](images/essentials/donations/essentials_donations_6.png)
128128

129-
Here, you can change all the information on the Donation, including adding and removing items.
129+
Here, you can change all the information on the Donation, including adding and removing items.
130130

131131
##### Note that changes you make to the levels of Items will take effect as of the date you made the changes. They will not be back-dated to the "issued on" date
132132

@@ -137,4 +137,4 @@ Hopefully you won't need to delete a Donation - but it's certainly possible that
137137

138138
## Printing a Donation
139139
You can print a single Donation by either viewing it, then clicking print, or just clicking "Print" beside it on the All Donations page. The printout is meant to be useful for parallel record keeping, or as the basis for a tax receipt.
140-
[Prior: Dashboard](essentials_dashboard.md)[Next: Purchases](essentials_purchases.md)
140+
[Prior: Dashboard](essentials_dashboard.md)[Next: Purchases](essentials_purchases.md)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
RSpec.describe DonationsHelper, type: :helper do
2+
describe "#options_with_new" do
3+
it "returns the options array including 'new' option for DonationSite" do
4+
donation_sites_relation = DonationSite.all
5+
options = helper.options_with_new(donation_sites_relation)
6+
expect(options.last).to eq(["---Create New Donation Site---", "new"])
7+
end
8+
9+
it "returns the options array including 'new' option for ProductDrive" do
10+
product_drives_relation = ProductDrive.all
11+
options = helper.options_with_new(product_drives_relation)
12+
expect(options.last).to eq(["---Create New Product Drive---", "new"])
13+
end
14+
end
15+
end

spec/system/donation_system_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@
360360
end.to change { Donation.count }.by(1)
361361
end
362362

363+
it "Allows User to create a Donataion Site from donation" do
364+
select Donation::SOURCES[:donation_site], from: "donation_source"
365+
select "---Create New Donation Site---", from: "donation_donation_site_id"
366+
367+
find(".modal-content")
368+
expect(page).to have_content("New Donation Site")
369+
370+
fill_in "donation_site_name", with: "Test Donation Site"
371+
fill_in "donation_site_address", with: "Test Address"
372+
within(".modal-content") { click_button "Save" }
373+
select "Test Donation Site", from: "donation_donation_site_id"
374+
end
375+
363376
it "Allows User to create a donation with a Miscellaneous source" do
364377
select Donation::SOURCES[:misc], from: "donation_source"
365378
expect(page).not_to have_xpath("//select[@id='donation_donation_site_id']")

0 commit comments

Comments
 (0)