0

Rails がビュー *.html.erb を作成する方法をオーバーライドしたい

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

しかし、 puts メソッドからの痕跡は見つかりません。

すべての名前変更は、というファイルに配置され/lib/*_extensions.erb/config/initializers/extensions.rb次のものがあります。

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

なぜ私がこれをしているのかを明らかにしたくありません。

ありがとう

4

1 に答える 1

0

少なくとも Rails 2.3.4 には存在しない before_render を使用したかったのです。

あなたがそれをしたいのであれば、私は次のようにしました:

    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
于 2010-12-08T10:27:46.573 に答える