Skip to content

Commit

Permalink
Improve tests and cleanup unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Sep 16, 2022
1 parent 1df906e commit 7c66aad
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
gemfile: gemfiles/rails_6.0.sqlite3.gemfile
- ruby: 2.6
gemfile: gemfiles/rails_6.1.sqlite3.gemfile
- ruby: "3.0"
- ruby: "3.1"
gemfile: gemfiles/rails_7.0.sqlite3.gemfile

env:
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changelog

* `Unreleased` - [View Diff](https://github.com/cortiz/prawn-rails/compare/v1.4.2...master)
- Nothing yet
- Remove unnecessary method `get_metadata` from `PrawnRailsHelper` which was getting included into `ActionView::Base`
- Remove unnecessary rescue in `PrawnRails.config`
- Remove redundant method `PrawnRails::Document.extensions`

* `v1.4.2` - [View Diff](https://github.com/cortiz/prawn-rails/compare/v1.4.1...v1.4.2)
- Remove dependency on `railsties` and instead only depend on `actionview`
Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
source "http://rubygems.org"

gemspec

gem 'appraisal'
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ prawn_document do |pdf|
pdf.table @products.collect{|p| [p.name,p.price]}
end
```

#### Using Custom options

```ruby
Expand All @@ -113,6 +114,19 @@ prawn_document(page_layout: :landscape) do |pdf|
end
```

#### Without using the prawn_document helper

```ruby
doc = PrawnRails::Document.new(page_layout: :landscape)

doc.text "Page 1"
doc.start_new_page
doc.text "Page 2"

pdf_str = doc.render
```


# Credits

Maintained by Weston Ganger - @westonganger
Expand Down
9 changes: 2 additions & 7 deletions lib/prawn-rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ module PrawnRails

@config = OpenStruct.new(page_layout: :portrait, page_size: "A4", skip_page_creation: false)

def config
begin
block_given? ? yield(@config) : @config
rescue => e
puts e
puts e.backtrace
end
def config(&block)
block_given? ? yield(@config) : @config
end

end
24 changes: 5 additions & 19 deletions lib/prawn-rails/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,19 @@
require 'prawn/table'

module PrawnRails

# This derives from Prawn::Document in order to override defaults.
# Note that the Prawn::Document behaviour itself shouldn't be changed.
class Document < Prawn::Document
def self.extensions
Prawn::Document.extensions
end

def initialize(options = {})
if PrawnRails.config.respond_to?(:to_h)
options.reverse_merge!(PrawnRails.config.to_h)
else
options.reverse_merge!(PrawnRails.config.marshal_dump)
end
options = PrawnRails.config.marshal_dump.merge(options) ### For Ruby 1.9.3 support, use `marshal_dump` instead of `to_h`

super(options)
end

# Typically text expects a string. But Rails views have this interesting concept that they implicitly call `to_s` on all the variables before rendering. So, passing an integer to text fails:
#
# pdf.text 10 #=> fails because 10 is not a string
# pdf.text 10.to_s #=> works
#
# To circumvent this situation, we call to_s on value, and delegate action to actual Prawn::Document.
def text(value, options = {})
# Typically text expects a string. But Rails views have this interesting concept that they implicitly call `to_s` on all the variables before rendering. So, passing an integer to text fails:
# pdf.text 10 #=> fails because 10 is not a string
# pdf.text 10.to_s #=> works
# To circumvent this situation, we call to_s on value, and delegate action to actual Prawn::Document
super(value.to_s, options)
end
end

end
18 changes: 7 additions & 11 deletions lib/prawn-rails/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ module RailsHelper
def prawn_document(options={})
@filename ||= options[:filename]

options.reverse_merge!(get_metadata)
metadata = {}

if @filename
metadata[:info] = { Title: @filename.sub(/\.pdf$/i, '') }
end

options.reverse_merge!(metadata)

pdf = PrawnRails::Document.new(options)

Expand All @@ -24,15 +30,5 @@ def prawn_document(options={})
return pdf.render
end

def get_metadata
return {} unless @filename

{
info: {
Title: @filename.sub(/\.pdf$/i, '')
}
}
end

end
end
5 changes: 5 additions & 0 deletions prawn-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Gem::Specification.new do |s|
s.add_development_dependency "rails"
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'appraisal'
s.add_development_dependency 'pry'

if RUBY_VERSION.to_f >= 3.1
s.add_development_dependency 'matrix'
end

if RUBY_VERSION.to_f >= 2.4
s.add_development_dependency 'warning'
Expand Down
4 changes: 4 additions & 0 deletions test/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

module Dummy
class Application < Rails::Application
if Rails::VERSION::STRING.to_f >= 5.1
config.load_defaults Rails::VERSION::STRING.to_f
end

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
5 changes: 0 additions & 5 deletions test/dummy_app/config/initializers/prawn-rails.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
### FOR RAILS TESTS

require 'test_helper'
require 'pdf/reader'

class NavigationTest < ActionDispatch::IntegrationTest
fixtures :all

def confirm_pdf_format(source)
reader = PDF::Reader.new(StringIO.new(source))
Expand All @@ -15,38 +11,34 @@ def confirm_pdf_format(source)
assert_not page_str.include?("<h1>Hello World!</h1>")
end

test "Registers :pdf mime type" do
test "registers :pdf mime type" do
assert Mime::Type.lookup_by_extension(:pdf)
end

test "Registers :prawn template handler" do
test "registers :prawn template handler" do
assert ActionView::Template::Handlers.extensions.include?(:prawn)
end

test "Renders html action" do
test "renders html action" do
get '/reports/sample'
assert_response :success
assert_match("<h1>Hello World!</h1>", @response.body)
end

test "Renders pdf to string" do
if RUBY_VERSION.to_f >= 2.7
skip "Test failing, couldnt figure it out, PR wanted"
end

pdf_str = ApplicationController.new.render_to_string("reports/sample.pdf", locals: {:@items => []})
test "renders pdf to string" do
pdf_str = ApplicationController.render(template: "reports/sample", formats: [:pdf], assigns: {:items => []})

confirm_pdf_format(pdf_str)
end

test "Renders sample pdf action" do
test "renders sample pdf action" do
get '/reports/sample', params: {format: :pdf}
assert_response :success

confirm_pdf_format(@response.body)
end

test "Renders table pdf action using auto-required plugin Prawn-Table" do
test "renders table pdf action using auto-required plugin prawn-table" do
get '/reports/table', params: {format: :pdf}
assert_response :success

Expand All @@ -60,23 +52,23 @@ def confirm_pdf_format(source)
assert_equal(page_str, "1 2 3\n\n4 5 6\n\n7 8 9")
end

test "Sets file name from '@filename' when present" do
test "sets file name from '@filename' when present" do
get '/reports/ivar_filename.pdf'

disposition_header = @response.headers["Content-Disposition"]
assert disposition_header.include?("attachment")
assert disposition_header.include?("ivar-filename.pdf")
end

test "Maintains existing 'Content-Disposition' header" do
test "maintains existing 'Content-Disposition' header" do
get '/reports/custom_headers.pdf'

disposition_header = @response.headers["Content-Disposition"]
assert disposition_header.include?("attachment")
assert disposition_header.include?("custom-headers.pdf")
end

test "Respects the 'filename' option alone" do
test "respects the 'filename' option alone" do
get '/reports/custom_filename.pdf'

disposition_header = @response.headers["Content-Disposition"]
Expand All @@ -92,7 +84,7 @@ def confirm_pdf_format(source)
assert_not disposition_header.include?("filename")
end

test "Respects both options on 'prawn-document' together" do
test "respects both options on 'prawn-document' together" do
get '/reports/custom.pdf'

disposition_header = @response.headers["Content-Disposition"]
Expand Down
28 changes: 0 additions & 28 deletions test/prawn_rails_test.rb

This file was deleted.

11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
Warning.ignore(
%r{mail/parsers/address_lists_parser}, ### Hide mail gem warnings
)

Warning.ignore(
%r{pdf/reader/font.* assigned but unused variable}, ### Hide pdf/reader gem warnings
)
rescue LoadError
# Do nothing
end

if RUBY_VERSION.to_f >= 2.7
require "matrix"
end

require File.expand_path("../dummy_app/config/environment.rb", __FILE__)
require "rails/test_help"

Rails.backtrace_cleaner.remove_silencers!

require 'pry'
require 'pdf/reader'
34 changes: 34 additions & 0 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'test_helper'

class ConfigTest < ActiveSupport::TestCase

def setup
@original_config = PrawnRails.config.to_h
end

def teardown
PrawnRails.config do |config|
config.to_h.keys.each do |key|
config.delete_field(key)
end

@original_config.to_h.each do |k,v|
config[k] = v
end
end
end

test "has a default config" do
assert_equal PrawnRails.config.to_h, {page_layout: :portrait, page_size: "A4", skip_page_creation: false}
end

test "config can be set with block syntax" do
PrawnRails.config do |config|
config.page_layout = :landscape
config.page_size = "A8"
end

assert_equal PrawnRails.config.to_h, {page_layout: :landscape, page_size: "A8", skip_page_creation: false}
end

end
Loading

0 comments on commit 7c66aad

Please sign in to comment.