2010-12-06 58 views
0

我要重寫的軌道是如何創建一個視圖* .html.erb鋼軌壓倒一切VIEWS renderization

在包裝的ActionView我已經嘗試過這樣做。這樣做

class ERB 
    class Compiler # :nodoc: 
.. 
class Buffer # :nodoc: 
    def compile(s) 
    ... 
    #It stores in a buffer each ruby chunk in the views inside of a Buffer. 
    end 
end 
    end 
... 
# Here it is where is called compile method. 
# The thing is that if my view is made up of several *.html.erb files such as partials this method will be invoked each time.   
     #INVOKED PER EACH html.erb file 

def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout') 
    puts ">>> initialize"  
    @safe_level = safe_level 
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => "%d %b %Y") 
    compiler = ERB::Compiler.new(trim_mode) 
    # raise "need a block" 
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => nil) 
    set_eoutvar(compiler, eoutvar) 
    @src = compiler.compile(str) 
    @filename = nil 
    end 
end 
end 

我想知道什麼時候底部的過程開始。我的意思是我必須重寫哪些類,文件等,以查看Rails何時開始爲特定視圖調用所有html.erb。

我想我應該在這裏:

require 'delegate' 
require 'optparse' 
require 'fileutils' 
require 'tempfile' 
require 'erb' 

module Rails 
    module Generator 
    module Commands 

     class Create 

       # Generate a file for a Rails application using an ERuby template. 
       # Looks up and evaluates a template by name and writes the result. 
       # 
       # The ERB template uses explicit trim mode to best control the 
       # proliferation of whitespace in generated code. <%- trims leading 
       # whitespace; -%> trims trailing whitespace including one newline. 
       # 
       # A hash of template options may be passed as the last argument. 
       # The options accepted by the file are accepted as well as :assigns, 
       # a hash of variable bindings. Example: 
       # template 'foo', 'bar', :assigns => { :action => 'view' } 
       # 
       # Template is implemented in terms of file. It calls file with a 
       # block which takes a file handle and returns its rendered contents. 
       def template(relative_source, relative_destination, template_options = {}) 
       puts "EEEEEEEEEEEEEEEEEEEEEe" 
       file(relative_source, relative_destination, template_options) do |file| 
        # Evaluate any assignments in a temporary, throwaway binding. 
        vars = template_options[:assigns] || {} 
        b = template_options[:binding] || binding 
        vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b } 

        # Render the source file with the temporary binding. 
        ERB.new(file.read, nil, '-').result(b) 
       end 
      end 

     end 

    end 
    end 
end 

但我不覺得從賣出期權方法的任何痕跡。

所有重命名都放在一個名爲/lib/*_extensions.erb文件,並在/config/initializers/extensions.rb我有下:

Dir[File.dirname(__FILE__) + "/../../lib/*_extensions.rb"].each do |fn| 
    require fn 
end 

我不想透露我爲什麼這樣做。

感謝

回答

0

我想用一個before_render不至少在Rails的2.3.4存在。

所以,如果你想這樣做,我也爲下一個:

class ApplicationController < ActionController::Base 
     helper :all # include all helpers, all the time 
     protect_from_forgery # See ActionController::RequestForgeryProtection for details 

     # Scrub sensitive parameters from your log 
     # filter_parameter_logging :password 

     protected 
     def render(options = nil, extra_options = {}, &block) #:doc: 
     puts "BEFORE render" 
     puts "Setting %H:%M:%S" 
     ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%H:%M:%S") 
     # call the ActionController::Base render to show the page 
     super 
     puts "AFTER render" 
     puts "Resetting" 
     ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => nil) 
     end 

end