如何使用进度条计算一天结束而不是开始?
How to use progressbar to count by end of day instead of beginning?
现在进度条代表每一天的开始。例如,
显示的数字是“1”,但从第一天开始它仍然应该显示“0”。到一天结束时,11:59pm,它应该说“1”,因为那时习惯已经完成。
habits/index.html.erb
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="10" style="width: <%= challenged.progress_in_percent %>%;">
<%= challenged.calculate_days_lost %>
</div>
<%= challenged.days_left_in_current_level %>
</div>
habit.rb
def real_missed_days
value = 0
levels.each do |level|
value += level.missed_days + level.days_lost
end
value
end
def calculate_days_lost
def n_days
((date_started.to_date)..Date.today).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
n_days
when 10..24
n_days-10
when 25..44
n_days-25
when 45..69
n_days-45
when 70..99
n_days-70
else
n_days-100
end
end
def days_left_in_current_level
def n_days
((date_started.to_date)..Date.today).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
10-n_days
when 10..24
25-n_days
when 25..44
45-n_days
when 45..69
70-n_days
when 70..99
100-n_days
else
0 # No end
end
end
这是其中的 Gist。
如果我应该提供进一步的代码或解释,请告诉我:]
你的代码很混乱,但如果你想知道从任务(或任何它是什么)开始到今天有多少天,作为一个整数,那么只需做
(Date.today - date_started.to_date).to_i
在 habit.rb 中,我将任何提及方法 .today
更改为 .yesterday
。
现在进度条代表每一天的开始。例如,
显示的数字是“1”,但从第一天开始它仍然应该显示“0”。到一天结束时,11:59pm,它应该说“1”,因为那时习惯已经完成。
habits/index.html.erb
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="10" style="width: <%= challenged.progress_in_percent %>%;">
<%= challenged.calculate_days_lost %>
</div>
<%= challenged.days_left_in_current_level %>
</div>
habit.rb
def real_missed_days
value = 0
levels.each do |level|
value += level.missed_days + level.days_lost
end
value
end
def calculate_days_lost
def n_days
((date_started.to_date)..Date.today).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
n_days
when 10..24
n_days-10
when 25..44
n_days-25
when 45..69
n_days-45
when 70..99
n_days-70
else
n_days-100
end
end
def days_left_in_current_level
def n_days
((date_started.to_date)..Date.today).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
10-n_days
when 10..24
25-n_days
when 25..44
45-n_days
when 45..69
70-n_days
when 70..99
100-n_days
else
0 # No end
end
end
这是其中的 Gist。
如果我应该提供进一步的代码或解释,请告诉我:]
你的代码很混乱,但如果你想知道从任务(或任何它是什么)开始到今天有多少天,作为一个整数,那么只需做
(Date.today - date_started.to_date).to_i
在 habit.rb 中,我将任何提及方法 .today
更改为 .yesterday
。