elkinsware-erubis_rails_helper 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,59 @@
1
+ = erubis_rails_helper
2
+
3
+ * http://github.com/elkinsware/erubis_rails_helper/tree/master
4
+
5
+ == DESCRIPTION:
6
+
7
+ This is a fix for Erubis integration with Rails 2.3. It is a drop in replacement for the rails helper in the Erubis gem.
8
+ The fix is minor and I am submitting it to Erubis but until it is fixed this will work.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Drop in replacement for the Erubis Rails Helper in the Erubis gem.
13
+
14
+ == SYNOPSIS:
15
+
16
+ FIX (code sample of usage)
17
+ inside config/environment.rb and the following
18
+ config.gem 'erubis' , :version => '2.6.4'
19
+ config.gem 'erubis_rails_helper'
20
+
21
+ add config/initializers/erubis_config.rb
22
+ #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
23
+ #Erubis::Helpers::RailsHelper.init_properties = {}
24
+ Erubis::Helpers::RailsHelper.show_src = false
25
+ Erubis::Helpers::RailsHelper.preprocessing = true
26
+
27
+ == REQUIREMENTS:
28
+
29
+ * Rails 2.3
30
+ * Erubis 2.6.4
31
+
32
+ == INSTALL:
33
+
34
+ * sudo gem install elkinsware-erubis_rails_helper
35
+
36
+ == LICENSE:
37
+
38
+ (The MIT License)
39
+
40
+ Copyright (c) 2009 Dave Elkins (elkinsware)
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ 'Software'), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
57
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
58
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
59
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,373 @@
1
+ begin
2
+ require 'action_controller'
3
+ require 'action_view'
4
+ rescue LoadError
5
+ $stderr.puts %[This erubis_rails requires actionpack 2.2.2 or higher]
6
+ end
7
+
8
+ begin
9
+ require 'erubis'
10
+ require 'erubis/preprocessing'
11
+ rescue LoadError
12
+ $stderr.puts %[This erubis_rails requires erubis 2.6.4\n
13
+ Add "config.gem 'erubis', :version => 2.6.4" to config/environment.rb]
14
+ end
15
+
16
+ module Erubis
17
+ module OutputBufferEnhancer
18
+
19
+ def self.desc # :nodoc:
20
+ "set '@output_buffer = _buf = \"\";'"
21
+ end
22
+
23
+ def add_preamble(src)
24
+ src << "__in_erubis_template=true; @output_buffer = _buf = '';"
25
+ end
26
+
27
+ end
28
+
29
+ class Eruby
30
+ #include ErboutEnhancer # will generate '_erbout = _buf = ""; '
31
+ include OutputBufferEnhancer
32
+ end
33
+
34
+ class FastEruby
35
+ #include ErboutEnhancer # will generate '_erbout = _buf = ""; '
36
+ include OutputBufferEnhancer
37
+ end
38
+
39
+ module Helpers
40
+
41
+ ##
42
+ ## helper module for Ruby on Rails
43
+ ##
44
+ ## howto:
45
+ ##
46
+ ## 1. add the folliwng code in your 'config/environment.rb'
47
+ ##
48
+ ## require 'erubis/helpers/rails_helper'
49
+ ## #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
50
+ ## #Erubis::Helpers::RailsHelper.init_properties = {}
51
+ ## #Erubis::Helpers::RailsHelper.show_src = false # set true for debugging
52
+ ## #Erubis::Helpers::RailsHelper.preprocessing = true # set true to enable preprocessing
53
+ ##
54
+ ## 2. restart web server.
55
+ ##
56
+ ## if Erubis::Helper::Rails.show_src is true, Erubis prints converted Ruby code
57
+ ## into log file ('log/development.log' or so). if false, it doesn't.
58
+ ## if nil, Erubis prints converted Ruby code if ENV['RAILS_ENV'] == 'development'.
59
+ ##
60
+ module RailsHelper
61
+
62
+ #cattr_accessor :init_properties
63
+ @@engine_class = ::Erubis::Eruby
64
+ #@@engine_class = ::Erubis::FastEruby
65
+ def self.engine_class
66
+ @@engine_class
67
+ end
68
+ def self.engine_class=(klass)
69
+ @@engine_class = klass
70
+ end
71
+
72
+ #cattr_accessor :init_properties
73
+ @@init_properties = {}
74
+ def self.init_properties
75
+ @@init_properties
76
+ end
77
+ def self.init_properties=(hash)
78
+ @@init_properties = hash
79
+ end
80
+
81
+ #cattr_accessor :show_src
82
+ @@show_src = nil
83
+ def self.show_src
84
+ @@show_src
85
+ end
86
+ def self.show_src=(flag)
87
+ @@show_src = flag
88
+ end
89
+
90
+ #cattr_accessor :preprocessing
91
+ @@preprocessing = false
92
+ def self.preprocessing
93
+ @@preprocessing
94
+ end
95
+ def self.preprocessing=(flag)
96
+ @@preprocessing = flag
97
+ end
98
+
99
+
100
+ ## define class for backward-compatibility
101
+ class PreprocessingEruby < Erubis::PreprocessingEruby # :nodoc:
102
+ end
103
+
104
+
105
+ module TemplateConverter
106
+ ## covert eRuby string into ruby code
107
+ def _convert_template(template) # :nodoc:
108
+ #src = ::Erubis::Eruby.new(template).src
109
+ klass = ::Erubis::Helpers::RailsHelper.engine_class
110
+ properties = ::Erubis::Helpers::RailsHelper.init_properties
111
+ show_src = ::Erubis::Helpers::RailsHelper.show_src
112
+ show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
113
+ ## preprocessing
114
+ if ::Erubis::Helpers::RailsHelper.preprocessing
115
+ preprocessor = _create_preprocessor(template)
116
+ template = preprocessor.evaluate(_preprocessing_context_object())
117
+ _logger_info "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
118
+ end
119
+ ## convert into ruby code
120
+ src = klass.new(template, properties).src
121
+ #src.insert(0, '_erbout = ')
122
+ _logger_info "** Erubis: src==<<'END'\n#{src}END\n" if show_src
123
+ return src
124
+ end
125
+ def _create_preprocessor(template)
126
+ return PreprocessingEruby.new(template, :escape=>true)
127
+ end
128
+ def _preprocessing_context_object
129
+ return self
130
+ end
131
+ def _logger_info(message)
132
+ logger.info message
133
+ end
134
+ end
135
+
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+
143
+ class ActionView::Base # :nodoc:
144
+ include ::Erubis::Helpers::RailsHelper::TemplateConverter
145
+ include ::Erubis::PreprocessingHelper
146
+ private
147
+ # convert template into ruby code
148
+ def convert_template_into_ruby_code(template)
149
+ #ERB.new(template, nil, @@erb_trim_mode).src
150
+ return _convert_template(template)
151
+ end
152
+ end
153
+
154
+
155
+ require 'action_pack/version'
156
+
157
+ rails22 = false
158
+
159
+ if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
160
+
161
+
162
+ if ActionPack::VERSION::MINOR >=2 ### Rails 2.2, 2.3 or higher
163
+
164
+ rails22 = true
165
+ module ActionView
166
+ module TemplateHandlers
167
+ class ErubisHandler < TemplateHandler
168
+ include Compilable
169
+ include ::Erubis::Helpers::RailsHelper::TemplateConverter
170
+ include ::Erubis::PreprocessingHelper
171
+ def compile(template)
172
+ #src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
173
+ return _convert_template("<% __in_erb_template=true %>#{template.source}")
174
+ end
175
+ end
176
+ end
177
+ handler_klass = TemplateHandlers::ErubisHandler
178
+ Template.register_default_template_handler :erb, handler_klass
179
+ Template.register_template_handler :rhtml, handler_klass
180
+ end
181
+ module Erubis::Helpers::RailsHelper::TemplateConverter
182
+ def _logger_info(message)
183
+ #logger.info message # logger.info seems not available in Rails 2.2
184
+ ActionController::Base.new.logger.info message
185
+ end
186
+ end
187
+
188
+ elsif ActionPack::VERSION::MINOR >=1 ### Rails 2.1
189
+
190
+ module ActionView
191
+ module TemplateHandlers # :nodoc:
192
+ class ErubisHandler < TemplateHandler
193
+ include Compilable
194
+ include Erubis::Helpers::RailsHelper::TemplateConverter
195
+ include Erubis::PreprocessingHelper
196
+ #
197
+ def compile(template)
198
+ return _convert_template(template.source) # template.is_a?(ActionView::Template)
199
+ end
200
+ def logger #:nodoc:
201
+ return @view.controller.logger
202
+ end
203
+ def _preprocessing_context_object #:nodoc:
204
+ return @view.controller.instance_variable_get('@template')
205
+ end
206
+ #
207
+ def cache_fragment(block, name = {}, options = nil) #:nodoc:
208
+ @view.fragment_for(block, name, options) do
209
+ #eval(ActionView::Base.erb_variable, block.binding)
210
+ eval('_buf', block.binding)
211
+ end
212
+ end
213
+ end
214
+ end
215
+ handler_klass = TemplateHandlers::ErubisHandler
216
+ Template.register_default_template_handler :erb, handler_klass
217
+ Template.register_template_handler :rhtml, handler_klass
218
+ end
219
+
220
+ elsif ActionPack::VERSION::TINY >= 2 ### Rails 2.0.X (X >= 2)
221
+
222
+ module ActionView
223
+ module TemplateHandlers # :nodoc:
224
+ class ErubisHandler < TemplateHandler
225
+ include Erubis::Helpers::RailsHelper::TemplateConverter
226
+ include Erubis::PreprocessingHelper
227
+ def compile(template)
228
+ return _convert_template(template) # template.is_a?(String)
229
+ end
230
+ def logger #:nodoc:
231
+ return @view.controller.logger
232
+ end
233
+ def _preprocessing_context_object #:nodoc:
234
+ return @view.controller.instance_variable_get('@template')
235
+ end
236
+ end
237
+ end
238
+ Base.class_eval do
239
+ handler_klass = TemplateHandlers::ErubisHandler
240
+ register_default_template_handler :erb, handler_klass
241
+ register_template_handler :rhtml, handler_klass
242
+ end
243
+ end
244
+
245
+ else ### Rails 2.0.0 or 2.0.1
246
+
247
+ class ActionView::Base # :nodoc:
248
+ private
249
+ # Method to create the source code for a given template.
250
+ def create_template_source(extension, template, render_symbol, locals)
251
+ if template_requires_setup?(extension)
252
+ body = case extension.to_sym
253
+ when :rxml, :builder
254
+ content_type_handler = (controller.respond_to?(:response) ? "controller.response" : "controller")
255
+ "#{content_type_handler}.content_type ||= Mime::XML\n" +
256
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
257
+ template +
258
+ "\nxml.target!\n"
259
+ when :rjs
260
+ "controller.response.content_type ||= Mime::JS\n" +
261
+ "update_page do |page|\n#{template}\nend"
262
+ end
263
+ else
264
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
265
+ body = convert_template_into_ruby_code(template)
266
+ end
267
+ #
268
+ @@template_args[render_symbol] ||= {}
269
+ locals_keys = @@template_args[render_symbol].keys | locals
270
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
271
+ #
272
+ locals_code = ""
273
+ locals_keys.each do |key|
274
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
275
+ end
276
+ #
277
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
278
+ end
279
+ end
280
+
281
+ end #if
282
+
283
+
284
+ else ### Rails 1.X
285
+
286
+
287
+ if ActionPack::VERSION::MINOR > 12 ### Rails 1.2
288
+
289
+ class ActionView::Base # :nodoc:
290
+ private
291
+ # Create source code for given template
292
+ def create_template_source(extension, template, render_symbol, locals)
293
+ if template_requires_setup?(extension)
294
+ body = case extension.to_sym
295
+ when :rxml
296
+ "controller.response.content_type ||= 'application/xml'\n" +
297
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
298
+ template
299
+ when :rjs
300
+ "controller.response.content_type ||= 'text/javascript'\n" +
301
+ "update_page do |page|\n#{template}\nend"
302
+ end
303
+ else
304
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
305
+ body = convert_template_into_ruby_code(template)
306
+ end
307
+ #
308
+ @@template_args[render_symbol] ||= {}
309
+ locals_keys = @@template_args[render_symbol].keys | locals
310
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
311
+ #
312
+ locals_code = ""
313
+ locals_keys.each do |key|
314
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
315
+ end
316
+ #
317
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
318
+ end
319
+ end
320
+
321
+ else ### Rails 1.1
322
+
323
+ class ActionView::Base # :nodoc:
324
+ private
325
+ # Create source code for given template
326
+ def create_template_source(extension, template, render_symbol, locals)
327
+ if template_requires_setup?(extension)
328
+ body = case extension.to_sym
329
+ when :rxml
330
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
331
+ "@controller.headers['Content-Type'] ||= 'application/xml'\n" +
332
+ template
333
+ when :rjs
334
+ "@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
335
+ "update_page do |page|\n#{template}\nend"
336
+ end
337
+ else
338
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
339
+ body = convert_template_into_ruby_code(template)
340
+ end
341
+ #
342
+ @@template_args[render_symbol] ||= {}
343
+ locals_keys = @@template_args[render_symbol].keys | locals
344
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
345
+ #
346
+ locals_code = ""
347
+ locals_keys.each do |key|
348
+ locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
349
+ end
350
+ #
351
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
352
+ end
353
+ end
354
+
355
+ end #if
356
+
357
+ ## make h() method faster (only for Rails 1.X)
358
+ module ERB::Util # :nodoc:
359
+ ESCAPE_TABLE = { '&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;', '"'=>'&quot;', "'"=>'&#039;', }
360
+ def h(value)
361
+ value.to_s.gsub(/[&<>"]/) {|s| ESCAPE_TABLE[s] }
362
+ end
363
+ module_function :h
364
+ end
365
+
366
+ end ###
367
+
368
+
369
+ ## finish
370
+ if defined?(Rails) and defined?(ActionController)
371
+ ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}"
372
+ end
373
+ $stdout.puts "** Erubis #{::Erubis::VERSION}....rails22=#{rails22}" if rails22
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestErubisRailsHelper < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/erubis_rails_helper'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elkinsware-erubis_rails_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Dave Elkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.2.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: erubis
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.2
34
+ version:
35
+ description: Drop in replacement for the Rails integration in the Erubis gem so that Erubis will work with Rails 2.3.
36
+ email: dave@elkinsware.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - lib/erubis_rails_helper.rb
45
+ - README.rdoc
46
+ has_rdoc: true
47
+ homepage: http://github.com/elkinsware/erubis_rails_helper/tree/master
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Drop in replacement for the Rails integration in the Erubis gem so that Erubis will work with Rails 2.3.
72
+ test_files:
73
+ - test/test_erubis_rails_helper.rb
74
+ - test/test_helper.rb