From d5f56a9cd48c17f7dacb4920f6219cddd4d9fcc8 Mon Sep 17 00:00:00 2001 From: YuriBocharov Date: Tue, 16 Jan 2024 17:10:38 -0500 Subject: [PATCH 1/3] feat: adds specs for donation details This commit adds RSpec model, request and service specs for expected behavior of donation sources: Source column shows type of donation Details columns shows name for all but misc donations. Those show a truncated comment. Service specs test csv creation. Model specs test the newly added donation.details method. --- .../exports/export_donations_csv_service.rb | 6 +-- spec/models/donation_spec.rb | 38 ++++++++++++++ spec/requests/donations_requests_spec.rb | 49 ++++++++++++++++++- .../export_donations_csv_service_spec.rb | 6 +-- 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/app/services/exports/export_donations_csv_service.rb b/app/services/exports/export_donations_csv_service.rb index 8bb760434d..254ebcd196 100644 --- a/app/services/exports/export_donations_csv_service.rb +++ b/app/services/exports/export_donations_csv_service.rb @@ -63,13 +63,13 @@ def headers_with_indexes def base_table { "Source" => ->(donation) { - donation.source_view + donation.source }, "Date" => ->(donation) { donation.issued_at.strftime("%F") }, - "Donation Site" => ->(donation) { - donation.donation_site.try(:name) + "Details" => ->(donation) { + donation.details }, "Storage Location" => ->(donation) { donation.storage_view diff --git a/spec/models/donation_spec.rb b/spec/models/donation_spec.rb index 2c5b973ff3..162b7d5004 100644 --- a/spec/models/donation_spec.rb +++ b/spec/models/donation_spec.rb @@ -223,6 +223,44 @@ expect(donation.source_view).to eq(donation.source) end end + + context "details" do + context "manufacturer" do + let(:manufacturer) { create(:manufacturer) } + let(:donation) { create(:donation, source: "Manufacturer", manufacturer: manufacturer) } + + it "returns manufacturer name" do + expect(donation.details).to eq(manufacturer.name) + end + end + + context "product drive" do + let(:product_drive) { create(:product_drive) } + let(:donation) { create(:donation, source: "Product Drive", product_drive: product_drive) } + + it "returns product_drive name" do + expect(donation.details).to eq(product_drive.name) + end + end + + context "donation site" do + let(:donation_site) { create(:donation_site) } + let(:donation) { create(:donation, source: "Donation Site", donation_site: donation_site) } + + it "returns donation_site name" do + expect(donation.details).to eq(donation_site.name) + end + end + + context "misc" do + let(:donation) { create(:donation, source: "Misc. Donation", comment: Faker::Lorem.paragraph) } + + it "returns a truncated comment" do + short_comment = donation.comment.truncate(25, separator: /\s/) + expect(donation.details).to eq(short_comment) + end + end + end end end diff --git a/spec/requests/donations_requests_spec.rb b/spec/requests/donations_requests_spec.rb index 76e2bffc4e..345949ea2b 100644 --- a/spec/requests/donations_requests_spec.rb +++ b/spec/requests/donations_requests_spec.rb @@ -19,11 +19,58 @@ before do create(:donation) end - context "html" do let(:response_format) { 'html' } it { is_expected.to be_successful } + + it "should have the columns source and details" do + expect(subject.body).to include("Source") + expect(subject.body).to include("Details") + end + + context "when given a product drive" do + let(:product_drive) { create(:product_drive) } + let(:donation) { create(:donation, source: "Product Drive", product_drive: product_drive) } + + it "should display Product Drive and the name of the drive" do + donation + expect(subject.body).to include("#{donation.source}") + expect(subject.body).to include("#{product_drive.name}") + end + end + + context "when given a donation site" do + let(:donation_site) { create(:donation_site) } + let(:donation) { create(:donation, source: "Donation Site", donation_site: donation_site) } + + it "should display Donation Site and the name of the site" do + donation + expect(subject.body).to include("#{donation.source}") + expect(subject.body).to include("#{donation_site.name}") + end + end + + context "when given a manufacturer" do + let(:manufacturer) { create(:manufacturer) } + let(:donation) { create(:donation, source: "Manufacturer", manufacturer: manufacturer) } + + it "should display Manufacturer and the manufacturer name" do + donation + expect(subject.body).to include("#{donation.source}") + expect(subject.body).to include("#{manufacturer.name}") + end + end + + context "when given a misc donation" do + let(:donation) { create(:donation, source: "Misc. Donation", comment: Faker::Lorem.paragraph) } + + it "should display Misc Donation and a truncated comment" do + donation + expect(subject.body).to include("#{donation.source}") + expect(subject.body).to include("#{donation.comment[..25]}") + end + end end context "csv" do diff --git a/spec/services/exports/export_donations_csv_service_spec.rb b/spec/services/exports/export_donations_csv_service_spec.rb index 9f6537a07b..cfffa7337b 100644 --- a/spec/services/exports/export_donations_csv_service_spec.rb +++ b/spec/services/exports/export_donations_csv_service_spec.rb @@ -56,7 +56,7 @@ [ "Source", "Date", - "Donation Site", + "Details", "Storage Location", "Quantity of Items", "Variety of Items", @@ -87,9 +87,9 @@ donations.zip(total_item_quantities).each_with_index do |(donation, total_item_quantity), idx| row = [ - donation.source_view, + donation.source, donation.issued_at.strftime("%F"), - donation.donation_site.try(:name), + donation.details, donation.storage_view, donation.line_items.total, total_item_quantity.count(&:positive?), From 23f1ff0b607a5adb189a3e67edf40db8472f58bb Mon Sep 17 00:00:00 2001 From: YuriBocharov Date: Tue, 16 Jan 2024 15:11:12 -0500 Subject: [PATCH 2/3] fix(#3324): adds donation source details Changes donation/index to have Source and Details columns for donations. Adds a details methods to a donation to provide appropriate details. --- app/models/donation.rb | 13 ++ app/views/donations/_donation_row.html.erb | 20 +-- app/views/donations/index.html.erb | 142 +++++++++++++-------- spec/requests/donations_requests_spec.rb | 3 +- 4 files changed, 118 insertions(+), 60 deletions(-) diff --git a/app/models/donation.rb b/app/models/donation.rb index 35120311e5..2100b738af 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -104,6 +104,19 @@ def self.daily_quantities_by_source(start, stop) .sum("line_items.quantity") end + def details + case source + when SOURCES[:product_drive] + product_drive.name + when SOURCES[:manufacturer] + manufacturer.name + when SOURCES[:donation_site] + donation_site.name + when SOURCES[:misc] + comment&.truncate(25, separator: /\s/) + end + end + def remove(item) # doing this will handle either an id or an object item_id = item.to_i diff --git a/app/views/donations/_donation_row.html.erb b/app/views/donations/_donation_row.html.erb index 0f01550f4a..50ea5a8618 100644 --- a/app/views/donations/_donation_row.html.erb +++ b/app/views/donations/_donation_row.html.erb @@ -1,13 +1,15 @@ - <%= donation_row.source_view %> + <%= donation_row.source %> <%= donation_row.issued_at.strftime("%F") %> - <%= donation_row.donation_site.try(:name) %> - <%= donation_row.storage_location.name %> - <%= donation_row.line_items.total %> - <%= dollar_value(donation_row.money_raised.to_i) %> - <%= dollar_value(donation_row.value_per_itemizable) %> - <%= truncate donation_row.comment, length: 140, separator: /\w+/ %> - - <%= view_button_to donation_path(donation_row) %> + <%= donation_row.details %> + <%= donation_row.storage_location.name %> + <%= donation_row.line_items.total %> + <%= dollar_value(donation_row.money_raised.to_i) %> + <%= dollar_value(donation_row.value_per_itemizable) %> + <%= truncate donation_row.comment, length: 140, separator: /\w+/ %> + + <%= view_button_to donation_path(donation_row) %> + + diff --git a/app/views/donations/index.html.erb b/app/views/donations/index.html.erb index adb67b0fd2..420aadf519 100644 --- a/app/views/donations/index.html.erb +++ b/app/views/donations/index.html.erb @@ -5,22 +5,24 @@ <% content_for :title, "Donations - #{current_organization.name}" %>

Donations - for <%= current_organization.name %> + for + <%= current_organization.name %>

- + + -
@@ -31,65 +33,103 @@

Donations Filters

- - +
<%= form_tag(donations_path, method: :get) do |f| %>
<% if @storage_locations.present? %>
- <%= filter_select(label: "Filter by Storage Location", scope: :at_storage_location, collection: @storage_locations, selected: @selected_storage_location) %> + <%= filter_select( + label: "Filter by Storage Location", + scope: :at_storage_location, + collection: @storage_locations, + selected: @selected_storage_location, + ) %>
<% end %> <% if @sources.present? %>
<%= label_tag "by Source" %> - <%= select_tag "filters[by_source]", options_for_select(@sources, @selected_source), {include_blank: true, class: "form-control"} %> + <%= select_tag "filters[by_source]", + options_for_select(@sources, @selected_source), + { include_blank: true, class: "form-control" } %>
<% end %> <% if @product_drives.present? %>
- <%= filter_select(scope: :by_product_drive, collection: @product_drives, selected: @selected_product_drive) %> - + <%= filter_select( + scope: :by_product_drive, + collection: @product_drives, + selected: @selected_product_drive, + ) %>
<% end %> <% if @product_drive_participants.present? %>
- <%= filter_select(scope: :by_product_drive_participant, collection: @product_drive_participants, value: :business_name, selected: @selected_product_drive_participant) %> + <%= filter_select( + scope: :by_product_drive_participant, + collection: @product_drive_participants, + value: :business_name, + selected: @selected_product_drive_participant, + ) %>
<% end %> <% if @manufacturers.present? %>
- <%= filter_select(label: "Filter by manufacturer", scope: :from_manufacturer, collection: @manufacturers, selected: @selected_manufacturer) %> + <%= filter_select( + label: "Filter by manufacturer", + scope: :from_manufacturer, + collection: @manufacturers, + selected: @selected_manufacturer, + ) %>
<% end %> <% if @donation_sites.present? %>
- <%= filter_select(label: "Filter by Donation Site", scope: :from_donation_site, collection: @donation_sites, key: :id, value: :name, selected: @selected_donation_site) %> + <%= filter_select( + label: "Filter by Donation Site", + scope: :from_donation_site, + collection: @donation_sites, + key: :id, + value: :name, + selected: @selected_donation_site, + ) %>
<% end %>
<%= label_tag "Date Range" %> - <%= render partial: "shared/date_range_picker", locals: {css_class: "form-control"} %> + <%= render partial: "shared/date_range_picker", + locals: { + css_class: "form-control", + } %>
-
+
+ <% end %>
- - + +
-
@@ -99,38 +139,40 @@
- - - - - - - - - - - + + + + + + + + + + + - <%= render partial: "donation_row", collection: @paginated_donations %> + <%= render partial: "donation_row", collection: @paginated_donations %> - - - - - - - - - - + + + + + + + + + +
SourceDateDonation SiteStorage LocationQuantity of ItemsMoney RaisedIn Kind ValueCommentsActions
SourceDateDetailsStorage LocationQuantity of ItemsMoney RaisedIn Kind ValueCommentsActions
Total - - <%= @donations_quantity %> (Total) - -
- <%= @paginated_donations_quantity %> (This page) -
<%= dollar_value(@total_money_raised) %><%= dollar_value(@total_value_all_donations) %>
Total + + <%= @donations_quantity %> + (Total) + +
+ <%= @paginated_donations_quantity %> + (This page) +
<%= dollar_value(@total_money_raised) %><%= dollar_value(@total_value_all_donations) %>
@@ -144,4 +186,4 @@
-
\ No newline at end of file + diff --git a/spec/requests/donations_requests_spec.rb b/spec/requests/donations_requests_spec.rb index 345949ea2b..ba2612a20d 100644 --- a/spec/requests/donations_requests_spec.rb +++ b/spec/requests/donations_requests_spec.rb @@ -67,8 +67,9 @@ it "should display Misc Donation and a truncated comment" do donation + short_comment = donation.comment.truncate(25, separator: /\s/) expect(subject.body).to include("#{donation.source}") - expect(subject.body).to include("#{donation.comment[..25]}") + expect(subject.body).to include("#{short_comment}") end end end From 77be37228ec366946a84dd74b697e0e85e2ac4fa Mon Sep 17 00:00:00 2001 From: YuriBocharov Date: Sun, 21 Jan 2024 18:02:22 -0500 Subject: [PATCH 3/3] fix: revert linting --- app/views/donations/index.html.erb | 118 ++++++++++------------------- 1 file changed, 38 insertions(+), 80 deletions(-) diff --git a/app/views/donations/index.html.erb b/app/views/donations/index.html.erb index 420aadf519..d4dd3d253e 100644 --- a/app/views/donations/index.html.erb +++ b/app/views/donations/index.html.erb @@ -5,24 +5,22 @@ <% content_for :title, "Donations - #{current_organization.name}" %>

Donations - for - <%= current_organization.name %> + for <%= current_organization.name %>

- - + +
@@ -33,103 +31,65 @@

Donations Filters

- + +
<%= form_tag(donations_path, method: :get) do |f| %>
<% if @storage_locations.present? %>
- <%= filter_select( - label: "Filter by Storage Location", - scope: :at_storage_location, - collection: @storage_locations, - selected: @selected_storage_location, - ) %> + <%= filter_select(label: "Filter by Storage Location", scope: :at_storage_location, collection: @storage_locations, selected: @selected_storage_location) %>
<% end %> <% if @sources.present? %>
<%= label_tag "by Source" %> - <%= select_tag "filters[by_source]", - options_for_select(@sources, @selected_source), - { include_blank: true, class: "form-control" } %> + <%= select_tag "filters[by_source]", options_for_select(@sources, @selected_source), {include_blank: true, class: "form-control"} %>
<% end %> <% if @product_drives.present? %>
- <%= filter_select( - scope: :by_product_drive, - collection: @product_drives, - selected: @selected_product_drive, - ) %> + <%= filter_select(scope: :by_product_drive, collection: @product_drives, selected: @selected_product_drive) %> +
<% end %> <% if @product_drive_participants.present? %>
- <%= filter_select( - scope: :by_product_drive_participant, - collection: @product_drive_participants, - value: :business_name, - selected: @selected_product_drive_participant, - ) %> + <%= filter_select(scope: :by_product_drive_participant, collection: @product_drive_participants, value: :business_name, selected: @selected_product_drive_participant) %>
<% end %> <% if @manufacturers.present? %>
- <%= filter_select( - label: "Filter by manufacturer", - scope: :from_manufacturer, - collection: @manufacturers, - selected: @selected_manufacturer, - ) %> + <%= filter_select(label: "Filter by manufacturer", scope: :from_manufacturer, collection: @manufacturers, selected: @selected_manufacturer) %>
<% end %> <% if @donation_sites.present? %>
- <%= filter_select( - label: "Filter by Donation Site", - scope: :from_donation_site, - collection: @donation_sites, - key: :id, - value: :name, - selected: @selected_donation_site, - ) %> + <%= filter_select(label: "Filter by Donation Site", scope: :from_donation_site, collection: @donation_sites, key: :id, value: :name, selected: @selected_donation_site) %>
<% end %>
<%= label_tag "Date Range" %> - <%= render partial: "shared/date_range_picker", - locals: { - css_class: "form-control", - } %> + <%= render partial: "shared/date_range_picker", locals: {css_class: "form-control"} %>
-
- +
<% end %>
- - + +
+
@@ -155,24 +115,22 @@ <%= render partial: "donation_row", collection: @paginated_donations %> - - Total - - - - - - <%= @donations_quantity %> - (Total) - -
- <%= @paginated_donations_quantity %> - (This page) - - <%= dollar_value(@total_money_raised) %> - <%= dollar_value(@total_value_all_donations) %> - - + + Total + + + + + + <%= @donations_quantity %> (Total) + +
+ <%= @paginated_donations_quantity %> (This page) + + <%= dollar_value(@total_money_raised) %> + <%= dollar_value(@total_value_all_donations) %> + +
@@ -186,4 +144,4 @@
-
+ \ No newline at end of file