-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.rb
More file actions
97 lines (84 loc) · 2.22 KB
/
helpers.rb
File metadata and controls
97 lines (84 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Hooks and helpers
before do
@title = "Home"
@tags = Post.all.map {|p| p.tags}.flatten.sort {|a,b| a <=> b}.inject(Hash.new(0)){|h,x| h[x]+=1;h}.to_a
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
# Get the current page from the query string. If none then is page 1
def get_page
case page = params[:page].to_i
when 0; 1
when (1..POST_PAGES); page
else POST_PAGES
end
end
def tidy_date(date)
date.strftime("#{date.day.ordinalize} of %B, %Y")
end
# format time for html time tag
def geek_date(date)
date.strftime("%Y-%m-%d")
end
# short time dd/mm/yy
def short_date(date)
date.strftime("%d/%m/%Y")
end
# Pluralize any word (2, post) (5, dice, die)
def pluralize(n, singular, plural=nil)
if n == 1
"1 #{singular}"
elsif plural
"#{n} #{plural}"
else
"#{n} #{singular}s"
end
end
# Loads partial view into template. Required vriables into locals
def partial(template, locals = {})
erb(template, :layout => false, :locals => locals)
end
# Check if js file exists with name
def js_exists? name
FileTest.exist? "public/js/#{name}.js"
end
# Check if css file exists with name
def css_exists? name
FileTest.exist? "public/js/#{name}.css"
end
# DateTime to words
def timeago(time, options = {})
start_date = options.delete(:start_date) || Time.new
date_format = options.delete(:date_format) || :default
delta_minutes = (start_date.to_i - time.to_i).floor / 60
if delta_minutes.abs <= (8724*60)
distance = distance_of_time_in_words(delta_minutes)
if delta_minutes < 0
return "#{distance} from now"
else
return "#{distance} ago"
end
else
return "on #{DateTime.now.to_formatted_s(date_format)}"
end
end
def distance_of_time_in_words(minutes)
case
when minutes < 1
"less than a minute"
when minutes < 50
pluralize(minutes, "minute")
when minutes < 90
"about one hour"
when minutes < 1080
"#{(minutes / 60).round} hours"
when minutes < 1440
"one day"
when minutes < 2880
"about one day"
else
"#{(minutes / 1440).round} days"
end
end
end