crayfish 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -93,6 +93,41 @@ You can also use Prawn directly:
93
93
  %>
94
94
  ```
95
95
 
96
+ ## Testing
97
+
98
+ You can intercept the template compiler to perform view tests on the generated HTML with the :html option.
99
+
100
+ ``` Ruby
101
+ class MyPdfController < ApplicationController
102
+ attr_reader :options
103
+
104
+ def show
105
+ @options = params[:options]
106
+ @pdf = MyPdfModel.find(params[:id])
107
+ end
108
+
109
+ end
110
+ ```
111
+
112
+ ``` Ruby
113
+ class MyPdfControllerTest < ActionController::TestCase
114
+
115
+ setup do
116
+ @pdf = Factory.create :my_pdf
117
+ end
118
+
119
+ test "should show PDF" do
120
+ get :show, id: @pdf.to_param, :format => :pdf, :options => {:html => true}
121
+ assert_response :success
122
+
123
+ assert_tag :tag => 'div',
124
+ :attributes => {
125
+ :class => 'my_div'
126
+ }
127
+ end
128
+ end
129
+ ```
130
+
96
131
  ## License
97
132
 
98
133
  (The MIT License)
@@ -26,10 +26,14 @@ module Crayfish
26
26
  def render *args
27
27
  if @branch_level
28
28
  @branch_level += 1
29
- stack = @output_buffer
30
- @output_buffer = ::ActionView::OutputBuffer.new
31
- super
32
- @output_buffer = stack
29
+ if @options[:html]
30
+ super
31
+ else
32
+ stack = @output_buffer
33
+ @output_buffer = ::ActionView::OutputBuffer.new
34
+ super
35
+ @output_buffer = stack
36
+ end
33
37
  @branch_level -= 1
34
38
  else
35
39
  super
@@ -68,7 +72,7 @@ module Crayfish
68
72
  def flush paint=true
69
73
  buf = @output_buffer.to_s || ''
70
74
  paint(buf,true) if paint
71
- @output_buffer = ::ActionView::OutputBuffer.new
75
+ @output_buffer = ::ActionView::OutputBuffer.new unless @options[:html]
72
76
  buf
73
77
  end
74
78
 
data/lib/crayfish/html.rb CHANGED
@@ -188,16 +188,24 @@ module Crayfish
188
188
  elsif node.kind_of?(Prawn::Table)
189
189
  if node.post_resize
190
190
  if /^(?<size>.*)%$/ =~ node.post_resize
191
+ new_width = parent_width * size.to_f / 100.0
192
+ column_widths = node.column_widths.dup
193
+ scale = new_width / column_widths.sum
191
194
  node.instance_variable_set '@column_widths', nil
195
+
192
196
  # reset constraints
193
- node.cells.each do |cell|
194
- cell.instance_variable_set '@max_width', parent_width
197
+ (0..node.row_length).map do |n|
198
+ row = node.row(n).columns(0..-1)
199
+ row.each_with_index do |cell,i|
200
+ cell.instance_variable_set '@max_width', column_widths[i]*scale
201
+ end
195
202
  end
196
- node.width = parent_width
203
+ node.width = new_width
197
204
  node.send(:set_column_widths)
198
205
  node.send(:position_cells)
199
206
  end
200
207
  end
208
+
201
209
  column_widths = node.column_widths
202
210
  (0..node.row_length).map do |n|
203
211
 
@@ -21,5 +21,5 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
23
  module Crayfish
24
- VERSION = "0.2.0"
24
+ VERSION = "0.2.1"
25
25
  end
@@ -0,0 +1,89 @@
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require 'test_helper'
24
+
25
+ class CrayfishActionViewTest < ActiveSupport::TestCase
26
+
27
+ class Response
28
+ def content_type
29
+ 'pdf'
30
+ end
31
+
32
+ def content_type= val
33
+ end
34
+
35
+ def headers
36
+ @headers ||= {}
37
+ end
38
+ end
39
+
40
+ class ActionController
41
+ include Crayfish::ActionController
42
+
43
+ def response
44
+ @response ||= Response.new
45
+ end
46
+
47
+ def headers
48
+ response.headers
49
+ end
50
+
51
+ def options
52
+ { :html => true }
53
+ end
54
+ end
55
+
56
+ class ActionView
57
+ include Crayfish::ActionView
58
+
59
+ def controller
60
+ @controller ||= ActionController.new
61
+ end
62
+ end
63
+
64
+ def setup
65
+ @view = ActionView.new
66
+ @view.send(:setup)
67
+ end
68
+
69
+ test "forced html paint" do
70
+ assert_equal @view.send(:paint,'plain',true), 'plain'
71
+ end
72
+
73
+ test "should not flush output bufer in HTML mode" do
74
+ @view.send(:output_buffer) << "A"
75
+ @view.send(:flush)
76
+ assert_equal @view.send(:output_buffer), 'A'
77
+ end
78
+
79
+ test "should flush output bufer in PDF mode" do
80
+ ActionController.any_instance.stubs(:options).returns({})
81
+ view = ActionView.new
82
+ view.send(:setup)
83
+
84
+ view.send(:output_buffer) << "A"
85
+ view.send(:flush)
86
+ assert_equal view.send(:output_buffer), ''
87
+ end
88
+
89
+ end