crayfish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/MIT-LICENSE +21 -0
  2. data/README.md +115 -0
  3. data/Rakefile +35 -0
  4. data/lib/crayfish.rb +53 -0
  5. data/lib/crayfish/action_controller.rb +35 -0
  6. data/lib/crayfish/action_view.rb +110 -0
  7. data/lib/crayfish/elements/cell_helper.rb +70 -0
  8. data/lib/crayfish/elements/container.rb +64 -0
  9. data/lib/crayfish/elements/form.rb +161 -0
  10. data/lib/crayfish/elements/row.rb +70 -0
  11. data/lib/crayfish/elements/table.rb +51 -0
  12. data/lib/crayfish/rails/base.rb +36 -0
  13. data/lib/crayfish/rails/pdf.rb +47 -0
  14. data/lib/crayfish/version.rb +3 -0
  15. data/lib/tasks/crayfish_tasks.rake +4 -0
  16. data/test/crayfish_test.rb +227 -0
  17. data/test/dummy/README.rdoc +261 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +15 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/test/dummy/app/controllers/application_controller.rb +3 -0
  22. data/test/dummy/app/helpers/application_helper.rb +2 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +56 -0
  26. data/test/dummy/config/boot.rb +10 -0
  27. data/test/dummy/config/database.yml +25 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/development.rb +37 -0
  30. data/test/dummy/config/environments/production.rb +67 -0
  31. data/test/dummy/config/environments/test.rb +37 -0
  32. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy/config/initializers/inflections.rb +15 -0
  34. data/test/dummy/config/initializers/mime_types.rb +5 -0
  35. data/test/dummy/config/initializers/secret_token.rb +7 -0
  36. data/test/dummy/config/initializers/session_store.rb +8 -0
  37. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy/config/locales/en.yml +5 -0
  39. data/test/dummy/config/routes.rb +58 -0
  40. data/test/dummy/db/test.sqlite3 +0 -0
  41. data/test/dummy/log/test.log +2 -0
  42. data/test/dummy/public/404.html +26 -0
  43. data/test/dummy/public/422.html +26 -0
  44. data/test/dummy/public/500.html +25 -0
  45. data/test/dummy/public/favicon.ico +0 -0
  46. data/test/dummy/script/rails +6 -0
  47. data/test/test_helper.rb +32 -0
  48. metadata +174 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright 2012 Bingoentrepenøren AS
2
+ Copyright 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.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Crayfish
2
+
3
+ PDF templating for Rails.
4
+
5
+ ## Installation
6
+
7
+ ``` ruby
8
+ gem install crayfish
9
+ ```
10
+
11
+ Prawn havent been gemified for a while, so you need our latest snapshot.
12
+ Add this to your Gemfile:
13
+
14
+ ``` Ruby
15
+ gem 'prawn', :git => 'git://github.com/teknobingo/prawn.git', :branch => 'master'
16
+ ```
17
+
18
+ ## Example (say app/views/main/show.pdf.crayfish):
19
+
20
+ ``` ruby
21
+ <%
22
+ @location = { :name => 'GitHub', :zip_code => '0000', :city => 'Internet' }
23
+
24
+ form %Q{
25
+ Apples %c{ }x%c{ } boxes | =%c{ }
26
+ Pears %c{ }+ bananas%c{ } | =%c{ }
27
+ }, :title => 'Sold in kjosk %c{ }'
28
+
29
+ move_down 6
30
+ %>
31
+
32
+ <% form do |f| %>
33
+ <% f.heading 'Sold in kjosk '+f.field(' ') %>
34
+ Apples <%= f.field(' ') %>x<%= f.field(' ') %> boxes <%= f.span %> =<%= f.field(' ') %>
35
+ Pears <%= f.field(' ') %>+ bananas<%= f.field(' ') %> <%= f.span %> =<%= f.field(' ') %>
36
+ <% end -%>
37
+
38
+ <% table do |f| %>
39
+ <% f.field :label => 'Repport (dd.mm.YYYY)', :value => Date.today %>
40
+ <% end -%>
41
+
42
+ <% table do |t| %>
43
+ <% t.row do |r| %>
44
+ <% r.label 'Permit'; r.span; r.text ' ' %>
45
+ <% r.field :label => 'Time', :value => ' ' %>
46
+ <% end -%>
47
+ <% t.row do |r| %>
48
+ <% r.label 'Cash'; r.span; r.text ' ' %>
49
+ <% end -%>
50
+ <% t.row do |r| %>
51
+ <% r.label 'Organizer'; r.span; r.text 'Crayfish' %>
52
+ <% end -%>
53
+ <% t.row_for @location do |r| %>
54
+ <% r.field :label => 'Location' , :value => @location[:name] %>
55
+ <% r.field :zip_code %>
56
+ <% r.field :city %>
57
+ <% end -%>
58
+ <% end -%>
59
+
60
+ <% form do |f| %>
61
+ <% f.row do |r| %>
62
+ <% r.label 'Permit'; r.span; r.text ' ' %>
63
+ <% r.field :label => 'Time', :value => ' ' %>
64
+ <% end -%>
65
+ <% f.row do |r| %>
66
+ <% r.label 'Cash'; r.span; r.text ' ' %>
67
+ <% end -%>
68
+ <% f.row do |r| %>
69
+ <% r.label 'Organizer'; r.span; r.text 'Crayfish' %>
70
+ <% end -%>
71
+ <% f.row_for @location do |r| %>
72
+ <% r.field :label => 'Location' , :value => @location[:name] %>
73
+ <% r.field :zip_code %>
74
+ <% r.field :city %>
75
+ <% end -%>
76
+ <% end -%>
77
+ ```
78
+
79
+ Which gives:
80
+
81
+ ![](http://github.com/patrickhno/crayfish/raw/master/doc/example.png)
82
+
83
+ You could also use Prawn directly if you want to:
84
+
85
+ ``` Ruby
86
+ <% table [
87
+ [
88
+ { :content => 'Permit', :background_color => color},
89
+ make_table([[' ', { :content => 'Time', :background_color => color}]])],
90
+ [{ :content => 'Cash', :background_color => color}, ''],
91
+ [{ :content => 'Organizer', :background_color => color}, 'Crayfish'],
92
+ [ { :content => make_table([
93
+ [
94
+ { :content => 'Location', :background_color => color}, location[:name],
95
+ { :content => 'Zip Code', :background_color => color}, location[:zip_code],
96
+ { :content => 'City', :background_color => color}, location[:city]]
97
+ ]), :colspan => 2
98
+ }
99
+ ]
100
+ ], :width => 540
101
+ %>
102
+ ```
103
+
104
+ ## License
105
+
106
+ (The MIT License)
107
+
108
+ Copyright (c) 2012 Bingoentrepenøren AS
109
+ Copyright (c) 2012 Patrick Hanevold
110
+
111
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
112
+
113
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
114
+
115
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Crayfish'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.md')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ require 'rake/testtask'
26
+
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+
35
+ task :default => :test
data/lib/crayfish.rb ADDED
@@ -0,0 +1,53 @@
1
+ # Copyright (c) 2012 Bingoentrepenø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 'action_controller'
24
+ require 'action_view'
25
+
26
+ require 'prawn'
27
+ begin
28
+ require "prawn/layout"
29
+ rescue LoadError
30
+ end
31
+
32
+ require 'crayfish/action_controller'
33
+ require 'crayfish/action_view'
34
+ require 'crayfish/rails/pdf'
35
+ require 'crayfish/rails/base'
36
+ require 'crayfish/elements/container.rb'
37
+ require 'crayfish/elements/cell_helper.rb'
38
+ require 'crayfish/elements/row.rb'
39
+ require 'crayfish/elements/table.rb'
40
+ require 'crayfish/elements/form.rb'
41
+
42
+ class ActionController::Base
43
+ include Crayfish::ActionController
44
+ end
45
+
46
+ class ActionView::Base
47
+ include Crayfish::ActionView
48
+ end
49
+
50
+ module ActionView
51
+ Mime::Type.register "application/pdf", :pdf
52
+ ActionView::Template.register_template_handler 'crayfish', Crayfish::Rails::Base
53
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright (c) 2012 Bingoentrepenø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
+ module Crayfish
24
+ module ActionController
25
+
26
+ private
27
+
28
+ def options
29
+ { :inline => true, :prawn => {} }
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+
@@ -0,0 +1,110 @@
1
+ # Copyright (c) 2012 Bingoentrepenø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 'erubis'
24
+
25
+ module Crayfish
26
+ module ActionView
27
+
28
+ def render *args
29
+ if @branch_level
30
+ @branch_level += 1
31
+ super
32
+ @branch_level -= 1
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def setup
41
+ @branch_level ||= 0
42
+ if @branch_level == 0
43
+ @options = Crayfish::Rails::PDF.new(controller).options
44
+ @pdf = Prawn::Document.new(@options[:prawn])
45
+ end
46
+ end
47
+
48
+ def paint template
49
+ begin
50
+ erb = Erubis::Eruby.new(template,:bufvar=>'@_out_buf')
51
+ eval(erb.src)
52
+ rescue => e
53
+ no = 0
54
+ ::Rails.logger.debug erb.src.split("\n").map{ |line| no+=1; "#{no}: #{line}" }.join("\n")
55
+ ::Rails.logger.debug e.message
56
+ ::Rails.logger.debug e.backtrace.join("\n")
57
+ end
58
+ @pdf.render if @branch_level == 0
59
+ end
60
+
61
+ def flush paint=true
62
+ buf = @_out_buf || ''
63
+ @pdf.text buf if paint
64
+ @_out_buf = ''
65
+ buf
66
+ end
67
+
68
+ def method_missing(meth, *args, &block)
69
+ if @pdf.respond_to?(meth)
70
+ flush
71
+ @pdf.send(meth,*args)
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ def form *args, &block
78
+ flush
79
+ if block_given?
80
+ pdf = CrayForm.new(self,@pdf)
81
+ block.call pdf
82
+ pdf.draw @_out_buf
83
+ else
84
+ form = args[0] || ''
85
+ options = args[1] || {}
86
+
87
+ pdf = CrayForm.new(self,@pdf,options.merge(:span => /\|/, :element => /%c{[^}]*}/))
88
+ pdf.heading options[:title] if options[:title]
89
+ pdf.send(:form_body,form,options)
90
+ pdf.draw @_out_buf
91
+ end
92
+ @_out_buf = ''
93
+ end
94
+
95
+ def table *args, &block
96
+ if block_given?
97
+ flush
98
+
99
+ pdf = CrayTable.new(self,@pdf)
100
+ block.call pdf
101
+ pdf.draw @_out_buf
102
+ else
103
+ flush
104
+ @pdf.table *args
105
+ end
106
+ end
107
+
108
+ end
109
+ end
110
+
@@ -0,0 +1,70 @@
1
+ # Copyright (c) 2012 Bingoentrepenø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
+ module Crayfish
24
+
25
+ class CellHelper < Prawn::Table::Cell::Text
26
+
27
+ def initialize cell, options={}
28
+ @pdf = cell.instance_variable_get('@pdf')
29
+ @type = options[:type] || :text
30
+ super @pdf,cell.instance_variable_get('@point'),{ :content => cell.instance_variable_get('@content') }
31
+ end
32
+
33
+ def width val=nil
34
+ if val
35
+ self.width = val
36
+ self
37
+ else
38
+ super()
39
+ end
40
+ end
41
+
42
+ def draw_content
43
+ if @type == :field
44
+ x = -5.25 # FIXME: guesswork
45
+ y = @pdf.cursor + padding[1] - (@pdf.font.line_gap + @pdf.font.descender)/2 + 3 # FIXME: guesswork
46
+ w = spanned_content_width + FPTolerance + padding[2] + padding[3]
47
+ h = @pdf.font.height + padding[0] + padding[1]
48
+
49
+ old = @pdf.fill_color
50
+ @pdf.fill_color = 'ffffff'
51
+ @pdf.rectangle [x, y], w, h
52
+ @pdf.fill
53
+ @pdf.fill_color = old
54
+
55
+ @pdf.stroke_line([x, y], [w+x, y])
56
+ @pdf.stroke_line([w+x, y], [w+x, y-h])
57
+ @pdf.stroke_line([w+x, y-h],[x, y-h])
58
+ @pdf.stroke_line([x, y-h],[x, y])
59
+ end
60
+
61
+ super
62
+ end
63
+
64
+ def inspect
65
+ "<CellHelper \"#{content}\">"
66
+ end
67
+
68
+ end
69
+
70
+ end