PageRenderTime 46ms CodeModel.GetById 13ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/haml/helpers/action_view_mods.rb

http://github.com/scalate/scalate
Ruby | 244 lines | 204 code | 20 blank | 20 comment | 41 complexity | 1d50ec0c32a833b05894d08597576c93 MD5 | raw file
  1module ActionView
  2  class Base
  3    def render_with_haml(*args, &block)
  4      options = args.first
  5
  6      # If render :layout is used with a block,
  7      # it concats rather than returning a string
  8      # so we need it to keep thinking it's Haml
  9      # until it hits the sub-render
 10      if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
 11        return non_haml { render_without_haml(*args, &block) }
 12      end
 13      render_without_haml(*args, &block)
 14    end
 15    alias_method :render_without_haml, :render
 16    alias_method :render, :render_with_haml
 17
 18    # Rails >2.1
 19    if Haml::Util.has?(:instance_method, self, :output_buffer)
 20      def output_buffer_with_haml
 21        return haml_buffer.buffer if is_haml?
 22        output_buffer_without_haml
 23      end
 24      alias_method :output_buffer_without_haml, :output_buffer
 25      alias_method :output_buffer, :output_buffer_with_haml
 26
 27      def set_output_buffer_with_haml(new)
 28        if is_haml?
 29          new = String.new(new) if Haml::Util.rails_xss_safe? &&
 30            new.is_a?(Haml::Util.rails_safe_buffer_class)
 31          haml_buffer.buffer = new
 32        else
 33          set_output_buffer_without_haml new
 34        end
 35      end
 36      alias_method :set_output_buffer_without_haml, :output_buffer=
 37      alias_method :output_buffer=, :set_output_buffer_with_haml
 38    end
 39  end
 40
 41  module Helpers
 42    # In Rails <=2.1, we've got to override considerable capturing infrastructure.
 43    # In Rails >2.1, we can make do with only overriding #capture
 44    # (which no longer behaves differently in helper contexts).
 45    unless Haml::Util.has?(:instance_method, ActionView::Base, :output_buffer)
 46      module CaptureHelper
 47        def capture_with_haml(*args, &block)
 48          # Rails' #capture helper will just return the value of the block
 49          # if it's not actually in the template context,
 50          # as detected by the existance of an _erbout variable.
 51          # We've got to do the same thing for compatibility.
 52
 53          if is_haml? && block_is_haml?(block)
 54            capture_haml(*args, &block)
 55          else
 56            capture_without_haml(*args, &block)
 57          end
 58        end
 59        alias_method :capture_without_haml, :capture
 60        alias_method :capture, :capture_with_haml
 61
 62        def capture_erb_with_buffer_with_haml(buffer, *args, &block)
 63          if is_haml?
 64            capture_haml(*args, &block)
 65          else
 66            capture_erb_with_buffer_without_haml(buffer, *args, &block)
 67          end
 68        end
 69        alias_method :capture_erb_with_buffer_without_haml, :capture_erb_with_buffer
 70        alias_method :capture_erb_with_buffer, :capture_erb_with_buffer_with_haml
 71      end
 72
 73      module TextHelper
 74        def concat_with_haml(string, binding = nil)
 75          if is_haml?
 76            haml_buffer.buffer.concat(string)
 77          else
 78            concat_without_haml(string, binding)
 79          end
 80        end
 81        alias_method :concat_without_haml, :concat
 82        alias_method :concat, :concat_with_haml
 83      end
 84    else
 85      module CaptureHelper
 86        def capture_with_haml(*args, &block)
 87          if Haml::Helpers.block_is_haml?(block)
 88            str = capture_haml(*args, &block)
 89            return ActionView::NonConcattingString.new(str) if defined?(ActionView::NonConcattingString)
 90            return str
 91          else
 92            capture_without_haml(*args, &block)
 93          end
 94        end
 95        alias_method :capture_without_haml, :capture
 96        alias_method :capture, :capture_with_haml
 97      end
 98    end
 99
100    module TagHelper
101      def content_tag_with_haml(name, *args, &block)
102        return content_tag_without_haml(name, *args, &block) unless is_haml?
103
104        preserve = haml_buffer.options[:preserve].include?(name.to_s)
105
106        if block_given? && block_is_haml?(block) && preserve
107          return content_tag_without_haml(name, *args) {preserve(&block)}
108        end
109
110        content = content_tag_without_haml(name, *args, &block)
111        content = Haml::Helpers.preserve(content) if preserve && content
112        content
113      end
114
115      alias_method :content_tag_without_haml, :content_tag
116      alias_method :content_tag, :content_tag_with_haml
117    end
118
119    class InstanceTag
120      # Includes TagHelper
121
122      def haml_buffer
123        @template_object.send :haml_buffer
124      end
125
126      def is_haml?
127        @template_object.send :is_haml?
128      end
129
130      def content_tag(*args)
131        html_tag = content_tag_with_haml(*args)
132        return html_tag unless respond_to?(:error_wrapping)
133        return error_wrapping(html_tag) if method(:error_wrapping).arity == 1
134        return html_tag unless object.respond_to?(:errors) && object.errors.respond_to?(:on)
135        return error_wrapping(html_tag, object.errors.on(@method_name))
136      end
137    end
138
139    if Haml::Util.ap_geq_3?
140      module FormTagHelper
141        def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
142          if is_haml?
143            wrap_block = block_given? && block_is_haml?(proc)
144            if wrap_block
145              oldproc = proc
146              proc = haml_bind_proc do |*args|
147                concat "\n"
148                with_tabs(1) {oldproc.call(*args)}
149              end
150            end
151            res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
152            res << "\n" if wrap_block
153            res
154          else
155            form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
156          end
157        end
158        alias_method :form_tag_without_haml, :form_tag
159        alias_method :form_tag, :form_tag_with_haml
160      end
161
162      module FormHelper
163        def form_for_with_haml(object_name, *args, &proc)
164          wrap_block = block_given? && is_haml? && block_is_haml?(proc)
165          if wrap_block
166            oldproc = proc
167            proc = proc {|*args| with_tabs(1) {oldproc.call(*args)}}
168          end
169          res = form_for_without_haml(object_name, *args, &proc)
170          res << "\n" if wrap_block
171          res
172        end
173        alias_method :form_for_without_haml, :form_for
174        alias_method :form_for, :form_for_with_haml
175      end
176
177      module CacheHelper
178        # This is a workaround for a Rails 3 bug
179        # that's present at least through beta 3.
180        # Their fragment_for assumes that the block
181        # will return its contents as a string,
182        # which is not always the case.
183        # Luckily, it only makes this assumption if caching is disabled,
184        # so we only override that case.
185        def fragment_for_with_haml(*args, &block)
186          return fragment_for_without_haml(*args, &block) if controller.perform_caching
187          capture(&block)
188        end
189        alias_method :fragment_for_without_haml, :fragment_for
190        alias_method :fragment_for, :fragment_for_with_haml
191      end
192    else
193      module FormTagHelper
194        def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
195          if is_haml?
196            wrap_block = block_given? && block_is_haml?(proc)
197            if wrap_block
198              oldproc = proc
199              proc = haml_bind_proc do |*args|
200                concat "\n"
201                tab_up
202                oldproc.call(*args)
203                tab_down
204                concat haml_indent
205              end
206              concat haml_indent
207            end
208            res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
209            if block_given?
210              concat "\n"
211              return Haml::Helpers::ErrorReturn.new("form_tag")
212            end
213            res
214          else
215            form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
216          end
217        end
218        alias_method :form_tag_without_haml, :form_tag
219        alias_method :form_tag, :form_tag_with_haml
220      end
221
222      module FormHelper
223        def form_for_with_haml(object_name, *args, &proc)
224          wrap_block = block_given? && is_haml? && block_is_haml?(proc)
225          if wrap_block
226            oldproc = proc
227            proc = haml_bind_proc do |*args|
228              tab_up
229              oldproc.call(*args)
230              tab_down
231              concat haml_indent
232            end
233            concat haml_indent
234          end
235          form_for_without_haml(object_name, *args, &proc)
236          concat "\n" if wrap_block
237          Haml::Helpers::ErrorReturn.new("form_for") if is_haml?
238        end
239        alias_method :form_for_without_haml, :form_for
240        alias_method :form_for, :form_for_with_haml
241      end
242    end
243  end
244end