Skip to content

Commit

Permalink
remove html_strip method, add legitimate tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Jun 13, 2018
1 parent bc39fba commit f810fac
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 140 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Changelog

* `v1.1.0` - UNRELEASED
- Breaking Change - Remove `html_strip` helper method. Use `ActionView::Base.full_sanitizer.sanitize(html_string)` if you were using this feature.
- [#29](https://github.com/cortiz/prawn-rails/pull/29) - Require all installed prawn extensions
- Removed unnecessary `html_strip` helper method. If you were using this feature, instead use Rails built in `strip_tags(html_str)` or `ActionView::Base.full_sanitizer.sanitize(html_str)`
- Add legitimate tests

* `v1.0.1`
Expand Down
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Prawn-Rails [![Gem Version](https://badge.fury.io/rb/prawn-rails.svg)](http://badge.fury.io/rb/prawn-rails)
<a href='http://ko-fi.com/A552JBK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>

## Install
# Install
```ruby
gem 'prawn-rails'
```

Note: `prawn` and `prawn-table` are dependencies of `prawn-rails` so there is no need to mention it in the projects Gemfile unless you want to use a specific version of either of those libraries.

## Usage
# Usage
Create a view with `pdf` as format and `prawn` as handler so filename should look like `example.pdf.prawn`

It provides a helper called `prawn_document` which builds a PrawnRails::Document with default options. You can override any options as you please. Example:
Expand All @@ -29,11 +29,7 @@ def show
end
```

## Built-in Helpers
* *html_strip(html)*:
Removes the html tags from a string

## Default configuration
# Default configuration

Add a `prawn-rails.rb` config to your Rails app under `config/initializers` like this

Expand All @@ -59,7 +55,7 @@ pdf.start_new_page
pdf.start_new_page size: "A4", page_layout: :portrait
```

## Examples
# Examples

#### Hello World

Expand Down Expand Up @@ -92,12 +88,10 @@ prawn_document(page_layout: :landscape) do |pdf|
end
```

## Credits

Created by Carlos Ortiz - @cortiz

<a href='http://ko-fi.com/A552JBK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
# Credits

Maintained by Weston Ganger - @westonganger

Special thanks to @rwilliams, @sigmike, @smber1, @iffyuva
Created by Carlos Ortiz - @cortiz

<a href='http://ko-fi.com/A552JBK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
21 changes: 4 additions & 17 deletions lib/prawn-rails/document.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
require 'prawn'
require 'prawn-rails/extension'

Gem.loaded_specs.keys.select{|spec_name| spec_name.starts_with?('prawn-')}.each do |gem_name|
next if gem_name == 'prawn-rails' # Prevent circular loading

file = gem_name.gsub('-', '/')

begin
require file
rescue LoadError => _
STDERR.puts "#{__FILE__}:#{__LINE__}: warning: prawn-rails could not require plugin `#{gem_name}`"
end
end
require 'prawn-rails/load_prawn_plugins'

module PrawnRails

# This derives from Prawn::Document in order to override defaults. Note that the Prawn::Document behaviour itself shouldn't be changed.
# 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 initialize(opts = {})
if PrawnRails.config.respond_to?(:to_h)
Expand All @@ -26,8 +15,7 @@ def initialize(opts = {})
super(default)
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:
# 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
Expand All @@ -38,5 +26,4 @@ def text(value, options = {})
end
end

Document.extensions << Extension
end
48 changes: 0 additions & 48 deletions lib/prawn-rails/extension.rb

This file was deleted.

11 changes: 11 additions & 0 deletions lib/prawn-rails/load_prawn_plugins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Gem.loaded_specs.keys.select{|spec_name| spec_name.starts_with?('prawn-')}.each do |gem_name|
next if gem_name == 'prawn-rails' # Prevent circular loading

file = gem_name.gsub('-', '/')

begin
require file
rescue LoadError => _
STDERR.puts "#{__FILE__}:#{__LINE__}: warning: prawn-rails could not require plugin `#{gem_name}`"
end
end
18 changes: 12 additions & 6 deletions lib/prawn-rails/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

module PrawnRails
module RailsHelper

def prawn_document(options={})
options.reverse_merge!(page_layout: PrawnRails.config.page_layout,
page_size: PrawnRails.config.page_size,
info: {
Title: @filename.sub(/\.(p|P)(d|D)(f|F)$/, '')
})
options.reverse_merge!({
page_layout: PrawnRails.config.page_layout,
page_size: PrawnRails.config.page_size,
info: {
Title: @filename.sub(/\.(p|P)(d|D)(f|F)$/, '')
}
})

options.reverse_merge!(skip_page_creation: true) if PrawnRails.config.skip_page_creation
if PrawnRails.config.skip_page_creation
options.reverse_merge!(skip_page_creation: true)
end

pdf = PrawnRails::Document.new(options)

yield pdf if block_given?

pdf.render
end

end
end
3 changes: 3 additions & 0 deletions lib/prawn-rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module PrawnRails
class Renderer

### WARNING: BE VERY CAREFUL IF EDITING THIS METHOD
def self.call(template)
%{
@filename ||= "\#{controller.action_name}.pdf"
Expand All @@ -13,5 +15,6 @@ def self.call(template)
#{template.source.strip}
}
end

end
end
13 changes: 12 additions & 1 deletion test/dummy_app/app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
class ReportsController < ApplicationController

def sample
@items = [{:name=> "Hello"},{:name=> "World"}]
@items = [
{name: "Hello"},
{name: "World"},
]
end

def table
@items = [
[1,2,3],
[4,5,6],
[7,8,9],
]
end

end
2 changes: 0 additions & 2 deletions test/dummy_app/app/helpers/application_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions test/dummy_app/app/helpers/pdf_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions test/dummy_app/app/helpers/products_helper.rb

This file was deleted.

22 changes: 13 additions & 9 deletions test/dummy_app/app/views/reports/sample.pdf.prawn
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
prawn_document(page_layout: :portrait) do |pdf|
pdf.text "Hello There :D"
pdf.text "Hello World!"
pdf.text "now it is here: #{pdf.cursor}"
pdf.text "Items are #{@items.inspect}"
pdf.text "Number of Items: #{@items.count}"

pdf.font("Courier") do
pdf.text "Written in Courier because we are inside the block."
end

pdf.text "with html<bold></bold>"
pdf.text pdf.html_strip("without html <bold>LD</bold>")
pdf.text strip_tags("without html <bold>LD</bold>")

pdf.text "Let's see which is the current font_size: #{pdf.font_size.inspect}"
pdf.move_down 10
pdf.font_size 16
Expand All @@ -17,20 +19,22 @@ prawn_document(page_layout: :portrait) do |pdf|
pdf.move_down 10
pdf.text "Back to 16 again."
pdf.move_down 10
pdf.text "Single line on 20 using the :size option.", :size => 20
pdf.text "Single line on 20 using the :size option.", size: 20
pdf.move_down 10
pdf.text "Back to 16 once more."
pdf.move_down 10
pdf.font("Courier", :size => 10) do

pdf.font("Courier", size: 10) do
pdf.text "Yeah, using Courier 10 courtesy of the font method."
end
pdf.font("Helvetica", :size => 12) # back to normal

pdf.font("Helvetica", size: 12) # back to normal

pdf.text "Default color is black"
pdf.move_down 25
pdf.text "Changed to red", :color => "FF0000"
pdf.text "Changed to red", color: "FF0000"
pdf.move_down 25
pdf.text "CMYK color", :color => [22, 55, 79, 30]
pdf.text "CMYK color", color: [22, 55, 79, 30]
pdf.move_down 25
pdf.text "Also works with <color rgb='ff0000'>inline</color> formatting", :color => "0000FF", :inline_format => true
pdf.text "Also works with <color rgb='ff0000'>inline</color> formatting", color: "0000FF", inline_format: true
end
7 changes: 7 additions & 0 deletions test/dummy_app/app/views/reports/table.pdf.prawn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
prawn_document(page_layout: :portrait) do |pdf|

pdf.table @items do

end

end
7 changes: 7 additions & 0 deletions test/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ class Application < Rails::Application
# Enable the asset pipeline
config.assets.enabled = true

config.assets.quiet = true

# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'

config.generators.test_framework = false
config.generators.helper = false
config.generators.stylesheets = false
config.generators.javascripts = false

if ActiveRecord.respond_to?(:gem_version)
gem_version = ActiveRecord.gem_version
if gem_version >= Gem::Version.new("5.2")
Expand Down
2 changes: 1 addition & 1 deletion test/dummy_app/config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Be sure to restart your server when you modify this file.

Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
Expand Down
2 changes: 1 addition & 1 deletion test/dummy_app/config/initializers/wrap_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters :format => [:json]
wrap_parameters format: [:json]
end

# Disable root element in JSON by default.
Expand Down
3 changes: 1 addition & 2 deletions test/dummy_app/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Dummy::Application.routes.draw do
resources :products

get "reports/sample"
get "reports/table"
end
2 changes: 1 addition & 1 deletion test/dummy_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120221152352) do
ActiveRecord::Schema.define(version: 20120221152352) do

end
Loading

0 comments on commit f810fac

Please sign in to comment.