spree-contrib / spree_print_invoice

Create a PDF invoice for Spree orders.
http://guides.spreecommerce.org
BSD 3-Clause "New" or "Revised" License
91 stars 239 forks source link

Don't use Rails.application.assets #105

Open sepastian opened 8 years ago

sepastian commented 8 years ago

I have updated sprockets and now get ActionView::Template::Error (undefined methodfind_asset' for nil:NilClass)`.

Rails.application.assets is nil unless assets.compile is set, which defaults to false in production.

See https://github.com/rails/sprockets-rails/issues/237, i.e.:

So it sounds like using those gems may have been behaving poorly in production before.

mdavo6 commented 7 years ago

I had the same issue. Was able to resolve by changing the first few lines of _header.pdf.prawn to:

rel_path = Rails.application.assets_manifest.assets[Spree::PrintInvoice::Config[:logo_path]]
im = Rails.root.join("public","assets",rel_path)

if im && File.exist?(im)
  pdf.image im, vposition: :top, height: 40, scale: Spree::PrintInvoice::Config[:logo_scale]
end
aman29april commented 7 years ago

@mdavo6 This solution doesn't work in the development environment.

mdavo6 commented 5 years ago

@aman29april Agree. Have just had some time to work on a better solution which works in production and development. What about the below as an approach? Working for me in development and production.

if Rails.env.production? && 
Rails.application.assets_manifest.assets[Spree::PrintInvoice::Config[:logo_path]].present?
  manifest_dir = Rails.application.assets_manifest.dir
  logo_path = Rails.application.assets_manifest.assets[Spree::PrintInvoice::Config[:logo_path]]
  im = File.join(manifest_dir, logo_path)
elsif Rails.application.assets.find_asset(Spree::PrintInvoice::Config[:logo_path]) != nil
  im = Rails.application.assets.find_asset(Spree::PrintInvoice::Config[:logo_path]).filename
end

if im && File.exist?(im)
  pdf.image im, vposition: :top, height: 40, scale: Spree::PrintInvoice::Config[:logo_scale]
end