public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Search Repo:
haml / lib / haml / helpers / action_view_mods.rb
100644 160 lines (132 sloc) 4.556 kb
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
if defined?(ActionView) and not defined?(Merb::Plugins)
 
  module Haml
    module Helpers
      module TemplateChecker
        # Handle the case where a module is uses outside of an ActionView context
        # calls to is_haml if it is defined, otherwise returns false.
        def check_is_haml?
          is_haml?
        rescue NoMethodError
          false
        end
      end
    end
  end
 
  module ActionView
    class Base # :nodoc:
 
      include Haml::Helpers::TemplateChecker
 
      def is_haml?
        false
      end
 
      def render_with_haml(*args, &block)
        return non_haml { render_without_haml(*args, &block) } if is_haml?
        render_without_haml(*args, &block)
      end
      alias_method :render_without_haml, :render
      alias_method :render, :render_with_haml
    end
 
    # This overrides various helpers in ActionView
    # to make them work more effectively with Haml.
    module Helpers
      # :stopdoc:
      module CaptureHelper
 
        include Haml::Helpers::TemplateChecker
 
        def capture_with_haml(*args, &block)
          if check_is_haml?
            capture_haml(*args, &block)
          else
            capture_without_haml(*args, &block)
          end
        end
        alias_method :capture_without_haml, :capture
        alias_method :capture, :capture_with_haml
 
        def capture_erb_with_buffer_with_haml(*args, &block)
          if check_is_haml?
            capture_haml_with_buffer(*args, &block)
          else
            capture_erb_with_buffer_without_haml(*args, &block)
          end
        end
        alias_method :capture_erb_with_buffer_without_haml, :capture_erb_with_buffer
        alias_method :capture_erb_with_buffer, :capture_erb_with_buffer_with_haml
      end
 
      module TextHelper
 
        include Haml::Helpers::TemplateChecker
 
        def concat_with_haml(string, binding = nil)
          if check_is_haml?
            haml_buffer.buffer.concat(string)
          else
            concat_without_haml(string, binding)
          end
        end
        alias_method :concat_without_haml, :concat
        alias_method :concat, :concat_with_haml
      end
 
      module TagHelper
 
        include Haml::Helpers::TemplateChecker
 
        def content_tag_with_haml(name, *args, &block)
          content = content_tag_without_haml(name, *args, &block)
 
          if check_is_haml? && haml_buffer.options[:preserve].include?(name.to_s)
            content = Haml::Helpers.preserve content
          end
 
          content
        end
        alias_method :content_tag_without_haml, :content_tag
        alias_method :content_tag, :content_tag_with_haml
      end
 
      class InstanceTag
        # Includes TagHelper
 
        include Haml::Helpers::TemplateChecker
 
        def haml_buffer
          @template_object.send :haml_buffer
        end
 
        def is_haml?
          @template_object.send :is_haml?
        end
 
        alias_method :content_tag_without_haml, :content_tag
        alias_method :content_tag, :content_tag_with_haml
      end
 
      module FormTagHelper
 
        include Haml::Helpers::TemplateChecker
 
        def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
          if check_is_haml?
            if block_given?
              oldproc = proc
              proc = haml_bind_proc do |*args|
                concat "\n"
                tab_up
                oldproc.call(*args)
                tab_down
              end
            end
            res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
            concat "\n" if block_given? && check_is_haml?
            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
 
      module FormHelper
 
        include Haml::Helpers::TemplateChecker
 
        def form_for_with_haml(object_name, *args, &proc)
          if block_given? && check_is_haml?
            oldproc = proc
            proc = haml_bind_proc do |*args|
              tab_up
              oldproc.call(*args)
              tab_down
            end
          end
          form_for_without_haml(object_name, *args, &proc)
          concat "\n" if block_given? && check_is_haml?
        end
        alias_method :form_for_without_haml, :form_for
        alias_method :form_for, :form_for_with_haml
      end
      # :startdoc:
    end
  end
end