-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
action_view_mods.rb
131 lines (114 loc) · 3.88 KB
/
action_view_mods.rb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true
module Haml
module Helpers
module ActionViewMods
def render(*args, &block)
options = args.first
# If render :layout is used with a block, it concats rather than returning
# a string so we need it to keep thinking it's Haml until it hits the
# sub-render.
if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
return non_haml { super }
end
super
end
def output_buffer
return haml_buffer.buffer if is_haml?
super
end
def output_buffer=(new_buffer)
if is_haml?
if Haml::Util.rails_xss_safe? && new_buffer.is_a?(ActiveSupport::SafeBuffer)
new_buffer = String.new(new_buffer)
end
haml_buffer.buffer = new_buffer
else
super
end
end
end
ActionView::Base.send(:prepend, ActionViewMods)
end
end
module ActionView
module Helpers
module CaptureHelper
def capture_with_haml(*args, &block)
if Haml::Helpers.block_is_haml?(block)
#double assignment is to avoid warnings
_hamlout = _hamlout = eval('_hamlout', block.binding) # Necessary since capture_haml checks _hamlout
capture_haml(*args, &block)
else
capture_without_haml(*args, &block)
end
end
alias_method :capture_without_haml, :capture
alias_method :capture, :capture_with_haml
end
module TagHelper
def content_tag_with_haml(name, *args, &block)
return content_tag_without_haml(name, *args, &block) unless is_haml?
@_content_tag_name_cache ||= {}
name_string = @_content_tag_name_cache[name] ||= name.to_s
preserve = haml_buffer.options.fetch(:preserve, %w[textarea pre code]).include?(name_string)
if block_given? && block_is_haml?(block) && preserve
return content_tag_without_haml(name, *args) do
haml_buffer.fix_textareas!(Haml::Helpers.preserve(&block)).html_safe
end
end
content = content_tag_without_haml(name, *args, &block)
if preserve && content
content = haml_buffer.fix_textareas!(Haml::Helpers.preserve(content)).html_safe
end
content
end
alias_method :content_tag_without_haml, :content_tag
alias_method :content_tag, :content_tag_with_haml
end
module HamlSupport
include Haml::Helpers
def haml_buffer
@template_object.send :haml_buffer
end
def is_haml?
@template_object.send :is_haml?
end
end
module Tags
class TextArea
include HamlSupport
end
end
class InstanceTag
include HamlSupport
def content_tag(*args, &block)
html_tag = content_tag_with_haml(*args, &block)
return html_tag unless respond_to?(:error_wrapping)
return error_wrapping(html_tag) if method(:error_wrapping).arity == 1
return html_tag unless object.respond_to?(:errors) && object.errors.respond_to?(:on)
return error_wrapping(html_tag, object.errors.on(@method_name))
end
end
module FormTagHelper
def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
if is_haml?
wrap_block = block_given? && block_is_haml?(proc)
if wrap_block
oldproc = proc
proc = haml_bind_proc do |*args|
concat "\n"
with_tabs(1) {oldproc.call(*args)}
end
end
res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) << "\n"
res << "\n" if wrap_block
res
else
form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
end
end
alias_method :form_tag_without_haml, :form_tag
alias_method :form_tag, :form_tag_with_haml
end
end
end