aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/deprecated_block_helpers.rb
blob: 3d0657e873de620a50bb55d29be9550f3a29e5e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module ActionView
  module Helpers
    module DeprecatedBlockHelpers
      extend ActiveSupport::Concern

      include ActionView::Helpers::TagHelper
      include ActionView::Helpers::TextHelper
      include ActionView::Helpers::JavaScriptHelper
      include ActionView::Helpers::FormHelper

      def content_tag(*, &block)
        block_called_from_erb?(block) ? safe_concat(super) : super
      end

      def javascript_tag(*, &block)
        block_called_from_erb?(block) ? safe_concat(super) : super
      end

      def form_for(*, &block)
        block_called_from_erb?(block) ? safe_concat(super) : super
      end

      def form_tag(*, &block)
        block_called_from_erb?(block) ? safe_concat(super) : super
      end

      def fields_for(*, &block)
        block_called_from_erb?(block) ? safe_concat(super) : super
      end

      def field_set_tag(*, &block)
        block_called_from_erb?(block) ? safe_concat(super) : super
      end

      BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'

      if RUBY_VERSION < '1.9.0'
        # Check whether we're called from an erb template.
        # We'd return a string in any other case, but erb <%= ... %>
        # can't take an <% end %> later on, so we have to use <% ... %>
        # and implicitly concat.
        def block_called_from_erb?(block)
          block && eval(BLOCK_CALLED_FROM_ERB, block)
        end
      else
        def block_called_from_erb?(block)
          block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
        end
      end
    end
  end
end