From 595b819f08ff3260ded97a21a054aac58d2ec0ae Mon Sep 17 00:00:00 2001 From: Mike Phillips Date: Sun, 7 Jul 2024 13:44:33 -0500 Subject: [PATCH] 4481 Print Individual Donation Receipts Requested Changes --- Gemfile | 2 ++ Gemfile.lock | 13 ++++++++++++- app/pdfs/donation_pdf.rb | 15 +++++++-------- spec/pdfs/donation_pdf_spec.rb | 20 +++++++++++++++++++- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 6dcd1ef7d4..3d476cf224 100644 --- a/Gemfile +++ b/Gemfile @@ -199,6 +199,8 @@ group :test do gem "webmock", "~> 3.23" # Interface capybara to chrome headless gem "cuprite" + # Read PDF files for tests + gem "pdf-reader" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index b9908cfe7a..30c909b9c9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + Ascii85 (1.1.1) actioncable (7.1.3.4) actionpack (= 7.1.3.4) activesupport (= 7.1.3.4) @@ -77,6 +78,7 @@ GEM tzinfo (~> 2.0) addressable (2.8.6) public_suffix (>= 2.0.2, < 6.0) + afm (0.2.2) annotate (3.2.0) activerecord (>= 3.2, < 8.0) rake (>= 10.4, < 14.0) @@ -288,6 +290,7 @@ GEM guard-compat (~> 1.1) rspec (>= 2.99.0, < 4.0) hashdiff (1.1.0) + hashery (2.1.2) hashie (5.0.0) httparty (0.22.0) csv @@ -441,6 +444,12 @@ GEM ast (~> 2.4.1) racc pdf-core (0.9.0) + pdf-reader (2.12.0) + Ascii85 (~> 1.0) + afm (~> 0.2.1) + hashery (~> 2.0) + ruby-rc4 + ttfunk pg (1.5.6) popper_js (2.11.8) prawn (2.4.0) @@ -591,6 +600,7 @@ GEM ruby-graphviz (1.2.5) rexml ruby-progressbar (1.13.0) + ruby-rc4 (0.1.5) ruby-vips (2.1.4) ffi (~> 1.12) ruby2_keywords (0.0.5) @@ -756,6 +766,7 @@ DEPENDENCIES omniauth-rails_csrf_protection orderly (~> 0.1) paper_trail + pdf-reader pg (~> 1.5.6) prawn-rails pry-doc @@ -787,4 +798,4 @@ DEPENDENCIES webmock (~> 3.23) BUNDLED WITH - 2.5.11 + 2.5.14 diff --git a/app/pdfs/donation_pdf.rb b/app/pdfs/donation_pdf.rb index adfbe55abd..773e1b9095 100644 --- a/app/pdfs/donation_pdf.rb +++ b/app/pdfs/donation_pdf.rb @@ -6,25 +6,24 @@ class DonationPdf class DonorInfo attr_reader :name, :address, :email - def initialize(donation = nil) + def initialize(donation) if donation.nil? - @name = @address = @email = nil - return + raise "Must pass a Donation object" end case donation.source - when "Donation Site" + when Donation::SOURCES[:donation_site] @name = donation.donation_site.name @address = donation.donation_site.address @email = donation.donation_site.email - when "Manufacturer" + when Donation::SOURCES[:manufacturer] @name = donation.manufacturer.name @address = nil @email = nil - when "Product Drive" + when Donation::SOURCES[:product_drive] @name = donation.product_drive_participant.business_name @address = donation.product_drive_participant.address @email = donation.product_drive_participant.email - when "Misc. Donation" + when Donation::SOURCES[:misc] @name = "Misc. Donation" @address = nil @email = nil @@ -80,7 +79,7 @@ def compute_and_render font_size 12 money_raised = "$0.00" - if !@donation.money_raised.nil? && @donation.money_raised > 0 + if @donation.money_raised && @donation.money_raised > 0 money_raised = dollar_value(@donation.money_raised) end text "Money Raised In Dollars: #{money_raised}", inline_format: true diff --git a/spec/pdfs/donation_pdf_spec.rb b/spec/pdfs/donation_pdf_spec.rb index 5930a031f9..6f49b0ee6e 100644 --- a/spec/pdfs/donation_pdf_spec.rb +++ b/spec/pdfs/donation_pdf_spec.rb @@ -1,7 +1,7 @@ describe DonationPdf do let(:donation_site) { create(:donation_site) } let(:organization) { create(:organization) } - let(:donation) { create(:donation, organization: organization, donation_site: donation_site) } + let(:donation) { create(:donation, organization: organization, donation_site: donation_site, source: Donation::SOURCES[:donation_site]) } let(:item1) { FactoryBot.create(:item, name: "Item 1", package_size: 50, value_in_cents: 100) } let(:item2) { FactoryBot.create(:item, name: "Item 2", value_in_cents: 200) } let(:item3) { FactoryBot.create(:item, name: "Item 3", value_in_cents: 300) } @@ -44,4 +44,22 @@ ]) end end + + context "render pdf" do + it "renders correctly" do + pdf = described_class.new(organization, donation) + pdf_test = PDF::Reader.new(StringIO.new(pdf.compute_and_render)) + expect(pdf_test.page(1).text).to include(donation_site.name) + expect(pdf_test.page(1).text).to include(donation_site.address) + expect(pdf_test.page(1).text).to include(donation_site.email) + if donation.comment + expect(pdf_test.page(1).text).to include(donation.comment) + end + expect(pdf_test.page(1).text).to include("Money Raised In Dollars: $0.00") + expect(pdf_test.page(1).text).to include("Items Received") + expect(pdf_test.page(1).text).to match(/Item 1\s+\$1\.00\s+\$50\.00\s+50/) + expect(pdf_test.page(1).text).to match(/Item 2\s+\$2\.00\s+\$200\.00\s+100/) + expect(pdf_test.page(1).text).to include("Total Items Received") + end + end end