Using rails 2 views in rails 3.2

Usage
Just keep using your deprecated block helpers, they can be converted once the transition to rails 3 is complete.

<% form_for xxx do |f| %>
  still works
<% end %>

Code

  # make <% helper do %> work
  block_concat = [
    [ActionView::Helpers::FormHelper, :fields_for],
    [ActionView::Helpers::FormBuilder, :fields_for],
    [ActionView::Helpers::FormHelper, :form_for],
    [ActionView::Helpers::FormTagHelper, :form_tag],
    [ActionView::Helpers::TagHelper, :content_tag],
    [ActionView::Helpers::UrlHelper, :link_to],
    [ActionView::Helpers::JavaScriptHelper, :javascript_tag],
    [ActionView::Helpers::CacheHelper, :cache],
  ]

  ERB_EXTENSION = '.erb'.freeze

  block_concat.each do |klass, method|
    klass.class_eval do
      without = "#{method}_without_concat".to_sym
      alias_method without, method
      define_method(method) do |*args, &block|
        result = send(without, *args, &block)
        if block && block.source_location.first.include?(ERB_EXTENSION) # called from a erb template ?
          (@template || self).concat(result) # deprecated erb concat helper
          ""
        else
          result
        end
      end
    end
  end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s