Module: Ginatra::Helpers
- Defined in:
- lib/ginatra/helpers.rb
Overview
Actually useful stuff
Instance Method Summary collapse
- #actor_box(actor, role, date) ⇒ Object
- #actor_boxes(commit) ⇒ Object
- #archive_link(tree, repo_param) ⇒ Object
- #atom_feed_link(repo_param, ref = nil) ⇒ Object
- #commit_ref(ref, repo_param) ⇒ Object
- #commit_refs(commit, repo_param) ⇒ Object
- #diff(diff) ⇒ Object
-
#file_listing(commit) ⇒ Object
The only reason this doesn’t work 100% of the time is because grit doesn’t :/ if i find a fix, it’ll go upstream :D.
- #gravatar_url(email) ⇒ Object
-
#hostname ⇒ Object
stolen from Marley.
-
#html_escape(s) ⇒ Object
(also: #h)
stolen from rails: ERB::Util.
- #nicetime(date) ⇒ Object
- #patch_link(commit, repo_param) ⇒ Object
-
#rfc_date(datetime) ⇒ Object
stolen from Marley.
-
#simple_format(text) ⇒ Object
Stolen from rails: ActionView::Helpers::TextHelper#simple_format and simplified to just use <p> tags without any options modified since.
-
#truncate(text, options = {}) ⇒ Object
Stolen and bastardised from rails.
Instance Method Details
#actor_box(actor, role, date) ⇒ Object
15 16 17 |
# File 'lib/ginatra/helpers.rb', line 15 def actor_box(actor, role, date) partial(:actor_box, :locals => { :actor => actor, :role => role, :date => date }) end |
#actor_boxes(commit) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/ginatra/helpers.rb', line 19 def actor_boxes(commit) o = actor_box(commit.committer, :committer, commit.committed_date) if commit..name != commit.committer.name o = actor_box(commit., :author, commit.) + o end end |
#archive_link(tree, repo_param) ⇒ Object
35 36 37 |
# File 'lib/ginatra/helpers.rb', line 35 def archive_link(tree, repo_param) "<a class=\"download\" href=\"/#{repo_param}/archive/#{tree.id}.tar.gz\" title=\"Download a tar.gz snapshot of this Tree\">Download Archive</a>" end |
#atom_feed_link(repo_param, ref = nil) ⇒ Object
107 108 109 |
# File 'lib/ginatra/helpers.rb', line 107 def atom_feed_link(repo_param, ref=nil) "<a href=\"/#{repo_param}#{"/#{ref}" if !ref.nil?}.atom\" title=\"Atom Feed\" class=\"atom\">Feed</a>" end |
#commit_ref(ref, repo_param) ⇒ Object
26 27 28 29 |
# File 'lib/ginatra/helpers.rb', line 26 def commit_ref(ref, repo_param) ref_class = ref.class.to_s.split("::")[1].to_s "<a class=\"ref #{ref_class}\" href=\"/#{repo_param}/#{ref.name}\">#{ref.name}</a>" end |
#commit_refs(commit, repo_param) ⇒ Object
31 32 33 |
# File 'lib/ginatra/helpers.rb', line 31 def commit_refs(commit, repo_param) commit.refs.map{ |r| commit_ref(r, repo_param) }.join("\n") end |
#diff(diff) ⇒ Object
59 60 61 |
# File 'lib/ginatra/helpers.rb', line 59 def diff(diff) diff = CodeRay.scan(diff, :diff).div(:line_numbers => :table, :css => :class) end |
#file_listing(commit) ⇒ Object
The only reason this doesn’t work 100% of the time is because grit doesn’t :/ if i find a fix, it’ll go upstream :D
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ginatra/helpers.rb', line 45 def file_listing(commit) count = 0 out = commit.diffs.map do |diff| count = count + 1 if diff.deleted_file %(<li class='file_rm'><a href='#file_#{count}'>#{diff.a_path}</a></li>) else cla = diff.new_file ? "add" : "diff" %(<li class='file_#{cla}'><a href='#file_#{count}'>#{diff.a_path}</a></li>) end end "<ul id='files'>#{out.join}</ul>" end |
#gravatar_url(email) ⇒ Object
7 8 9 |
# File 'lib/ginatra/helpers.rb', line 7 def gravatar_url(email) "https://secure.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=40" end |
#hostname ⇒ Object
stolen from Marley
103 104 105 |
# File 'lib/ginatra/helpers.rb', line 103 def hostname (request.env['HTTP_X_FORWARDED_SERVER'] =~ /[a-z]*/) ? request.env['HTTP_X_FORWARDED_SERVER'] : request.env['HTTP_HOST'] end |
#html_escape(s) ⇒ Object Also known as: h
stolen from rails: ERB::Util
74 75 76 77 78 79 80 81 |
# File 'lib/ginatra/helpers.rb', line 74 def html_escape(s) s.to_s.gsub(/[&"<>]/) do |special| { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }[special] end end |
#nicetime(date) ⇒ Object
11 12 13 |
# File 'lib/ginatra/helpers.rb', line 11 def nicetime(date) date.strftime("%b %d, %Y – %H:%M") end |
#patch_link(commit, repo_param) ⇒ Object
39 40 41 |
# File 'lib/ginatra/helpers.rb', line 39 def patch_link(commit, repo_param) "<a class=\"download\" href=\"/#{repo_param}/commit/#{commit.id}.patch\" title=\"Download a patch file of this Commit\">Download Patch</a>" end |
#rfc_date(datetime) ⇒ Object
stolen from Marley
98 99 100 |
# File 'lib/ginatra/helpers.rb', line 98 def rfc_date(datetime) datetime.strftime("%Y-%m-%dT%H:%M:%SZ") # 2003-12-13T18:30:02Z end |
#simple_format(text) ⇒ Object
Stolen from rails: ActionView::Helpers::TextHelper#simple_format
and simplified to just use <p> without any
modified since
66 67 68 69 70 71 |
# File 'lib/ginatra/helpers.rb', line 66 def simple_format(text) text.gsub!(/ +/, " ") text.gsub!(/\r\n?/, "\n") text.gsub!(/\n/, "<br />\n") text end |
#truncate(text, options = {}) ⇒ Object
Stolen and bastardised from rails
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ginatra/helpers.rb', line 85 def truncate(text, ={}) [:length] ||= 30 [:omission] ||= "..." if text l = [:length] - [:omission].length chars = text stop = [:separator] ? (chars.rindex([:separator], l) || l) : l (chars.length > [:length] ? chars[0...stop] + [:omission] : text).to_s end end |