-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.rb
More file actions
85 lines (69 loc) · 2.21 KB
/
helpers.rb
File metadata and controls
85 lines (69 loc) · 2.21 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
class Application < Sinatra::Base
before do
@title = "Home"
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
def tidy_date(date)
date.strftime("#{date.day.ordinalize} of %B, %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
def today_entered?
return "disabled" if Weighin.first(:created_at.gte => Time.now)
nil
end
def weight_loss
(WeightConverter.new(Weighin.first.kg).kg - WeightConverter.new(Weighin.last.kg).kg).round(1)
end
def current_weight
WeightConverter.new(Weighin.last.kg).kg
end
def start_weight
WeightConverter.new(Weighin.first.kg).kg
end
def goal
settings.weight_goal
end
def weight_loss_progress
(current_weight - goal).round(1)
end
def weight_loss_progress_percentage
return 100 if current_weight <= goal
((start_weight - current_weight) * (100 / (start_weight - goal))).floor
end
def weight_diff(now, prev)
diff = WeightConverter.new( now - prev )
diff_lbs = ( WeightConverter.new(now).lbs - WeightConverter.new(prev).lbs )
case
when diff.kg < 0
diff_label = "label-success"
when diff.kg > 0
diff_label = "label-important"
else
diff_label = ""
end
OpenStruct.new(:kg => diff.kg, :lbs => diff_lbs, :bmi => diff.bmi, :label => diff_label)
end
def graph(weights)
x_labels = weights.map{|w| w.created_at.strftime("%d %b")}.join("|")
y_min = weights.min_by{|w| w.kg }.kg - 0.5
y_max = weights.max_by{|w| w.kg }.kg + 0.5
data = weights.map{|w| WeightConverter.new(w.kg).kg}.join(",")
"http://chart.googleapis.com/chart?cht=lc&chs=960x300&chxt=x,y&chxr=1,#{y_min},#{y_max}&chd=t:0|#{data}&chds=#{y_min},#{y_max}&chls=5&chxl=0:| #{x_labels}&chg=10,10"
end
end
end