refactor due date tag on todo for mobile and non-mobile view

This commit is contained in:
Reinier Balt 2014-07-17 13:56:08 +02:00
parent b7126998b2
commit bafd55cf09
4 changed files with 82 additions and 65 deletions

View file

@ -106,7 +106,7 @@ class ApplicationController < ActionController::Base
# config/settings.yml
#
def format_date(date)
return date ? date.in_time_zone(prefs.time_zone).strftime("#{prefs.date_format}") : ''
return prefs.format_date(date)
end
def for_autocomplete(coll, substr)

View file

@ -25,80 +25,19 @@ module ApplicationHelper
def navigation_link(name, options = {}, html_options = nil, *parameters_for_method_reference)
link_to name, options, html_options
end
def days_from_today(date)
(date.in_time_zone.to_date - Date.current).to_i
end
# Check due date in comparison to today's date Flag up date appropriately with
# a 'traffic light' colour code
#
def due_date(due)
return "" if due.nil?
days = days_from_today(due)
colors = ['amber','amber','orange','orange','orange','orange','orange','orange']
color = :red if days < 0
color = :green if days > 7
color = colors[days] if color.nil?
return content_tag(:a, {:title => format_date(due)}) {
content_tag(:span, {:class => color}) {
case days
when 0
t('todos.next_actions_due_date.due_today')
when 1
t('todos.next_actions_due_date.due_tomorrow')
when 2..7
if prefs.due_style == Preference.due_styles[:due_on]
# TODO: internationalize strftime here
t('models.preference.due_on', :date => due.strftime("%A"))
else
t('models.preference.due_in', :days => days)
end
else
# overdue or due very soon! sound the alarm!
if days == -1
t('todos.next_actions_due_date.overdue_by', :days => days * -1)
elsif days < -1
t('todos.next_actions_due_date.overdue_by_plural', :days => days * -1)
else
# more than a week away - relax
t('models.preference.due_in', :days => days)
end
end
}
}
return DueDateHelper::DueDateView.new(due, prefs).due_date_html
end
# Check due date in comparison to today's date Flag up date appropriately with
# a 'traffic light' colour code Modified method for mobile screen
#
def due_date_mobile(due)
if due == nil
return ""
end
days = days_from_today(due)
case days
when 0
"<span class=\"amber\">"+ format_date(due) + "</span>"
when 1
"<span class=\"amber\">" + format_date(due) + "</span>"
# due 2-7 days away
when 2..7
"<span class=\"orange\">" + format_date(due) + "</span>"
else
# overdue or due very soon! sound the alarm!
if days < 0
"<span class=\"red\">" + format_date(due) +"</span>"
else
# more than a week away - relax
"<span class=\"green\">" + format_date(due) + "</span>"
end
end
return DueDateHelper::DueDateView.new(due, prefs).due_date_mobile_html
end
# Returns a count of next actions in the given context or project. The result

View file

@ -0,0 +1,73 @@
module DueDateHelper
class DueDateView
include ActionView::Context
include ActionView::Helpers
COLORS = ['amber','amber','orange','orange','orange','orange','orange','orange']
def initialize(date, prefs)
@due = date
@days = date.nil? ? nil : days_from_today(date)
@prefs = prefs
end
def get_color
return :red if @days < 0
return :green if @days > 7
return COLORS[@days]
end
def due_text
case @days
when 0
t('todos.next_actions_due_date.due_today')
when 1
t('todos.next_actions_due_date.due_tomorrow')
when 2..7
if @prefs.due_style == Preference.due_styles[:due_on]
# TODO: internationalize strftime here
t('models.preference.due_on', :date => @due.strftime("%A"))
else
t('models.preference.due_in', :days => @days)
end
else
# overdue or due very soon! sound the alarm!
if @days == -1
t('todos.next_actions_due_date.overdue_by', :days => @days * -1)
elsif @days < -1
t('todos.next_actions_due_date.overdue_by_plural', :days => @days * -1)
else
# more than a week away - relax
t('models.preference.due_in', :days => @days)
end
end
end
def due_date_html
return "" if @due.nil?
return content_tag(:a, {:title => @prefs.format_date(@due)}) {
content_tag(:span, {:class => get_color}) {
due_text
}
}
end
def due_date_mobile_html
return "" if @due == nil
return content_tag(:span, {:class => get_color}) {
@prefs.format_date(@due)
}
end
private
def days_from_today(date)
(date.in_time_zone.to_date - Date.current).to_i
end
end
end

View file

@ -24,4 +24,9 @@ class Preference < ActiveRecord::Base
date.in_time_zone(time_zone).beginning_of_day
end
def format_date (date)
return date ? date.in_time_zone(time_zone).strftime("#{date_format}") : ''
end
end