Module: Cheat::Helpers

Defined in:
lib/cheat/site.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.h(text) ⇒ Object



573
574
575
# File 'lib/cheat/site.rb', line 573

def self.h(text)
  ERB::Util::h(text)
end

Instance Method Details

#color_diff(diff) ⇒ Object



581
582
583
584
585
586
587
588
589
# File 'lib/cheat/site.rb', line 581

def color_diff(diff)
  diff.split("\n").map do |line|
    action = case line
             when /^-/  then 'cut'
             when /^\+/ then 'add'
             end
    action ? span.send("diff_#{action}", line) : line
  end * "\n"
end

#current_sheetObject



544
545
546
547
# File 'lib/cheat/site.rb', line 544

def current_sheet
  title = @sheet.title
  @current_sheet ||= Cheat::Models::Sheet.detect { |s| s.title == title }
end

#h(text) ⇒ Object



577
578
579
# File 'lib/cheat/site.rb', line 577

def h(text)
  ::Cheat::Helpers.h(text)
end

#last_updated(sheet) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/cheat/site.rb', line 557

def last_updated(sheet)
  from = sheet.updated_at.to_i
  to = Time.now.to_i
  from = from.to_time if from.respond_to?(:to_time)
  to = to.to_time if to.respond_to?(:to_time)
  distance = (((to - from).abs)/60).round
  case distance
    when 0..1       then return (distance==0) ? 'less than a minute' : '1 minute'
    when 2..45      then "#{distance} minutes"
    when 46..90     then 'about 1 hour'
    when 90..1440   then "about #{(distance.to_f / 60.0).round} hours"
    when 1441..2880 then '1 day'
    else                 "#{(distance / 1440).round} days"
  end
end


535
536
537
538
539
540
541
542
# File 'lib/cheat/site.rb', line 535

def logo_link(title)
  ctr = Cheat::Controllers
  if @sheet && !@sheet.new_record? && @sheet.version != current_sheet.version
    a title, :href => R(ctr::Show, @sheet.title)
  else
    a title, :href => R(ctr::Index)
  end
end

#recent_sheetsObject



549
550
551
# File 'lib/cheat/site.rb', line 549

def recent_sheets
  Cheat::Models::Sheet.sort_by { |s| -s.updated_at }.first(15)
end


553
554
555
# File 'lib/cheat/site.rb', line 553

def sheet_link(title, version = nil)
  a title, :href => R(Cheat::Controllers::Show, title, version)
end