move as_text helpers to respective pattern

This commit is contained in:
Reinier Balt 2014-02-23 15:54:02 +01:00
parent cbdbb792a5
commit d8507bf8b7
6 changed files with 77 additions and 85 deletions

View file

@ -90,90 +90,12 @@ class RecurringTodo < ActiveRecord::Base
end
end
def recurring_target_as_text
case self.target
when 'due_date'
I18n.t("todos.recurrence.pattern.due")
when 'show_from_date'
I18n.t("todos.recurrence.pattern.show")
else
raise Exception.new, "unexpected value of recurrence target '#{self.target}'"
end
end
def daily_recurrence_pattern
if only_work_days
I18n.t("todos.recurrence.pattern.on_work_days")
elsif every_other1 > 1
I18n.t("todos.recurrence.pattern.every_n", :n => every_other1) + " " + I18n.t("common.days_midsentence.other")
else
I18n.t("todos.recurrence.pattern.every_day")
end
end
def weekly_recurrence_pattern
if every_other1 > 1
I18n.t("todos.recurrence.pattern.every_n", :n => every_other1) + " " + I18n.t("common.weeks")
else
I18n.t('todos.recurrence.pattern.weekly')
end
end
def monthly_recurrence_pattern
return "invalid repeat pattern" if every_other2.nil?
if self.recurrence_selector == 0
on_day = " #{I18n.t('todos.recurrence.pattern.on_day_n', :n => self.every_other1)}"
if self.every_other2>1
I18n.t("todos.recurrence.pattern.every_n", :n => self.every_other2) + " " + I18n.t('common.months') + on_day
else
I18n.t("todos.recurrence.pattern.every_month") + on_day
end
else
n_months = if self.every_other2 > 1
"#{self.every_other2} #{I18n.t('common.months')}"
else
I18n.t('common.month')
end
I18n.t('todos.recurrence.pattern.every_xth_day_of_every_n_months',
:x => self.xth, :day => self.day_of_week, :n_months => n_months)
end
end
def yearly_recurrence_pattern
if self.recurrence_selector == 0
I18n.t("todos.recurrence.pattern.every_year_on",
:date => I18n.l(DateTime.new(Time.zone.now.year, self.every_other2, self.every_other1), :format => :month_day))
else
I18n.t("todos.recurrence.pattern.every_year_on",
:date => I18n.t("todos.recurrence.pattern.the_xth_day_of_month", :x => self.xth, :day => self.day_of_week, :month => self.month_of_year))
end
end
def recurrence_pattern
return "invalid repeat pattern" if every_other1.nil?
case recurring_period
when 'daily' then daily_recurrence_pattern
when 'weekly' then weekly_recurrence_pattern
when 'monthly' then monthly_recurrence_pattern
when 'yearly' then yearly_recurrence_pattern
else
'unknown recurrence pattern: period unknown'
end
pattern.recurrence_pattern
end
def xth
xth_day = [
I18n.t('todos.recurrence.pattern.first'),I18n.t('todos.recurrence.pattern.second'),I18n.t('todos.recurrence.pattern.third'),
I18n.t('todos.recurrence.pattern.fourth'),I18n.t('todos.recurrence.pattern.last')]
self.every_other3.nil? ? '??' : xth_day[self.every_other3-1]
end
def day_of_week
self.every_count.nil? ? '??' : I18n.t('todos.recurrence.pattern.day_names')[self.every_count]
end
def month_of_year
self.every_other2.nil? ? '??' : I18n.t('todos.recurrence.pattern.month_names')[self.every_other2]
def recurring_target_as_text
pattern.recurring_target_as_text
end
def starred?

View file

@ -32,6 +32,29 @@ module RecurringTodos
get :show_from_delta
end
def recurring_target_as_text
target == 'due_date' ? I18n.t("todos.recurrence.pattern.due") : I18n.t("todos.recurrence.pattern.show")
end
def recurrence_pattern
raise "Should not call AbstractRepeatPattern.recurrence_pattern directly. Overwrite in subclass"
end
def xth(x)
xth_day = [
I18n.t('todos.recurrence.pattern.first'),I18n.t('todos.recurrence.pattern.second'),I18n.t('todos.recurrence.pattern.third'),
I18n.t('todos.recurrence.pattern.fourth'),I18n.t('todos.recurrence.pattern.last')]
x.nil? ? '??' : xth_day[x-1]
end
def day_of_week_as_text(day)
day.nil? ? '??' : I18n.t('todos.recurrence.pattern.day_names')[day]
end
def month_of_year_as_text(month)
month.nil? ? '??' : I18n.t('todos.recurrence.pattern.month_names')[month]
end
def build_recurring_todo(attribute_handler)
@recurring_todo = @user.recurring_todos.build(attribute_handler.safe_attributes)
end

View file

@ -14,6 +14,16 @@ module RecurringTodos
get :only_work_days
end
def recurrence_pattern
if only_work_days?
I18n.t("todos.recurrence.pattern.on_work_days")
elsif every_x_days > 1
I18n.t("todos.recurrence.pattern.every_n", :n => every_x_days) + " " + I18n.t("common.days_midsentence.other")
else
I18n.t("todos.recurrence.pattern.every_day")
end
end
def validate
super
errors[:base] << "Every other nth day may not be empty for this daily recurrence setting" if (!only_work_days?) && every_x_days.blank?

View file

@ -22,10 +22,6 @@ module RecurringTodos
get(:recurrence_selector) == 1
end
def every_xth_day
get :every_other2
end
def every_x_month
# in case monthly pattern is every day x, return every_other2 otherwise
# return a default value
@ -46,6 +42,25 @@ module RecurringTodos
get :every_count
end
def recurrence_pattern
if recurrence_selector == 0
on_day = " #{I18n.t('todos.recurrence.pattern.on_day_n', :n => every_x_day)}"
if every_xth_day(0) > 1
I18n.t("todos.recurrence.pattern.every_n", :n => every_xth_day) + " " + I18n.t('common.months') + on_day
else
I18n.t("todos.recurrence.pattern.every_month") + on_day
end
else
n_months = if get(:every_other2) > 1
"#{get(:every_other2)} #{I18n.t('common.months')}"
else
I18n.t('common.month')
end
I18n.t('todos.recurrence.pattern.every_xth_day_of_every_n_months',
:x => xth(every_xth_day), :day => day_of_week_as_text(day_of_week), :n_months => n_months)
end
end
def validate
super

View file

@ -20,6 +20,14 @@ module RecurringTodos
get(:every_day) && get(:every_day)[n, 1] != ' '
end
def recurrence_pattern
if every_x_week > 1
I18n.t("todos.recurrence.pattern.every_n", :n => every_x_week) + " " + I18n.t("common.weeks")
else
I18n.t('todos.recurrence.pattern.weekly')
end
end
def validate
super
validate_not_blank(every_x_week, "Every other nth week may not be empty for weekly recurrence setting")

View file

@ -32,6 +32,20 @@ module RecurringTodos
get(:recurrence_selector) == 1 ? get(:every_other2) : Time.zone.now.month
end
def recurrence_pattern
if self.recurrence_selector == 0
I18n.t("todos.recurrence.pattern.every_year_on",
:date => I18n.l(DateTime.new(Time.zone.now.year, month_of_year, every_x_day), :format => :month_day))
else
I18n.t("todos.recurrence.pattern.every_year_on",
:date => I18n.t("todos.recurrence.pattern.the_xth_day_of_month",
:x => xth(every_xth_day),
:day => day_of_week_as_text(day_of_week),
:month => month_of_year_as_text(month_of_year)
))
end
end
def validate
super
case recurrence_selector