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