tracks/app/models/recurring_todo.rb

143 lines
3.9 KiB
Ruby
Raw Normal View History

class RecurringTodo < ApplicationRecord
2008-07-19 20:27:45 +02:00
belongs_to :context
belongs_to :project
belongs_to :user
has_many :todos
2013-04-26 16:30:40 -05:00
2013-05-11 23:13:32 +02:00
scope :active, -> { where state: 'active'}
scope :completed, -> { where state: 'completed'}
2012-07-13 00:31:11 +02:00
include IsTaggable
2011-05-16 15:42:47 +08:00
include AASM
aasm :column => :state do
2015-02-10 15:49:13 +01:00
state :active, :initial => true, :before_enter => Proc.new { |t| t.occurrences_count = 0 }
state :completed, :before_enter => Proc.new { |t| t.completed_at = Time.zone.now }, :before_exit => Proc.new { |t| t.completed_at = nil }
event :complete do
transitions :to => :completed, :from => [:active]
end
event :activate do
transitions :to => :active, :from => [:completed]
end
end
validates_presence_of :description, :recurring_period, :target, :ends_on, :context
2008-07-19 20:27:45 +02:00
validates_length_of :description, :maximum => 100
validates_length_of :notes, :maximum => 60000, :allow_nil => true
2008-07-19 20:27:45 +02:00
validate :period_validation
validate :pattern_specific_validations
def pattern_specific_validations
if pattern
pattern.validate
else
errors[:recurring_todo] << "Invalid recurrence period '#{recurring_period}'"
end
end
def valid_period?
%W[daily weekly monthly yearly].include?(recurring_period)
end
def period_validation
errors.add(:recurring_period, "is an unknown recurrence pattern: '#{recurring_period}'") unless valid_period?
end
2008-07-19 20:27:45 +02:00
# the following recurrence patterns can be stored:
#
2008-07-19 20:27:45 +02:00
# daily todos - recurrence_period = 'daily'
# every nth day - nth stored in every_other1
# every work day - only_work_days = true
# tracks will choose between both options using only_work_days
# weekly todos - recurrence_period = 'weekly'
# every nth week on a specific day -
# nth stored in every_other1 and the specific day is stored in every_day
# monthly todos - recurrence_period = 'monthly'
# every day x of nth month - x stored in every_other1 and nth is stored in every_other2
# the xth y-day of every nth month (the forth tuesday of every 2 months) -
# x stored in every_other3, y stored in every_count, nth stored in every_other2
# choosing between both options is done on recurrence_selector where 0 is
# for first type and 1 for second type
# yearly todos - recurrence_period = 'yearly'
# every day x of month y - x is stored in every_other1, y is stored in every_other2
# the x-th day y of month z (the forth tuesday of september) -
# x is stored in every_other3, y is stored in every_count, z is stored in every_other2
# choosing between both options is done on recurrence_selector where 0 is
# for first type and 1 for second type
def pattern
if valid_period?
@pattern = eval("RecurringTodos::#{recurring_period.capitalize}RecurrencePattern.new(user)")
@pattern.build_from_recurring_todo(self)
2008-07-19 20:27:45 +02:00
end
@pattern
2008-07-19 20:27:45 +02:00
end
2012-07-13 00:31:11 +02:00
def recurrence_pattern
pattern.recurrence_pattern
2008-07-19 20:27:45 +02:00
end
def recurring_target_as_text
pattern.recurring_target_as_text
2008-07-19 20:27:45 +02:00
end
2008-07-19 20:27:45 +02:00
def starred?
has_tag?(Todo::STARRED_TAG_NAME)
end
2008-07-19 20:27:45 +02:00
def get_due_date(previous)
pattern.get_due_date(previous)
2008-07-19 20:27:45 +02:00
end
2008-07-19 20:27:45 +02:00
def get_show_from_date(previous)
pattern.get_show_from_date(previous)
2008-07-19 20:27:45 +02:00
end
def done?(end_date)
!continues_recurring?(end_date)
end
2008-07-19 20:27:45 +02:00
def toggle_completion!
completed? ? activate! : complete!
2008-07-19 20:27:45 +02:00
end
2008-07-19 20:27:45 +02:00
def toggle_star!
if starred?
2012-07-13 00:31:11 +02:00
_remove_tags(Todo::STARRED_TAG_NAME)
2008-07-19 20:27:45 +02:00
else
_add_tags(Todo::STARRED_TAG_NAME)
end
2012-07-13 00:31:11 +02:00
tags.reload
starred?
2008-07-19 20:27:45 +02:00
end
def remove_from_project!
self.project = nil
self.save
2014-08-14 21:05:05 -05:00
end
def clear_todos_association
unless todos.nil?
self.todos.each do |t|
t.recurring_todo = nil
t.save
end
end
end
def increment_occurrences
2015-02-10 15:49:13 +01:00
self.occurrences_count += 1
2008-07-19 20:27:45 +02:00
self.save
end
def continues_recurring?(previous)
pattern.continues_recurring?(previous)
end
2014-08-14 21:05:05 -05:00
end