From 3df59ae01e3c363e6f1fa5627c641c56e3ef354b Mon Sep 17 00:00:00 2001 From: Kevin Perez Date: Thu, 4 Jan 2024 18:56:30 -0600 Subject: [PATCH 1/4] Add WickedPdf.configure method This methods uses the config block pattern to allow users to add config values without risking overwriting WickedPdf.config. --- README.md | 6 +++--- lib/wicked_pdf.rb | 11 +++++++++++ test/unit/wicked_pdf_test.rb | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bed73f9b..988a0ef2 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ You can see what flags are supported for the current version in [wkhtmltopdf's a If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer: ```ruby -WickedPdf.config = { - exe_path: '/usr/local/bin/wkhtmltopdf', - enable_local_file_access: true +WickedPdf.configure do |c| + c.exe_path = '/usr/local/bin/wkhtmltopdf', + c.enable_local_file_access = true } ``` diff --git a/lib/wicked_pdf.rb b/lib/wicked_pdf.rb index 15503275..38d477a5 100644 --- a/lib/wicked_pdf.rb +++ b/lib/wicked_pdf.rb @@ -24,6 +24,17 @@ class WickedPdf include Progress + def self.configure + config = OpenStruct.new(@@config) + yield config + + @@config.merge! config.to_h + end + + def self.clear_config + @@config = {} + end + def initialize(wkhtmltopdf_binary_path = nil) @binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION) end diff --git a/test/unit/wicked_pdf_test.rb b/test/unit/wicked_pdf_test.rb index 113db19b..40dfdef9 100644 --- a/test/unit/wicked_pdf_test.rb +++ b/test/unit/wicked_pdf_test.rb @@ -7,6 +7,23 @@ def setup @wp = WickedPdf.new end + test 'should update config through .configure class method' do + WickedPdf.configure do |c| + c.test = 'foobar' + end + + assert WickedPdf.config == { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf', test: 'foobar' } + end + + test 'should clear config through .clear_config class method' do + backup_config = WickedPdf.config + + WickedPdf.clear_config + assert WickedPdf.config == {} + + WickedPdf.config = backup_config + end + test 'should generate PDF from html document' do pdf = @wp.pdf_from_string HTML_DOCUMENT assert pdf.start_with?('%PDF-1.4') From ef566beb4551ccad4970fe945214f7efbd2dca65 Mon Sep 17 00:00:00 2001 From: Kevin Perez Date: Thu, 4 Jan 2024 18:57:33 -0600 Subject: [PATCH 2/4] Add deprecation warning for WickedPdf.config= This adds a deprecation warning for the old way to set config. Users have the option to silence the warning if they want to do so. --- lib/wicked_pdf.rb | 10 +++++++++- test/test_helper.rb | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/wicked_pdf.rb b/lib/wicked_pdf.rb index 38d477a5..ab34d959 100644 --- a/lib/wicked_pdf.rb +++ b/lib/wicked_pdf.rb @@ -20,10 +20,18 @@ class WickedPdf DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9') @@config = {} - cattr_accessor :config + cattr_accessor :config, :silence_deprecations include Progress + def self.config=(config) + unless @@silence_deprecations + ::Kernel.warn "WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead." + end + + @@config = config + end + def self.configure config = OpenStruct.new(@@config) yield config diff --git a/test/test_helper.rb b/test/test_helper.rb index 15990f93..74257052 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,6 +11,7 @@ require 'wicked_pdf' Rails.backtrace_cleaner.remove_silencers! +WickedPdf.silence_deprecations = true if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir) # Copy CSS file From 3b58fed1ab202fd03da38f8a42eec829f4590329 Mon Sep 17 00:00:00 2001 From: Kevin Perez Date: Tue, 23 Jan 2024 10:24:26 -0600 Subject: [PATCH 3/4] Fix rubocop offenses --- lib/wicked_pdf.rb | 4 +--- test/unit/wicked_pdf_test.rb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/wicked_pdf.rb b/lib/wicked_pdf.rb index ab34d959..9c08a348 100644 --- a/lib/wicked_pdf.rb +++ b/lib/wicked_pdf.rb @@ -25,9 +25,7 @@ class WickedPdf include Progress def self.config=(config) - unless @@silence_deprecations - ::Kernel.warn "WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead." - end + ::Kernel.warn 'WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead.' unless @@silence_deprecations @@config = config end diff --git a/test/unit/wicked_pdf_test.rb b/test/unit/wicked_pdf_test.rb index 40dfdef9..d5a49899 100644 --- a/test/unit/wicked_pdf_test.rb +++ b/test/unit/wicked_pdf_test.rb @@ -12,7 +12,7 @@ def setup c.test = 'foobar' end - assert WickedPdf.config == { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf', test: 'foobar' } + assert WickedPdf.config == { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf', :test => 'foobar' } end test 'should clear config through .clear_config class method' do From 7a749c297ee70c1728cc901fd6d104831d2b5aba Mon Sep 17 00:00:00 2001 From: Kevin Perez Date: Fri, 9 Feb 2024 17:13:41 -0600 Subject: [PATCH 4/4] Update initializer template with new config syntax --- generators/wicked_pdf/templates/wicked_pdf.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/generators/wicked_pdf/templates/wicked_pdf.rb b/generators/wicked_pdf/templates/wicked_pdf.rb index 37c38793..965e2dd8 100644 --- a/generators/wicked_pdf/templates/wicked_pdf.rb +++ b/generators/wicked_pdf/templates/wicked_pdf.rb @@ -8,23 +8,23 @@ # # https://github.com/mileszs/wicked_pdf/blob/master/README.md -WickedPdf.config = { +WickedPdf.configure do |config| # Path to the wkhtmltopdf executable: This usually isn't needed if using # one of the wkhtmltopdf-binary family of gems. - # exe_path: '/usr/local/bin/wkhtmltopdf', + # config.exe_path = '/usr/local/bin/wkhtmltopdf', # or - # exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf') + # config.exe_path = Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf') # Needed for wkhtmltopdf 0.12.6+ to use many wicked_pdf asset helpers - # enable_local_file_access: true, + # config.enable_local_file_access = true, # Layout file to be used for all PDFs # (but can be overridden in `render :pdf` calls) - # layout: 'pdf.html', + # config.layout = 'pdf.html', # Using wkhtmltopdf without an X server can be achieved by enabling the # 'use_xvfb' flag. This will wrap all wkhtmltopdf commands around the # 'xvfb-run' command, in order to simulate an X server. # - # use_xvfb: true, -} + # config.use_xvfb = true, +end