Skip to content
This repository
Browse code

* Pass around handler instances, not their classes [Koz]

 * Move compilation, rendering and 'compilable?' checks into the Handlers [Koz]
 * Remove delegate_* methods as the handler is now an instance [Koz]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8624 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e6de95889dd361359bcd885c9f38087f14f57628 1 parent feea0f1
Michael Koziarski NZKoz authored
24 actionpack/lib/action_view/base.rb
@@ -252,7 +252,7 @@ def self.register_default_template_handler(extension, klass)
252 252 @@default_template_handlers = klass
253 253 end
254 254
255   - def self.handler_for_extension(extension)
  255 + def self.handler_class_for_extension(extension)
256 256 (extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers
257 257 end
258 258
@@ -361,13 +361,13 @@ def render(options = {}, old_local_assigns = {}, &block) #:nodoc:
361 361 # Renders the +template+ which is given as a string as either erb or builder depending on <tt>template_extension</tt>.
362 362 # The hash in <tt>local_assigns</tt> is made available as local variables.
363 363 def render_template(template_extension, template, file_path = nil, local_assigns = {}) #:nodoc:
364   - handler = self.class.handler_for_extension(template_extension)
  364 + handler = self.class.handler_class_for_extension(template_extension).new(self)
365 365
366   - if template_handler_is_compilable?(handler)
  366 + if handler.compilable?
367 367 compile_and_render_template(handler, template, file_path, local_assigns)
368 368 else
369 369 template ||= read_template_file(file_path, template_extension) # Make sure that a lazyily-read template is loaded.
370   - delegate_render(handler, template, local_assigns)
  370 + handler.render(template, local_assigns)
371 371 end
372 372 end
373 373
@@ -513,18 +513,6 @@ def evaluate_assigns
513 513 end
514 514 end
515 515
516   - def delegate_render(handler, template, local_assigns)
517   - handler.new(self).render(template, local_assigns)
518   - end
519   -
520   - def delegate_compile(handler, template)
521   - handler.new(self).compile(template)
522   - end
523   -
524   - def template_handler_is_compilable?(handler)
525   - handler.new(self).respond_to?(:compile)
526   - end
527   -
528 516 # Assigns instance variables from the controller to the view.
529 517 def assign_variables_from_controller
530 518 @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
@@ -565,7 +553,7 @@ def template_changed_since?(file_name, compile_time)
565 553
566 554 # Method to create the source code for a given template.
567 555 def create_template_source(handler, template, render_symbol, locals)
568   - body = delegate_compile(handler, template)
  556 + body = handler.compile(template)
569 557
570 558 @@template_args[render_symbol] ||= {}
571 559 locals_keys = @@template_args[render_symbol].keys | locals
@@ -585,7 +573,7 @@ def assign_method_name(handler, template, file_name)
585 573 end
586 574
587 575 def compiled_method_name(handler, template, file_name)
588   - ['_run', handler.to_s.demodulize.underscore, compiled_method_name_file_path_segment(file_name)].compact.join('_').to_sym
  576 + ['_run', handler.class.to_s.demodulize.underscore, compiled_method_name_file_path_segment(file_name)].compact.join('_').to_sym
589 577 end
590 578
591 579 def compiled_method_name_file_path_segment(file_name)
12 actionpack/lib/action_view/template_handler.rb
@@ -4,6 +4,10 @@ def self.line_offset
4 4 0
5 5 end
6 6
  7 + def self.compilable?
  8 + false
  9 + end
  10 +
7 11 def initialize(view)
8 12 @view = view
9 13 end
@@ -14,6 +18,14 @@ def render(template, local_assigns)
14 18 def compile(template)
15 19 end
16 20
  21 + def compilable?
  22 + self.class.compilable?
  23 + end
  24 +
  25 + def line_offset
  26 + self.class.line_offset
  27 + end
  28 +
17 29 # Called by CacheHelper#cache
18 30 def cache_fragment(block, name = {}, options = nil)
19 31 end
4 actionpack/lib/action_view/template_handlers/builder.rb
@@ -7,6 +7,10 @@ def self.line_offset
7 7 2
8 8 end
9 9
  10 + def self.compilable?
  11 + true
  12 + end
  13 +
10 14 def compile(template)
11 15 content_type_handler = (@view.send!(:controller).respond_to?(:response) ? "controller.response" : "controller")
12 16 "#{content_type_handler}.content_type ||= Mime::XML\n" +
4 actionpack/lib/action_view/template_handlers/erb.rb
@@ -26,6 +26,10 @@ def compile(template)
26 26 ::ERB.new(template, nil, @view.erb_trim_mode).src
27 27 end
28 28
  29 + def self.compilable?
  30 + true
  31 + end
  32 +
29 33 def cache_fragment(block, name = {}, options = nil) #:nodoc:
30 34 @view.fragment_for(block, name, options) do
31 35 eval(ActionView::Base.erb_variable, block.binding)
4 actionpack/lib/action_view/template_handlers/rjs.rb
@@ -10,6 +10,10 @@ def compile(template)
10 10 "update_page do |page|\n#{template}\nend"
11 11 end
12 12
  13 + def self.compilable?
  14 + true
  15 + end
  16 +
13 17 def cache_fragment(block, name = {}, options = nil) #:nodoc:
14 18 @view.fragment_for(block, name, options) do
15 19 begin
2  actionpack/test/controller/custom_handler_test.rb
... ... @@ -1,6 +1,6 @@
1 1 require 'abstract_unit'
2 2
3   -class CustomHandler
  3 +class CustomHandler < ActionView::TemplateHandler
4 4 def initialize( view )
5 5 @view = view
6 6 end
3  actionpack/test/controller/layout_test.rb
@@ -31,7 +31,7 @@ class ControllerNameSpace::NestedController < LayoutTest
31 31 class MultipleExtensions < LayoutTest
32 32 end
33 33
34   -class MabView
  34 +class MabView < ActionView::TemplateHandler
35 35 def initialize(view)
36 36 end
37 37
@@ -67,6 +67,7 @@ def test_third_party_template_library_auto_discovers_layout
67 67 get :hello
68 68 assert_equal 'layouts/third_party_template_library', @controller.active_layout
69 69 assert_equal 'layouts/third_party_template_library', @response.layout
  70 + assert_response :success
70 71 assert_equal 'Mab', @response.body
71 72 end
72 73
217 actionpack/test/template/compiled_templates_test.rb
@@ -73,115 +73,116 @@ def test_mtime
73 73 end
74 74
75 75 uses_mocha 'test_compile_time' do
76   - def test_compile_time
77   - t = Time.now
78   -
79   - File.open(@a, "w"){|f| f.puts @a}
80   - File.open(@b, "w"){|f| f.puts @b}
81   - # windows doesn't support symlinks (even under cygwin)
82   - windows = (RUBY_PLATFORM =~ /win32/)
83   - `ln -s #{@a} #{@s}` unless windows
84   -
85   - v = ActionView::Base.new
86   - v.base_path = '.'
87   - v.cache_template_loading = false
88   -
89   - # All templates were created at t+1
90   - File::Stat.any_instance.expects(:mtime).times(windows ? 2 : 3).returns(t + 1.second)
91   -
92   - # private methods template_changed_since? and compile_template?
93   - # should report true for all since they have not been compiled
94   - assert v.send(:template_changed_since?, @a, t)
95   - assert v.send(:template_changed_since?, @b, t)
96   - assert v.send(:template_changed_since?, @s, t) unless windows
97   -
98   - assert v.send(:compile_template?, nil, @a, {})
99   - assert v.send(:compile_template?, nil, @b, {})
100   - assert v.send(:compile_template?, nil, @s, {}) unless windows
101   -
102   - @handler = ActionView::Base.handler_for_extension(:rhtml)
103   -
104   - # All templates are rendered at t+2
105   - Time.expects(:now).times(windows ? 2 : 3).returns(t + 2.seconds)
106   - v.send(:compile_and_render_template, @handler, '', @a)
107   - v.send(:compile_and_render_template, @handler, '', @b)
108   - v.send(:compile_and_render_template, @handler, '', @s) unless windows
109   - a_n = v.method_names[@a]
110   - b_n = v.method_names[@b]
111   - s_n = v.method_names[@s] unless windows
112   - # all of the files have changed since last compile
113   - assert v.compile_time[a_n] > t
114   - assert v.compile_time[b_n] > t
115   - assert v.compile_time[s_n] > t unless windows
116   -
117   - # private methods template_changed_since? and compile_template?
118   - # should report false for all since none have changed since compile
119   - File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(t + 1.second)
120   - assert !v.send(:template_changed_since?, @a, v.compile_time[a_n])
121   - assert !v.send(:template_changed_since?, @b, v.compile_time[b_n])
122   - assert !v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows
123   - assert !v.send(:compile_template?, nil, @a, {})
124   - assert !v.send(:compile_template?, nil, @b, {})
125   - assert !v.send(:compile_template?, nil, @s, {}) unless windows
126   - v.send(:compile_and_render_template, @handler, '', @a)
127   - v.send(:compile_and_render_template, @handler, '', @b)
128   - v.send(:compile_and_render_template, @handler, '', @s) unless windows
129   - # none of the files have changed since last compile
130   - assert v.compile_time[a_n] < t + 3.seconds
131   - assert v.compile_time[b_n] < t + 3.seconds
132   - assert v.compile_time[s_n] < t + 3.seconds unless windows
133   -
134   - `rm #{@s}; ln -s #{@b} #{@s}` unless windows
135   - # private methods template_changed_since? and compile_template?
136   - # should report true for symlink since it has changed since compile
  76 +
  77 + def test_compile_time
  78 + t = Time.now
  79 +
  80 + File.open(@a, "w"){|f| f.puts @a}
  81 + File.open(@b, "w"){|f| f.puts @b}
  82 + # windows doesn't support symlinks (even under cygwin)
  83 + windows = (RUBY_PLATFORM =~ /win32/)
  84 + `ln -s #{@a} #{@s}` unless windows
  85 +
  86 + v = ActionView::Base.new
  87 + v.base_path = '.'
  88 + v.cache_template_loading = false
  89 +
  90 + # All templates were created at t+1
  91 + File::Stat.any_instance.expects(:mtime).times(windows ? 2 : 3).returns(t + 1.second)
  92 +
  93 + # private methods template_changed_since? and compile_template?
  94 + # should report true for all since they have not been compiled
  95 + assert v.send(:template_changed_since?, @a, t)
  96 + assert v.send(:template_changed_since?, @b, t)
  97 + assert v.send(:template_changed_since?, @s, t) unless windows
  98 +
  99 + assert v.send(:compile_template?, nil, @a, {})
  100 + assert v.send(:compile_template?, nil, @b, {})
  101 + assert v.send(:compile_template?, nil, @s, {}) unless windows
  102 +
  103 + @handler_class = ActionView::Base.handler_class_for_extension(:rhtml)
  104 + @handler = @handler_class.new(v)
  105 + # All templates are rendered at t+2
  106 + Time.expects(:now).times(windows ? 2 : 3).returns(t + 2.seconds)
  107 + v.send(:compile_and_render_template, @handler, '', @a)
  108 + v.send(:compile_and_render_template, @handler, '', @b)
  109 + v.send(:compile_and_render_template, @handler, '', @s) unless windows
  110 + a_n = v.method_names[@a]
  111 + b_n = v.method_names[@b]
  112 + s_n = v.method_names[@s] unless windows
  113 + # all of the files have changed since last compile
  114 + assert v.compile_time[a_n] > t
  115 + assert v.compile_time[b_n] > t
  116 + assert v.compile_time[s_n] > t unless windows
  117 +
  118 + # private methods template_changed_since? and compile_template?
  119 + # should report false for all since none have changed since compile
  120 + File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(t + 1.second)
  121 + assert !v.send(:template_changed_since?, @a, v.compile_time[a_n])
  122 + assert !v.send(:template_changed_since?, @b, v.compile_time[b_n])
  123 + assert !v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows
  124 + assert !v.send(:compile_template?, nil, @a, {})
  125 + assert !v.send(:compile_template?, nil, @b, {})
  126 + assert !v.send(:compile_template?, nil, @s, {}) unless windows
  127 + v.send(:compile_and_render_template, @handler, '', @a)
  128 + v.send(:compile_and_render_template, @handler, '', @b)
  129 + v.send(:compile_and_render_template, @handler, '', @s) unless windows
  130 + # none of the files have changed since last compile
  131 + assert v.compile_time[a_n] < t + 3.seconds
  132 + assert v.compile_time[b_n] < t + 3.seconds
  133 + assert v.compile_time[s_n] < t + 3.seconds unless windows
  134 +
  135 + `rm #{@s}; ln -s #{@b} #{@s}` unless windows
  136 + # private methods template_changed_since? and compile_template?
  137 + # should report true for symlink since it has changed since compile
137 138
138   - # t + 3.seconds is for the symlink
139   - File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 9).returns(
140   - *(windows ? [ t + 1.second, t + 1.second ] :
141   - [ t + 1.second, t + 1.second, t + 3.second ]) * 3)
142   - assert !v.send(:template_changed_since?, @a, v.compile_time[a_n])
143   - assert !v.send(:template_changed_since?, @b, v.compile_time[b_n])
144   - assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows
145   - assert !v.send(:compile_template?, nil, @a, {})
146   - assert !v.send(:compile_template?, nil, @b, {})
147   - assert v.send(:compile_template?, nil, @s, {}) unless windows
148   -
149   - # Only the symlink template gets rendered at t+3
150   - Time.stubs(:now).returns(t + 3.seconds) unless windows
151   - v.send(:compile_and_render_template, @handler, '', @a)
152   - v.send(:compile_and_render_template, @handler, '', @b)
153   - v.send(:compile_and_render_template, @handler, '', @s) unless windows
154   - # the symlink has changed since last compile
155   - assert v.compile_time[a_n] < t + 3.seconds
156   - assert v.compile_time[b_n] < t + 3.seconds
157   - assert_equal v.compile_time[s_n], t + 3.seconds unless windows
158   -
159   - FileUtils.touch @b
160   - # private methods template_changed_since? and compile_template?
161   - # should report true for symlink and file at end of symlink
162   - # since it has changed since last compile
163   - #
164   - # t+4 is for @b and also for the file that @s points to, which is @b
165   - File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(
166   - *(windows ? [ t + 1.second, t + 4.seconds ] :
167   - [ t + 1.second, t + 4.seconds, t + 3.second, t + 4.seconds ]) * 3)
168   - assert !v.send(:template_changed_since?, @a, v.compile_time[a_n])
169   - assert v.send(:template_changed_since?, @b, v.compile_time[b_n])
170   - assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows
171   - assert !v.send(:compile_template?, nil, @a, {})
172   - assert v.send(:compile_template?, nil, @b, {})
173   - assert v.send(:compile_template?, nil, @s, {}) unless windows
174   -
175   - Time.expects(:now).times(windows ? 1 : 2).returns(t + 5.seconds)
176   - v.send(:compile_and_render_template, @handler, '', @a)
177   - v.send(:compile_and_render_template, @handler, '', @b)
178   - v.send(:compile_and_render_template, @handler, '', @s) unless windows
179   - # the file at the end of the symlink has changed since last compile
180   - # both the symlink and the file at the end of it should be recompiled
181   - assert v.compile_time[a_n] < t + 5.seconds
182   - assert_equal v.compile_time[b_n], t + 5.seconds
183   - assert_equal v.compile_time[s_n], t + 5.seconds unless windows
184   - end
  139 + # t + 3.seconds is for the symlink
  140 + File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 9).returns(
  141 + *(windows ? [ t + 1.second, t + 1.second ] :
  142 + [ t + 1.second, t + 1.second, t + 3.second ]) * 3)
  143 + assert !v.send(:template_changed_since?, @a, v.compile_time[a_n])
  144 + assert !v.send(:template_changed_since?, @b, v.compile_time[b_n])
  145 + assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows
  146 + assert !v.send(:compile_template?, nil, @a, {})
  147 + assert !v.send(:compile_template?, nil, @b, {})
  148 + assert v.send(:compile_template?, nil, @s, {}) unless windows
  149 +
  150 + # Only the symlink template gets rendered at t+3
  151 + Time.stubs(:now).returns(t + 3.seconds) unless windows
  152 + v.send(:compile_and_render_template, @handler, '', @a)
  153 + v.send(:compile_and_render_template, @handler, '', @b)
  154 + v.send(:compile_and_render_template, @handler, '', @s) unless windows
  155 + # the symlink has changed since last compile
  156 + assert v.compile_time[a_n] < t + 3.seconds
  157 + assert v.compile_time[b_n] < t + 3.seconds
  158 + assert_equal v.compile_time[s_n], t + 3.seconds unless windows
  159 +
  160 + FileUtils.touch @b
  161 + # private methods template_changed_since? and compile_template?
  162 + # should report true for symlink and file at end of symlink
  163 + # since it has changed since last compile
  164 + #
  165 + # t+4 is for @b and also for the file that @s points to, which is @b
  166 + File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(
  167 + *(windows ? [ t + 1.second, t + 4.seconds ] :
  168 + [ t + 1.second, t + 4.seconds, t + 3.second, t + 4.seconds ]) * 3)
  169 + assert !v.send(:template_changed_since?, @a, v.compile_time[a_n])
  170 + assert v.send(:template_changed_since?, @b, v.compile_time[b_n])
  171 + assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows
  172 + assert !v.send(:compile_template?, nil, @a, {})
  173 + assert v.send(:compile_template?, nil, @b, {})
  174 + assert v.send(:compile_template?, nil, @s, {}) unless windows
  175 +
  176 + Time.expects(:now).times(windows ? 1 : 2).returns(t + 5.seconds)
  177 + v.send(:compile_and_render_template, @handler, '', @a)
  178 + v.send(:compile_and_render_template, @handler, '', @b)
  179 + v.send(:compile_and_render_template, @handler, '', @s) unless windows
  180 + # the file at the end of the symlink has changed since last compile
  181 + # both the symlink and the file at the end of it should be recompiled
  182 + assert v.compile_time[a_n] < t + 5.seconds
  183 + assert_equal v.compile_time[b_n], t + 5.seconds
  184 + assert_equal v.compile_time[s_n], t + 5.seconds unless windows
  185 + end
185 186 end
186 187 end
187 188

0 comments on commit e6de958

Please sign in to comment.
Something went wrong with that request. Please try again.