Module: ExtendActionController

Defined in:
lib/extend_action_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
# File 'lib/extend_action_controller.rb', line 2

def self.included(base)
  base.alias_method_chain :process, :clientperf
end

Instance Method Details

#add_clientperf?(request, response) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/extend_action_controller.rb', line 41

def add_clientperf?(request, response)
  !request.xhr? && response.content_type && response.content_type.include?('html') && 
    response.body && response.headers['Status'] && response.headers['Status'].include?('200') && 
    controller_name != 'clientperf'
end

#add_clientperf_to(response) ⇒ Object



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
# File 'lib/extend_action_controller.rb', line 14

def add_clientperf_to(response)
  if response.body =~ /(<html[^>]*?>).*(<\/html>)/im
    top = %q(\1
      <script type='text/javascript'>
        var _clientPerfStart = (new Date()).getTime();
      </script>
    )
    
    bottom = %q(
      <script type='text/javascript'>
      (function() {
      	function fn() { var end = (new Date()).getTime(), img = document.createElement('img'); img.height ='1'; img.width = '1'; img.src = '/clientperf/measure.gif?b='+_clientPerfStart+'&e='+end+'&u='+encodeURIComponent(location.href); document.body.appendChild(img); };
      	if (window.addEventListener) { window.addEventListener('load', fn, false); }
      	else if (window.attachEvent) { window.attachEvent('onload', fn); }
      	else { var chain = window.onload; window.onload = function(e) { if(chain !== undefined) { chain(e); } fn(); } }
      })();
      </script>
      </html>
    )
    
    response.body.sub!(/(<html[^>]*?>)/im, top)
    response.body.sub!(/<\/html>/im, bottom)
    
    response.headers["Content-Length"] = response.body.size
  end
end

#process_with_clientperf(request, response, *args, &block) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/extend_action_controller.rb', line 6

def process_with_clientperf(request, response, *args, &block)
  result = process_without_clientperf(request, response, *args, &block)
  if add_clientperf?(request, response)
    add_clientperf_to(response)
  end
  result
end