use helper class for both due date and show from date

This commit is contained in:
Reinier Balt 2014-07-17 15:14:12 +02:00
parent 8ea417c792
commit e0aec34df0
2 changed files with 101 additions and 67 deletions

View file

@ -1,69 +1,131 @@
module DueDateHelper
class DueDateView
class GenericDateView
include ActionView::Context
include ActionView::Helpers
COLORS = ['amber','amber','orange','orange','orange','orange','orange','orange']
COLORS = {
:overdue_by_more_than_one => :red,
:overdue_by_one => :red,
:today => :amber,
:tomorrow => :amber,
:this_week => :orange,
:more_than_a_week => :green
}
def initialize(date, prefs)
@due = date
@days = date.nil? ? nil : self.class.days_from_today(date)
@date = date
@days = date.nil? ? nil : days_from_today(date)
@days_sym = days_to_sym(@days)
@prefs = prefs
end
def get_color
return :red if @days < 0
return :green if @days > 7
return COLORS[@days]
COLORS[@days_sym]
end
def due_text
case @days
def days_from_today(date)
(date.in_time_zone.to_date - Date.current).to_i
end
def days_to_sym(days)
case days
when nil
return nil
when 0
t('todos.next_actions_due_date.due_today')
return :today
when 1
t('todos.next_actions_due_date.due_tomorrow')
return :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
return :this_week
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)
if days == -1
return :overdue_by_one
elsif days < -1
return :overdue_by_more_than_one
else
# more than a week away - relax
t('models.preference.due_in', :days => @days)
return :more_than_a_week
end
end
end
def due_date_html
return "" if @due.nil?
def date_html_wrapper
return "" if @date.nil?
return content_tag(:a, {:title => @prefs.format_date(@due)}) {
content_tag(:span, {:class => get_color}) {
due_text
return content_tag(:a, {:title => @prefs.format_date(@date)}) {
content_tag(:span, {:class => get_color}) {
yield
}
}
}
end
def date_mobile_html_wrapper
return "" if @date.nil?
return content_tag(:span, {:class => get_color}) {
yield
}
end
end
class DueDateView < GenericDateView
def due_text
case @days_sym
when :overdue_by_one
t('todos.next_actions_due_date.overdue_by', :days => @days * -1)
when :overdue_by_more_than_one
t('todos.next_actions_due_date.overdue_by_plural', :days => @days * -1)
when :today
t('todos.next_actions_due_date.due_today')
when :tomorrow
t('todos.next_actions_due_date.due_tomorrow')
when :this_week
if @prefs.due_style == Preference.due_styles[:due_on]
# TODO: internationalize strftime here
t('models.preference.due_on', :date => @date.strftime("%A"))
else
t('models.preference.due_in', :days => @days)
end
else # should be :more_than_a_week
t('models.preference.due_in', :days => @days)
end
end
def due_date_html
date_html_wrapper { due_text }
end
def due_date_mobile_html
return "" if @due == nil
return content_tag(:span, {:class => get_color}) {
@prefs.format_date(@due)
}
date_mobile_html_wrapper { @prefs.format_date(@due) }
end
def self.days_from_today(date)
(date.in_time_zone.to_date - Date.current).to_i
end
class ShowFromDateView < GenericDateView
def show_from_text
case @days_sym
when :overdue_by_more_than_one, :overdue_by_one
t('todos.scheduled_overdue', :days => @days * -1)
when :today
t('todos.show_today')
when :tomorrow
t('todos.show_tomorrow')
when :this_week
if @prefs.due_style == Preference.due_styles[:due_on]
t('todos.show_on_date', :date => @date.strftime("%A"))
else
t('todos.show_in_days', :days => @days.to_s)
end
else
t('todos.show_in_days', :days => @days.to_s)
end
end
def show_from_date_html
date_html_wrapper { show_from_text }
end
end

View file

@ -349,39 +349,11 @@ module TodosHelper
end
end
def show_date_tag(date, the_class, text)
content_tag(:a, :title => format_date(date)) do
content_tag(:span, :class => the_class) { text }
end
end
# Check show_from date in comparison to today's date Flag up date
# appropriately with a 'traffic light' colour code
#
def show_date(d)
return "" if d == nil
days = DueDateHelper::DueDateView.days_from_today(d)
case days
# overdue or due very soon! sound the alarm!
when -1000..-1
show_date_tag(d, :red, t('todos.scheduled_overdue', :days => (days * -1).to_s))
when 0
show_date_tag(d, :amber, t('todos.show_today'))
when 1
show_date_tag(d, :amber, t('todos.show_tomorrow'))
# due 2-7 days away
when 2..7
if prefs.due_style == Preference.due_styles[:due_on]
show_date_tag(d, :orange, t('todos.show_on_date', :date => d.strftime("%A")) )
else
show_date_tag(d, :orange, t('todos.show_in_days', :days => days.to_s) )
end
# more than a week away - relax
else
show_date_tag(d, :green, t('todos.show_in_days', :days => days.to_s) )
end
def show_date(date)
return DueDateHelper::ShowFromDateView.new(date, prefs).show_from_date_html
end
def date_field_tag(name, id, value = nil, options = {})