Module: TuneupHelper

Includes:
BumpsparkHelper
Defined in:
lib/tuneup_helper.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from BumpsparkHelper

#build_data_url, #build_png, #build_png_chunk, #bumpspark, #normalize

Instance Method Details



88
89
90
91
92
93
# File 'lib/tuneup_helper.rb', line 88

def additional_step_links(step)
  returning '' do |text|
    text << sql_link(step) if step.sql
    text << schema_link(step) if step.table_name
  end
end


196
197
198
199
# File 'lib/tuneup_helper.rb', line 196

def link_to_edit_step(step)
  return nil unless step.file && step.line && RUBY_PLATFORM.include?('darwin')
  link_to(image_tag('/images/tuneup/edit.png', :alt => 'Edit'), "txmt://open?url=file://#{CGI.escape step.file}&amp;line=#{step.line}", :class => 'tuneup-edit tuneup-halo', :title => 'Open in TextMate')
end


105
106
107
# File 'lib/tuneup_helper.rb', line 105

def link_to_schema(text, table, html_options={})
  link_to_function(text, "TuneUp.switchSchema('#{table}')", html_options.merge(:title => "View Schema"))
end


78
79
80
# File 'lib/tuneup_helper.rb', line 78

def link_to_show
  %|<a id="tuneup-back-to-run-link" onclick="#{redisplay_last_run} return false;" href="#">&lt;&lt;&lt; Back to Run</a>|
end


70
71
72
73
74
75
76
# File 'lib/tuneup_helper.rb', line 70

def link_to_upload
  if @config.state == :registered
    %|<a onclick="new TuneUpSandbox.Ajax.Request('/tuneup/upload?uri=#{CGI.escape(session['fiveruns_tuneup_last_uri'])}', {asynchronous:true, evalScripts:true, onComplete:function(request){ TuneUp.Spinner.stop(); TuneUpSandbox.$('tuneup-top').show();}, onLoading:function(request){TuneUpSandbox.$('tuneup-top').hide(); TuneUp.Spinner.start()}}); return false;" id="tuneup-save-link" href="#">Share this Run</a>|
  else
    %|<a onclick="new TuneUpSandbox.Ajax.Request('/tuneup/register', {asynchronous:true, evalScripts:true}); return false;" id="tuneup-save-link" href="#">Login to Share this Run</a>|
  end
end

#open_step?(step) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/tuneup_helper.rb', line 56

def open_step?(step)
   [/^Around filter/, /^Invoke/].any? { |pattern| step.name =~ pattern }
end

#redisplay_last_run(namespaced = true) ⇒ Object



82
83
84
85
# File 'lib/tuneup_helper.rb', line 82

def redisplay_last_run(namespaced=true)
  namespace_js = lambda { |fun| namespaced ? "TuneUpSandbox.#{fun}" : fun }
  "#{namespace_js['$']}('tuneup-panel').show(); #{namespace_js['$']}('tuneup-signin-form').remove();"
end


95
96
97
# File 'lib/tuneup_helper.rb', line 95

def schema_link(step)
  link_to_schema(image_tag('/images/tuneup/schema.png', :alt => 'Schema'), step.table_name,  :class => 'tuneup-schema tuneup-halo')
end


99
100
101
102
103
# File 'lib/tuneup_helper.rb', line 99

def sql_link(step)
  link_to_function(image_tag('/images/tuneup/magnify.png', :alt => 'Query'), :class => 'tuneup-sql tuneup-halo', :title => 'View Query') do |page| 
    page << %(TuneUpSandbox.$("#{dom_id(step, :sql)}").toggle(); return false;)
  end
end

#trendObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tuneup_helper.rb', line 44

def trend
  numbers= if Fiveruns::Tuneup.trend.size > 50
    Fiveruns::Tuneup.trend[-50..-1]
  else
    Fiveruns::Tuneup.trend
  end
  return unless numbers.size > 1
  tag(:img,
    :src => build_data_url("image/png",bumpspark(numbers)), :alt => '',
    :title => "Trend over last #{pluralize(numbers.size, 'run')}")
end

#tuneup_bar(step = tuneup_data, options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tuneup_helper.rb', line 114

def tuneup_bar(step=tuneup_data, options={})
  width = options.delete(:width) || 200
  bars = Fiveruns::Tuneup::Step.layers.map do |layer|
    percent = step.percentages_by_layer[layer]
    if percent == 0
      next
    else
      begin
        size = (percent * width).to_i
      rescue
        raise "Can't find percent for #{layer.inspect} from #{step.percentages_by_layer.inspect}"\
      end
    end
    size = 1 if size.zero?
    (:li, ((size >= 10 && layer != :other) ? layer.to_s[0, 1].capitalize : ''),
      :class => "tuneup-layer-#{layer}",
      :style => "width:#{size}px",
      :title => layer.to_s.titleize)
  end
  (:ul, bars.compact.join, options.merge(:class => 'tuneup-bar'))
end

#tuneup_collecting?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/tuneup_helper.rb', line 31

def tuneup_collecting?
  Fiveruns::Tuneup.collecting
end


13
14
15
16
# File 'lib/tuneup_helper.rb', line 13

def tuneup_collection_link
  state = tuneup_collecting? ? :off : :on
  %|<a onclick="new TuneUpSandbox.Ajax.Request('/tuneup/#{state}', {asynchronous:true, evalScripts:true}); return false;" href="#">Turn #{state.to_s.titleize}</a>|
end

#tuneup_css_class_for_step(step) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/tuneup_helper.rb', line 22

def tuneup_css_class_for_step(step)
  returning [] do |classes|
    if step.children.any?
      classes << 'with-children'
      classes << 'tuneup-opened' if step.depth == 1 || open_step?(step)
    end
  end.join(' ')
end

#tuneup_dataObject



35
36
37
38
# File 'lib/tuneup_helper.rb', line 35

def tuneup_data
  most_recent_data = Fiveruns::Tuneup.stack.first
  most_recent_data.blank? ? most_recent_data : most_recent_data['stack']
end

#tuneup_open_run(token) ⇒ Object



169
170
171
# File 'lib/tuneup_helper.rb', line 169

def tuneup_open_run(token)
  %[window.open("#{Fiveruns::Tuneup.frontend_url}/runs/#{token}", 'fiveruns_tuneup');]
end

#tuneup_recording?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/tuneup_helper.rb', line 18

def tuneup_recording?
  Fiveruns::Tuneup.recording?
end

#tuneup_reload_panelObject



173
174
175
176
177
178
179
180
# File 'lib/tuneup_helper.rb', line 173

def tuneup_reload_panel
  update_page do |page|
    page << "$('tuneup-flash').removeClassName('tuneup-show');"
    page << %[$('tuneup-content').update("#{escape_javascript(render(:partial => 'tuneup/panel/show.html.erb'))}");]
    page << 'TuneUp.adjustAbsoluteElements(_document.body);'
    page << 'TuneUp.adjustFixedElements();'
  end
end

#tuneup_schemasObject



40
41
42
# File 'lib/tuneup_helper.rb', line 40

def tuneup_schemas
  Fiveruns::Tuneup.stack.first['schemas']
end

#tuneup_show_flash(type, locals) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/tuneup_helper.rb', line 182

def tuneup_show_flash(type, locals)
  types = [:error, :notice].reject { |t| t == type }
  update_page do |page|
    page << %[$('tuneup-flash').update("#{escape_javascript(render(:partial => 'flash.html.erb', :locals => locals.merge(:type => type)))}");]
    page << "$('tuneup-flash').addClassName('tuneup-show')"
    types.each do |other_type|
      page << "$('tuneup-flash').removeClassName('tuneup-#{other_type}')"
    end
    page << "$('tuneup-flash').addClassName('tuneup-#{type}')"
    page << 'TuneUp.adjustAbsoluteElements(_document.body);'
    page << 'TuneUp.adjustFixedElements();'
  end
end

#tuneup_signin_urlObject



5
6
7
# File 'lib/tuneup_helper.rb', line 5

def 
  "#{Fiveruns::Tuneup.collector_url}/users"
end

#tuneup_signup_urlObject



9
10
11
# File 'lib/tuneup_helper.rb', line 9

def 
  "#{Fiveruns::Tuneup.frontend_url}/signup"
end


60
61
62
63
64
65
66
67
68
# File 'lib/tuneup_helper.rb', line 60

def tuneup_step_link(step)
  name = tuneup_style_step_name(tuneup_truncate_step_name(step))
  link = if step.children.any?
    link_to_function(name, "TuneUpSandbox.$('#{dom_id(step, :children)}').toggle();TuneUpSandbox.$('#{dom_id(step)}').toggleClassName('tuneup-opened');", :class => "tuneup-step-link", :title => step.name)
  else
    (:span, name, :title => step.name)
  end
  link << additional_step_links(step)
end

#tuneup_style_step_name(name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tuneup_helper.rb', line 136

def tuneup_style_step_name(name)
  case name
  when /^Perform (\S+) action in (\S+Controller)$/
    "Perform <strong>#{h $1}</strong> action in <strong>#{h $2}</strong>"
  when /^Invoke (\S+) action$/
    "Invoke <strong>#{h $1}</strong> action"
  when /^(Find|Create|Delete|Update) ([A-Z]\S*)(.*?)$/
    "#{h $1} <strong>#{h $2}</strong>#{h $3}"
  when /^(Render.*?)(\S+)$/
    "#{h $1}<strong>#{h $2}</strong>"
  when /^(\S+ filter )(.*?)$/
    "#{h $1}<strong>#{h $2}</strong>"
  when 'Other'
    "(<i>Other</i>)"
  else
    h(name)
  end
end

#tuneup_truncate(text, max = 32) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tuneup_helper.rb', line 155

def tuneup_truncate(text, max=32)
  if text.size > max
    component = (max - 3) / 2
    remainder = (max - 3) % 2
    begin
      text.sub(/^(.{#{component}}).*?(.{#{component + remainder}})$/s, '\1...\2')
    rescue
      text
    end
  else
    text
  end
end

#tuneup_truncate_step_name(step) ⇒ Object



109
110
111
112
# File 'lib/tuneup_helper.rb', line 109

def tuneup_truncate_step_name(step)
  chars = 50 - (step.depth * 2)
  tuneup_truncate(step.name, chars)
end