Skip to content

Commit

Permalink
Add WickedPdf.configure method
Browse files Browse the repository at this point in the history
This methods uses the config block pattern to allow users to add config
values without risking overwriting WickedPdf.config.
  • Loading branch information
kevinnio committed Jan 23, 2024
1 parent 9221f6d commit 3df59ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
11 changes: 11 additions & 0 deletions lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/unit/wicked_pdf_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 3df59ae

Please sign in to comment.