From 79f6d81d7985a1675c4da3dc6bbf188c7aff5e29 Mon Sep 17 00:00:00 2001 From: Brad Gessler Date: Wed, 28 Feb 2024 14:24:46 -0800 Subject: [PATCH] Add Zeitwerk and a helper to stream PDFs via send_pdf --- Gemfile.lock | 2 ++ lib/phlex/pdf.rb | 7 +++++++ lib/phlex/pdf/rails.rb | 13 +++++++++++++ phlex-pdf.gemspec | 6 ++---- spec/phlex/pdf_spec.rb | 6 ++++++ 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 lib/phlex/pdf/rails.rb diff --git a/Gemfile.lock b/Gemfile.lock index af661ec..7a6c93d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,7 @@ PATH phlex-pdf (0.1.2) matrix (~> 0.4) prawn (~> 2.0) + zeitwerk (~> 2.0) GEM remote: https://rubygems.org/ @@ -41,6 +42,7 @@ GEM rspec-support (3.13.0) ruby-rc4 (0.1.5) ttfunk (1.7.0) + zeitwerk (2.6.13) PLATFORMS arm64-darwin-23 diff --git a/lib/phlex/pdf.rb b/lib/phlex/pdf.rb index dff299b..0170c69 100644 --- a/lib/phlex/pdf.rb +++ b/lib/phlex/pdf.rb @@ -3,9 +3,16 @@ require_relative "pdf/version" require "prawn" require "matrix" +require "zeitwerk" module Phlex class PDF + Loader = Zeitwerk::Loader.for_gem_extension(Phlex).tap do |loader| + loader.ignore "#{__dir__}/generators" + loader.inflector.inflect("pdf" => "PDF") + loader.setup + end + include Prawn::View def document = @document diff --git a/lib/phlex/pdf/rails.rb b/lib/phlex/pdf/rails.rb new file mode 100644 index 0000000..20852de --- /dev/null +++ b/lib/phlex/pdf/rails.rb @@ -0,0 +1,13 @@ +module Phlex::PDF::Rails + module Helpers + # Sends a PDF to the browser via Rails controllers. + def send_pdf(pdf, disposition: "inline", type: "application/pdf", **kwargs) + send_data( + pdf.to_pdf, + disposition: disposition, + type: type, + **kwargs + ) + end + end +end \ No newline at end of file diff --git a/phlex-pdf.gemspec b/phlex-pdf.gemspec index 05d0448..b8d47da 100644 --- a/phlex-pdf.gemspec +++ b/phlex-pdf.gemspec @@ -35,9 +35,7 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem spec.add_dependency "prawn", "~> 2.0" spec.add_dependency "matrix", "~> 0.4" + spec.add_dependency "zeitwerk", "~> 2.0" - spec.add_development_dependency("pdf-inspector", "~> 1.0") - - # For more information and examples about making a new gem, check out our - # guide at: https://bundler.io/guides/creating_gem.html + spec.add_development_dependency "pdf-inspector", "~> 1.0" end diff --git a/spec/phlex/pdf_spec.rb b/spec/phlex/pdf_spec.rb index a6d1804..029ddda 100644 --- a/spec/phlex/pdf_spec.rb +++ b/spec/phlex/pdf_spec.rb @@ -102,3 +102,9 @@ def view_template end end end + +RSpec.describe Phlex::PDF::Loader do + it "resolves modules" do + expect { Phlex::PDF::Rails::Helpers }.not_to raise_error + end +end