| Module | ApplicationHelper |
| In: |
app/helpers/application_helper.rb
|
Methods in this helper are available to all templates in the application.
An Hpricot based breadcrumb helper. Usage: bcrmb("<a href=’/x/y’>About</a> | <a href=’/z’>Contact</a>") Loads html into an hpricot and then swaps out any a-tag with a span-tag if the a-tag-href matches the request.path
# File app/helpers/application_helper.rb, line 9
9: def bcrmb(h)
10: # get an hpricot from the input-html
11: hp = Hpricot(h)
12: # Inside the hpricot, look for an a-tag containing the request.path
13: hps = hp.search("a[@href=#{request.path}]")
14: if (matched_a_tag = hps.first)
15: # I hooked one, get it's text node
16: txtnode = matched_a_tag.inner_text
17: # Inside the hpricot, swap the a-tag with a span-tag
18: matched_a_tag.swap("<span class='bcrmb'>#{txtnode}</span>")
19: end
20: # Pull html out of the hpricot.
21: return hp.to_html
22: end
Override the frgtxt column so I can add links to it and maybe show a subset of the data in it.
# File app/helpers/application_helper.rb, line 36
36: def frgtxt_column(record)
37: link_to_rndr = link_to("Render The HTML Below:", {:id => record, :action => "rndr_frgmnt", :controller => "frgmnts"}, {:target => "l"})
38: # Use the ERB::Util.h() method below to make sure the HTML is displayed rather than rendered.
39: # If they want to render, they can click the link.
40: # Notice that I use .slice() to limit the amount of text sent back to the browser.
41: # If they want to see all of the text they can .rndr_frgmnt() and use browser-view-source.
42: myfrgtxt = (record.frgtxt || "nil")
43: if (h(myfrgtxt).length > 1024)
44: snipmsg = "<span class='snipmsg'>SNIPPED at character number 1024. Use render and then browser-view-source to see all of it.</span>"
45: else
46: snipmsg = ""
47: end # if
48: return("<div class='frgmnt-frgtxt'> <hr />#{link_to_rndr}<hr />#{h(myfrgtxt.slice(0,1024))} <hr /> #{snipmsg} <hr /></div>")
49: end
Builds a simple a-element from URL
# File app/helpers/application_helper.rb, line 26
26: def inputurl_column(record)
27: "<a target='inputurl' href='#{record.inputurl}'>#{record.inputurl}</a>"
28: end
I found this in the AS demo. They use it to show ruby code which corresponds to scaffold views. This helper is called in a partial here: app/views/layouts/_show_source.rhtml Here is a sample line from app/views/layouts/_show_source.rhtml: <%= show_code("controllers", "#{params[:controller]}_controller.rb") -%>
# File app/helpers/application_helper.rb, line 55
55: def show_code(path, filename, comment = "")
56: begin
57: file = File.open("#{File.dirname __FILE__}/../../app/#{path}/#{filename}")
58: "<h4>/\#{path}/\#{filename} \#{comment}</h4>\n<pre><code class=\\\"ruby\\\">\#{file.read.gsub(\"<\", \"<\").gsub(\">\", \">\").strip}</code></pre>\n"
59: rescue
60: "#{filename} is missing"
61: end # begin, rescue
62: end