Skip to content

Commit

Permalink
Add Zeitwerk and a helper to stream PDFs via send_pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
bradgessler committed Feb 28, 2024
1 parent 0cc6ed2 commit 79f6d81
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
phlex-pdf (0.1.2)
matrix (~> 0.4)
prawn (~> 2.0)
zeitwerk (~> 2.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/phlex/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/phlex/pdf/rails.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions phlex-pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/phlex/pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 79f6d81

Please sign in to comment.