diff --git a/app/helpers/distributions_helper.rb b/app/helpers/distributions_helper.rb deleted file mode 100644 index d845c8cb48..0000000000 --- a/app/helpers/distributions_helper.rb +++ /dev/null @@ -1,9 +0,0 @@ -module DistributionsHelper - def logo_file_path(organization = nil) - if organization&.logo&.attached? - organization.logo.path - else - Rails.root.join("app", "assets", "images", "DiaperBase-Logo.png") - end - end -end diff --git a/app/models/organization.rb b/app/models/organization.rb index 7f455dd065..cd71dba799 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -17,6 +17,8 @@ # class Organization < ApplicationRecord + DIAPER_APP_LOGO = Rails.root.join("app", "assets", "images", "DiaperBase-Logo.png") + validates :name, presence: true validates :short_name, presence: true, format: /\A[a-z0-9_]+\z/i validates :url, format: { with: URI::DEFAULT_PARSER.make_regexp, message: "it should look like 'http://www.example.com'" }, allow_blank: true @@ -95,6 +97,14 @@ def self.seed_items(org) org.reload end + def logo_path + if logo.attached? + ActiveStorage::Blob.service.send(:path_for, logo.key).to_s + else + Organization::DIAPER_APP_LOGO.to_s + end + end + private def correct_logo_mime_type diff --git a/app/views/distributions/print.pdf.prawn b/app/views/distributions/print.pdf.prawn index 46dbadb3f4..e0d7d1f5d4 100644 --- a/app/views/distributions/print.pdf.prawn +++ b/app/views/distributions/print.pdf.prawn @@ -1,5 +1,5 @@ prawn_document do |pdf| - pdf.image logo_file_path(current_organization), height: 110 + pdf.image current_organization.logo_path, fit: [325, 110] pdf.bounding_box [pdf.bounds.right - 225, pdf.bounds.top - 20], width: 225 do pdf.text current_organization.name, align: :right @@ -117,7 +117,7 @@ prawn_document do |pdf| logo_offset = (pdf.bounds.width - 190) / 2 pdf.bounding_box([logo_offset, 0], width: 190, height: 33) do pdf.text "Lovingly created with", valign: :center - pdf.image logo_file_path, width: 75, vposition: :center, position: :right + pdf.image Organization::DIAPER_APP_LOGO, width: 75, vposition: :center, position: :right end end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index d39477104b..965eda9779 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -75,4 +75,19 @@ expect(organization.total_inventory).to eq(0) end end + + describe "logo_path" do + it "returns the the default logo path when no logo attached" do + org = build(:organization, logo: nil) + expect(org.logo_path).to include("app/assets/images/DiaperBase-Logo.png") + end + + it "returns the logo path attached for the organization" do + org = build(:organization, + logo: Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/logo.jpg"), + "image/jpeg")) + + expect(org.logo_path).to include(Rails.root.join("tmp/storage").to_s) + end + end end