diff --git a/README b/README
index 3e6d8f9d..28cf4e46 100644
--- a/README
+++ b/README
@@ -20,4 +20,4 @@ For those upgrading, change notes are available in /doc/CHANGELOG. If you are th
While fully usable for everyday use, Tracks is still a work in progress. Make sure that you take sensible precautions and back up all your data frequently, taking particular care when you are upgrading.
-Enjoy being productive!
\ No newline at end of file
+Enjoy being productive!
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index 2cfba813..3d26cdd0 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -1,5 +1,6 @@
-# The filters added to this controller will be run for all controllers in the application.
-# Likewise will all the methods added be available for all controllers.
+# The filters added to this controller will be run for all controllers in the
+# application. Likewise will all the methods added be available for all
+# controllers.
require_dependency "login_system"
require_dependency "tracks/source_view"
@@ -47,11 +48,12 @@ class ApplicationController < ActionController::Base
# http://wiki.rubyonrails.com/rails/show/HowtoChangeSessionOptions
unless session == nil
return if @controller_name == 'feed' or session['noexpiry'] == "on"
- # If the method is called by the feed controller (which we don't have under session control)
- # or if we checked the box to keep logged in on login
- # don't set the session expiry time.
+ # If the method is called by the feed controller (which we don't have
+ # under session control) or if we checked the box to keep logged in on
+ # login don't set the session expiry time.
if session
- # Get expiry time (allow ten seconds window for the case where we have none)
+ # Get expiry time (allow ten seconds window for the case where we have
+ # none)
expiry_time = session['expiry_time'] || Time.now + 10
if expiry_time < Time.now
# Too late, matey... bang goes your session!
@@ -80,10 +82,10 @@ class ApplicationController < ActionController::Base
# end
# end
- # Returns a count of next actions in the given context or project
- # The result is count and a string descriptor, correctly pluralised if there are no
+ # Returns a count of next actions in the given context or project The result
+ # is count and a string descriptor, correctly pluralised if there are no
# actions or multiple actions
- #
+ #
def count_undone_todos_phrase(todos_parent, string="actions")
count = count_undone_todos(todos_parent)
if count == 1
@@ -105,9 +107,9 @@ class ApplicationController < ActionController::Base
count || 0
end
- # Convert a date object to the format specified in the user's preferences
- # in config/settings.yml
- #
+ # Convert a date object to the format specified in the user's preferences in
+ # config/settings.yml
+ #
def format_date(date)
if date
date_format = prefs.date_format
@@ -118,10 +120,10 @@ class ApplicationController < ActionController::Base
formatted_date
end
- # Uses RedCloth to transform text using either Textile or Markdown
- # Need to require redcloth above
- # RedCloth 3.0 or greater is needed to use Markdown, otherwise it only handles Textile
- #
+ # Uses RedCloth to transform text using either Textile or Markdown Need to
+ # require redcloth above RedCloth 3.0 or greater is needed to use Markdown,
+ # otherwise it only handles Textile
+ #
def markdown(text)
RedCloth.new(text).to_html
end
@@ -130,21 +132,19 @@ class ApplicationController < ActionController::Base
Hash[*projects.reject{ |p| p.default_context.nil? }.map{ |p| [p.name, p.default_context.name] }.flatten].to_json
end
- # Here's the concept behind this "mobile content negotiation" hack:
- # In addition to the main, AJAXy Web UI, Tracks has a lightweight
- # low-feature 'mobile' version designed to be suitablef or use
- # from a phone or PDA. It makes some sense that tne pages of that
- # mobile version are simply alternate representations of the same
- # Todo resources. The implementation goal was to treat mobile
- # as another format and be able to use respond_to to render both
- # versions. Unfortunately, I ran into a lot of trouble simply
- # registering a new mime type 'text/html' with format :m because
- # :html already is linked to that mime type and the new
- # registration was forcing all html requests to be rendered in
- # the mobile view. The before_filter and after_filter hackery
- # below accomplishs that implementation goal by using a 'fake'
- # mime type during the processing and then setting it to
- # 'text/html' in an 'after_filter' -LKM 2007-04-01
+ # Here's the concept behind this "mobile content negotiation" hack: In
+ # addition to the main, AJAXy Web UI, Tracks has a lightweight low-feature
+ # 'mobile' version designed to be suitablef or use from a phone or PDA. It
+ # makes some sense that tne pages of that mobile version are simply alternate
+ # representations of the same Todo resources. The implementation goal was to
+ # treat mobile as another format and be able to use respond_to to render both
+ # versions. Unfortunately, I ran into a lot of trouble simply registering a
+ # new mime type 'text/html' with format :m because :html already is linked to
+ # that mime type and the new registration was forcing all html requests to be
+ # rendered in the mobile view. The before_filter and after_filter hackery
+ # below accomplishs that implementation goal by using a 'fake' mime type
+ # during the processing and then setting it to 'text/html' in an
+ # 'after_filter' -LKM 2007-04-01
def mobile?
return params[:format] == 'm' || response.content_type == MOBILE_CONTENT_TYPE
end
@@ -220,9 +220,9 @@ class ApplicationController < ActionController::Base
end
end
- # Set the contents of the flash message from a controller
- # Usage: notify :warning, "This is the message"
- # Sets the flash of type 'warning' to "This is the message"
+ # Set the contents of the flash message from a controller Usage: notify
+ # :warning, "This is the message" Sets the flash of type 'warning' to "This is
+ # the message"
def notify(type, message)
flash[type] = message
logger.error("ERROR: #{message}") if type == :error
@@ -231,5 +231,29 @@ class ApplicationController < ActionController::Base
def set_time_zone
Time.zone = current_user.prefs.time_zone if logged_in?
end
+
+ def create_todo_from_recurring_todo(rt, date=nil)
+ # create todo and initialize with data from recurring_todo rt
+ todo = current_user.todos.build( { :description => rt.description, :notes => rt.notes, :project_id => rt.project_id, :context_id => rt.context_id})
+
+ # set dates
+ todo.due = rt.get_due_date(date)
+ todo.show_from = rt.get_show_from_date(date)
+ todo.recurring_todo_id = rt.id
+ saved = todo.save
+ if saved
+ todo.tag_with(rt.tag_list, current_user)
+ todo.tags.reload
+ end
+
+ # increate number of occurences created from recurring todo
+ rt.inc_occurences
+
+ # mark recurring todo complete if there are no next actions left
+ checkdate = todo.due.nil? ? todo.show_from : todo.due
+ rt.toggle_completion! unless rt.has_next_todo(checkdate)
+
+ return saved ? todo : nil
+ end
end
diff --git a/app/controllers/recurring_todos_controller.rb b/app/controllers/recurring_todos_controller.rb
new file mode 100644
index 00000000..1f9d6939
--- /dev/null
+++ b/app/controllers/recurring_todos_controller.rb
@@ -0,0 +1,237 @@
+class RecurringTodosController < ApplicationController
+
+ helper :todos, :recurring_todos
+
+ append_before_filter :init, :only => [:index, :new, :edit]
+ append_before_filter :get_recurring_todo_from_param, :only => [:destroy, :toggle_check, :toggle_star, :edit, :update]
+
+ def index
+ @recurring_todos = current_user.recurring_todos.find(:all, :conditions => ["state = ?", "active"])
+ @completed_recurring_todos = current_user.recurring_todos.find(:all, :conditions => ["state = ?", "completed"])
+ @no_recurring_todos = @recurring_todos.size == 0
+ @no_completed_recurring_todos = @completed_recurring_todos.size == 0
+ @count = @recurring_todos.size
+ end
+
+ def new
+ end
+
+ def show
+ end
+
+ def edit
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def update
+ @recurring_todo.tag_with(params[:tag_list], current_user) if params[:tag_list]
+ @original_item_context_id = @recurring_todo.context_id
+ @original_item_project_id = @recurring_todo.project_id
+
+ # we needed to rename the recurring_period selector in the edit form
+ # because the form for a new recurring todo and the edit form are on the
+ # same page.
+ params['recurring_todo']['recurring_period']=params['recurring_edit_todo']['recurring_period']
+
+ # update project
+ if params['recurring_todo']['project_id'].blank? && !params['project_name'].nil?
+ if params['project_name'] == 'None'
+ project = Project.null_object
+ else
+ project = current_user.projects.find_by_name(params['project_name'].strip)
+ unless project
+ project = current_user.projects.build
+ project.name = params['project_name'].strip
+ project.save
+ @new_project_created = true
+ end
+ end
+ params["recurring_todo"]["project_id"] = project.id
+ end
+
+ # update context
+ if params['recurring_todo']['context_id'].blank? && !params['context_name'].blank?
+ context = current_user.contexts.find_by_name(params['context_name'].strip)
+ unless context
+ context = current_user.contexts.build
+ context.name = params['context_name'].strip
+ context.save
+ @new_context_created = true
+ end
+ params["recurring_todo"]["context_id"] = context.id
+ end
+
+ params["recurring_todo"]["weekly_return_monday"]=' ' if params["recurring_todo"]["weekly_return_monday"].nil?
+ params["recurring_todo"]["weekly_return_tuesday"]=' ' if params["recurring_todo"]["weekly_return_tuesday"].nil?
+ params["recurring_todo"]["weekly_return_wednesday"]=' ' if params["recurring_todo"]["weekly_return_wednesday"].nil?
+ params["recurring_todo"]["weekly_return_thursday"]=' ' if params["recurring_todo"]["weekly_return_thursday"].nil?
+ params["recurring_todo"]["weekly_return_friday"]=' ' if params["recurring_todo"]["weekly_return_friday"].nil?
+ params["recurring_todo"]["weekly_return_saturday"]=' ' if params["recurring_todo"]["weekly_return_saturday"].nil?
+ params["recurring_todo"]["weekly_return_sunday"]=' ' if params["recurring_todo"]["weekly_return_sunday"].nil?
+
+ @saved = @recurring_todo.update_attributes params["recurring_todo"]
+
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def create
+ p = RecurringTodoCreateParamsHelper.new(params)
+ @recurring_todo = current_user.recurring_todos.build(p.attributes)
+
+ if p.project_specified_by_name?
+ project = current_user.projects.find_or_create_by_name(p.project_name)
+ @new_project_created = project.new_record_before_save?
+ @recurring_todo.project_id = project.id
+ end
+
+ if p.context_specified_by_name?
+ context = current_user.contexts.find_or_create_by_name(p.context_name)
+ @new_context_created = context.new_record_before_save?
+ @recurring_todo.context_id = context.id
+ end
+
+ @recurring_saved = @recurring_todo.save
+ unless (@recurring_saved == false) || p.tag_list.blank?
+ @recurring_todo.tag_with(p.tag_list, current_user)
+ @recurring_todo.tags.reload
+ end
+
+ if @recurring_saved
+ @message = "The recurring todo was saved"
+ @todo_saved = create_todo_from_recurring_todo(@recurring_todo).nil? == false
+ if @todo_saved
+ @message += " / created a new todo"
+ else
+ @message += " / did not create todo"
+ end
+ @count = current_user.recurring_todos.count(:all, :conditions => ["state = ?", "active"])
+ else
+ @message = "Error saving recurring todo"
+ end
+
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def destroy
+
+ # remove all references to this recurring todo
+ @todos = current_user.todos.find(:all, {:conditions => ["recurring_todo_id = ?", params[:id]]})
+ @number_of_todos = @todos.size
+ @todos.each do |t|
+ t.recurring_todo_id = nil
+ t.save
+ end
+
+ # delete the recurring todo
+ @saved = @recurring_todo.destroy
+ @remaining = current_user.recurring_todos.count(:all)
+
+ respond_to do |format|
+
+ format.html do
+ if @saved
+ notify :notice, "Successfully deleted recurring action", 2.0
+ redirect_to :action => 'index'
+ else
+ notify :error, "Failed to delete the recurring action", 2.0
+ redirect_to :action => 'index'
+ end
+ end
+
+ format.js do
+ render
+ end
+ end
+ end
+
+ def toggle_check
+ @saved = @recurring_todo.toggle_completion!
+
+ @count = current_user.recurring_todos.count(:all, :conditions => ["state = ?", "active"])
+ @remaining = @count
+
+ if @recurring_todo.active?
+ @remaining = current_user.recurring_todos.count(:all, :conditions => ["state = ?", 'completed'])
+
+ # from completed back to active -> check if there is an active todo
+ @active_todos = current_user.todos.count(:all, {:conditions => ["state = ? AND recurring_todo_id = ?", 'active',params[:id]]})
+ # create todo if there is no active todo belonging to the activated
+ # recurring_todo
+ @new_recurring_todo = create_todo_from_recurring_todo(@recurring_todo) if @active_todos == 0
+ end
+
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def toggle_star
+ @recurring_todo.toggle_star!
+ @saved = @recurring_todo.save!
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ class RecurringTodoCreateParamsHelper
+
+ def initialize(params)
+ @params = params['request'] || params
+ @attributes = params['request'] && params['request']['recurring_todo'] || params['recurring_todo']
+ end
+
+ def attributes
+ @attributes
+ end
+
+ def project_name
+ @params['project_name'].strip unless @params['project_name'].nil?
+ end
+
+ def context_name
+ @params['context_name'].strip unless @params['context_name'].nil?
+ end
+
+ def tag_list
+ @params['tag_list']
+ end
+
+ def project_specified_by_name?
+ return false unless @attributes['project_id'].blank?
+ return false if project_name.blank?
+ return false if project_name == 'None'
+ true
+ end
+
+ def context_specified_by_name?
+ return false unless @attributes['context_id'].blank?
+ return false if context_name.blank?
+ true
+ end
+
+ end
+
+ private
+
+ def init
+ @days_of_week = [ ['Sunday',0], ['Monday',1], ['Tuesday', 2], ['Wednesday',3], ['Thursday',4], ['Friday',5], ['Saturday',6]]
+ @months_of_year = [
+ ['January',1], ['Februari',2], ['March', 3], ['April',4], ['May',5], ['June',6],
+ ['July',7], ['August',8], ['September',9], ['October', 10], ['November', 11], ['December',12]]
+ @xth_day = [['first',1],['second',2],['third',3],['fourth',4],['last',5]]
+ @projects = current_user.projects.find(:all, :include => [:default_context])
+ @contexts = current_user.contexts.find(:all)
+ @default_project_context_name_map = build_default_project_context_name_map(@projects).to_json
+ end
+
+ def get_recurring_todo_from_param
+ @recurring_todo = current_user.recurring_todos.find(params[:id])
+ end
+
+end
\ No newline at end of file
diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb
index 69d71586..9448d70c 100644
--- a/app/controllers/todos_controller.rb
+++ b/app/controllers/todos_controller.rb
@@ -121,6 +121,10 @@ class TodosController < ApplicationController
#
def toggle_check
@saved = @todo.toggle_completion!
+
+ # check if this todo has a related recurring_todo. If so, create next todo
+ check_for_next_todo if @saved
+
respond_to do |format|
format.js do
if @saved
@@ -235,6 +239,10 @@ class TodosController < ApplicationController
@todo = get_todo_from_params
@context_id = @todo.context_id
@project_id = @todo.project_id
+
+ # check if this todo has a related recurring_todo. If so, create next todo
+ check_for_next_todo
+
@saved = @todo.destroy
respond_to do |format|
@@ -643,6 +651,19 @@ class TodosController < ApplicationController
def self.is_feed_request(req)
['rss','atom','txt','ics'].include?(req.parameters[:format])
end
+
+ def check_for_next_todo
+ # check if this todo has a related recurring_todo. If so, create next todo
+ @new_recurring_todo = nil
+ @recurring_todo = nil
+ if @todo.from_recurring_todo?
+ @recurring_todo = current_user.recurring_todos.find(@todo.recurring_todo_id)
+ if @recurring_todo.active? && @recurring_todo.has_next_todo(@todo.due)
+ date = @todo.due >= Date.today() ? @todo.due : Date.today()-1.day
+ @new_recurring_todo = create_todo_from_recurring_todo(@recurring_todo, date)
+ end
+ end
+ end
class FindConditionBuilder
@@ -711,6 +732,6 @@ class TodosController < ApplicationController
return false if context_name.blank?
true
end
-
+
end
end
diff --git a/app/helpers/recurring_todos_helper.rb b/app/helpers/recurring_todos_helper.rb
new file mode 100644
index 00000000..b346e7ea
--- /dev/null
+++ b/app/helpers/recurring_todos_helper.rb
@@ -0,0 +1,73 @@
+module RecurringTodosHelper
+
+ def recurrence_time_span(rt)
+ case rt.ends_on
+ when "no_end_date"
+ return ""
+ when "ends_on_number_of_times"
+ return "for "+rt.number_of_occurences.to_s + " times"
+ when "ends_on_end_date"
+ starts = rt.start_from.nil? ? "" : "from " + format_date(rt.start_from)
+ ends = rt.end_date.nil? ? "" : " until " + format_date(rt.end_date)
+ return starts+ends
+ else
+ raise Exception.new, "unknown recurrence time span selection (#{self.ends_on})"
+ end
+ end
+
+ def recurrence_target(rt)
+ case rt.target
+ when 'due_date'
+ return "due"
+ when 'show_from_date'
+ return "show"
+ else
+ return "ERROR"
+ end
+ end
+
+ def recurring_todo_tag_list
+ tags_except_starred = @recurring_todo.tags.reject{|t| t.name == Todo::STARRED_TAG_NAME}
+ tag_list = tags_except_starred.collect{|t| "" +
+ # link_to(t.name, :controller => "todos", :action => "tag", :id =>
+ # t.name) + TODO: tag view for recurring_todos (yet?)
+ t.name +
+ ""}.join('')
+ "#{tag_list}"
+ end
+
+ def recurring_todo_remote_delete_icon
+ str = link_to( image_tag_for_delete,
+ recurring_todo_path(@recurring_todo), :id => "delete_icon_"+@recurring_todo.id.to_s,
+ :class => "icon delete_icon", :title => "delete the recurring action '#{@recurring_todo.description}'")
+ set_behavior_for_delete_icon
+ str
+ end
+
+ def recurring_todo_remote_star_icon
+ str = link_to( image_tag_for_star(@recurring_todo),
+ toggle_star_recurring_todo_path(@recurring_todo),
+ :class => "icon star_item", :title => "star the action '#{@recurring_todo.description}'")
+ set_behavior_for_star_icon
+ str
+ end
+
+ def recurring_todo_remote_edit_icon
+ if !@recurring_todo.completed?
+ str = link_to( image_tag_for_edit(@recurring_todo),
+ edit_recurring_todo_path(@recurring_todo),
+ :class => "icon edit_icon")
+ set_behavior_for_edit_icon
+ else
+ str = '' + image_tag("blank.png") + " "
+ end
+ str
+ end
+
+ def recurring_todo_remote_toggle_checkbox
+ str = check_box_tag('item_id', toggle_check_recurring_todo_path(@recurring_todo), @recurring_todo.completed?, :class => 'item-checkbox')
+ set_behavior_for_toggle_checkbox
+ str
+ end
+
+end
\ No newline at end of file
diff --git a/app/helpers/todos_helper.rb b/app/helpers/todos_helper.rb
index c543bbb0..1e199962 100644
--- a/app/helpers/todos_helper.rb
+++ b/app/helpers/todos_helper.rb
@@ -47,7 +47,7 @@ module TodosHelper
:prevent_default => true
end
- def remote_star_icon
+ def remote_star_icon
str = link_to( image_tag_for_star(@todo),
toggle_star_todo_path(@todo),
:class => "icon star_item", :title => "star the action '#{@todo.description}'")
@@ -66,7 +66,7 @@ module TodosHelper
def remote_edit_icon
if !@todo.completed?
- str = link_to( image_tag_for_edit,
+ str = link_to( image_tag_for_edit(@todo),
edit_todo_path(@todo),
:class => "icon edit_icon")
set_behavior_for_edit_icon
@@ -205,12 +205,12 @@ module TodosHelper
javascript_tag str
end
- def item_container_id
+ def item_container_id (todo)
if source_view_is :project
- return "p#{@todo.project_id}" if @todo.active?
- return "tickler" if @todo.deferred?
+ return "p#{todo.project_id}" if todo.active?
+ return "tickler" if todo.deferred?
end
- return "c#{@todo.context_id}"
+ return "c#{todo.context_id}"
end
def should_show_new_item
@@ -272,8 +272,8 @@ module TodosHelper
image_tag("blank.png", :title =>"Delete action", :class=>"delete_item")
end
- def image_tag_for_edit
- image_tag("blank.png", :title =>"Edit action", :class=>"edit_item", :id=> dom_id(@todo, 'edit_icon'))
+ def image_tag_for_edit(todo)
+ image_tag("blank.png", :title =>"Edit action", :class=>"edit_item", :id=> dom_id(todo, 'edit_icon'))
end
def image_tag_for_star(todo)
@@ -281,4 +281,4 @@ module TodosHelper
image_tag("blank.png", :title =>"Star action", :class => class_str)
end
-end
\ No newline at end of file
+end
diff --git a/app/models/recurring_todo.rb b/app/models/recurring_todo.rb
new file mode 100644
index 00000000..92b331f3
--- /dev/null
+++ b/app/models/recurring_todo.rb
@@ -0,0 +1,517 @@
+class RecurringTodo < ActiveRecord::Base
+
+ belongs_to :context
+ belongs_to :project
+ belongs_to :user
+
+ attr_protected :user
+
+ acts_as_state_machine :initial => :active, :column => 'state'
+
+ state :active, :enter => Proc.new { |t|
+ t[:show_from], t.completed_at = nil, nil
+ t.occurences_count = 0
+ }
+ state :completed, :enter => Proc.new { |t| t.completed_at = Time.now.utc }, :exit => Proc.new { |t| t.completed_at = nil }
+
+ validates_presence_of :description
+ validates_length_of :description, :maximum => 100
+ validates_length_of :notes, :maximum => 60000, :allow_nil => true
+
+ validates_presence_of :context
+
+ event :complete do
+ transitions :to => :completed, :from => [:active]
+ end
+
+ event :activate do
+ transitions :to => :active, :from => [:completed]
+ end
+
+ # the following recurrence patterns can be stored:
+ #
+ # 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
+
+ # DAILY
+
+ def daily_selector=(selector)
+ case selector
+ when 'daily_every_x_day'
+ self.only_work_days = false
+ when 'daily_every_work_day'
+ self.only_work_days = true
+ end
+ end
+
+ def daily_every_x_days=(x)
+ if recurring_period=='daily'
+ self.every_other1 = x
+ end
+ end
+
+ # WEEKLY
+
+ def weekly_every_x_week=(x)
+ if recurring_period=='weekly'
+ self.every_other1 = x
+ end
+ end
+
+ def switch_week_day (day, position)
+ if self.every_day.nil?
+ self.every_day=' '
+ end
+ self.every_day = self.every_day[0,position] + day + self.every_day[position+1,self.every_day.length]
+ end
+
+ def weekly_return_monday=(selector)
+ if recurring_period=='weekly'
+ switch_week_day(selector,1)
+ end
+ end
+
+ def weekly_return_tuesday=(selector)
+ switch_week_day(selector,2) if recurring_period=='weekly'
+ end
+
+ def weekly_return_wednesday=(selector)
+ switch_week_day(selector,3) if recurring_period=='weekly'
+ end
+
+ def weekly_return_thursday=(selector)
+ switch_week_day(selector,4) if recurring_period=='weekly'
+ end
+
+ def weekly_return_friday=(selector)
+ switch_week_day(selector,5) if recurring_period=='weekly'
+ end
+
+ def weekly_return_saturday=(selector)
+ switch_week_day(selector,6) if recurring_period=='weekly'
+ end
+
+ def weekly_return_sunday=(selector)
+ switch_week_day(selector,0) if recurring_period=='weekly'
+ end
+
+ def on_xday(n)
+ unless self.every_day.nil?
+ return self.every_day[n,1] == ' ' ? false : true
+ else
+ return false
+ end
+ end
+
+ def on_monday
+ return on_xday(1)
+ end
+
+ def on_tuesday
+ return on_xday(2)
+ end
+
+ def on_wednesday
+ return on_xday(3)
+ end
+
+ def on_thursday
+ return on_xday(4)
+ end
+
+ def on_friday
+ return on_xday(5)
+ end
+
+ def on_saturday
+ return on_xday(6)
+ end
+
+ def on_sunday
+ return on_xday(0)
+ end
+
+ # MONTHLY
+
+ def monthly_selector=(selector)
+ if recurring_period=='monthly'
+ self.recurrence_selector= (selector=='monthly_every_x_day')? 0 : 1
+ end
+ # todo
+ end
+
+ def monthly_every_x_day=(x)
+ if recurring_period=='monthly'
+ self.every_other1 = x
+ end
+ end
+
+ def monthly_every_x_month=(x)
+ if recurring_period=='monthly'
+ self.every_other2 = x
+ end
+ end
+
+ def monthly_every_xth_day=(x)
+ if recurring_period=='monthly'
+ self.every_other3 = x
+ end
+ end
+
+ def monthly_day_of_week=(dow)
+ if recurring_period=='monthly'
+ self.every_count = dow
+ end
+ end
+
+ # YEARLY
+
+ def yearly_selector=(selector)
+ if recurring_period=='yearly'
+ self.recurrence_selector = (selector=='yearly_every_x_day') ? 0 : 1
+ end
+ end
+
+ def yearly_month_of_year=(moy)
+ if recurring_period=='yearly'
+ self.every_other2 = moy
+ end
+ end
+
+ def yearly_every_x_day=(x)
+ if recurring_period=='yearly'
+ self.every_other1 = x
+ end
+ end
+
+ def yearly_every_xth_day=(x)
+ if recurring_period=='yearly'
+ self.every_other3 = x
+ end
+ end
+
+ def yearly_day_of_week=(dow)
+ if recurring_period=='yearly'
+ self.every_count=dow
+ end
+ end
+
+ # target
+
+ def recurring_target=(t)
+ self.target = t
+ end
+
+ def recurring_show_days_before=(days)
+ self.show_from_delta=days
+ end
+
+ def recurrence_pattern
+ case recurring_period
+ when 'daily'
+ if only_work_days
+ return "on work days"
+ else
+ if every_other1 > 1
+ return "every #{every_other1} days"
+ else
+ return "every day"
+ end
+ end
+ when 'weekly'
+ if every_other1 > 1
+ return "every #{every_other1} weeks"
+ else
+ return 'weekly'
+ end
+ when 'monthly'
+ if self.recurrence_selector == 0
+ return "every month on day #{self.every_other1}"
+ else
+ return "every #{self.xth} #{self.day_of_week} of every #{self.every_other2} month#{self.every_other2>1?'s':''}"
+ end
+ when 'yearly'
+ if self.recurrence_selector == 0
+ return "every year on #{self.month_of_year} #{self.every_other1}"
+ else
+ return "every year on the #{self.xth} #{self.day_of_week} of #{self.month_of_year}"
+ end
+ else
+ return 'unknown recurrence pattern: period unknown'
+ end
+ end
+
+ def xth
+ xth_day = ['first','second','third','fourth','last']
+ return self.every_other3.nil? ? '??' : xth_day[self.every_other3-1]
+ end
+
+ def day_of_week
+ days_of_week = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
+ return (self.every_count.nil? ? '??' : days_of_week[self.every_count])
+ end
+
+ def month_of_year
+ months_of_year = ['January','Februari','March','April','May','June','July','August','September','October','November','December']
+ return self.every_other2.nil? ? '??' : months_of_year[self.every_other2-1]
+ end
+
+ def starred?
+ tags.any? {|tag| tag.name == Todo::STARRED_TAG_NAME}
+ end
+
+ def get_due_date(previous)
+ case self.target
+ when 'due_date'
+ return get_next_date(previous)
+ when 'show_from'
+ # so leave due date empty
+ return nil
+ end
+ end
+
+ def get_show_from_date(previous)
+ case self.target
+ when 'due_date'
+ # so set show from date relative to due date unless show_from_delta is
+ # zero / nil
+ return (self.show_from_delta == 0 || self.show_from_delta.nil?) ? nil : get_due_date(previous) - self.show_from_delta.days
+ when 'show_from_date'
+ # Leave due date empty
+ return get_next_date(previous)
+ else
+ raise Exception.new, "unexpected value of recurrence target '#{self.target}'"
+ end
+ end
+
+ def get_next_date(previous)
+ case self.recurring_period
+ when 'daily'
+ return get_daily_date(previous)
+ when 'weekly'
+ return get_weekly_date(previous)
+ when 'monthly'
+ return get_monthly_date(previous)
+ when 'yearly'
+ return get_yearly_date(previous)
+ else
+ raise Exception.new, "unknown recurrence pattern: '#{self.recurring_period}'"
+ end
+ end
+
+ def get_daily_date(previous)
+ # previous is the due date of the previous todo or it is the completed_at
+ # date when the completed_at date is after due_date (i.e. you did not make
+ # the due date in time)
+ #
+ # assumes self.recurring_period == 'daily'
+ if previous.nil?
+ start = self.start_from.nil? ? Time.now.utc : self.start_from
+ else
+ # use the next day
+ start = previous + 1.day
+ end
+
+ if self.only_work_days
+ if start.wday() >= 1 && start.wday() <= 5 # 1=monday; 5=friday
+ return start
+ else
+ if start.wday() == 0 # sunday
+ return start + 1.day
+ else # saturday
+ return start + 2.day
+ end
+ end
+ else # every nth day; n = every_other1
+ # if there was no previous todo, do not add n: the first todo starts on
+ # today or on start_from
+ return previous == nil ? start : start+every_other1.day-1.day
+ end
+ end
+
+ def get_weekly_date(previous)
+ if previous == nil
+ start = self.start_from.nil? ? Time.now.utc : self.start_from
+ else
+ start = previous + 1.day
+ if start.wday() == 0
+ # we went to a new week , go to the nth next week and find first match
+ # that week
+ start += self.every_other1.week
+ end
+ end
+ # check if there are any days left this week for the next todo
+ start.wday().upto 6 do |i|
+ return start + (i-start.wday()).days unless self.every_day[i,1] == ' '
+ end
+
+ # we did not find anything this week, so check the nth next, starting from
+ # sunday
+ start = start + self.every_other1.week - (start.wday()).days
+
+ # check if there are any days left this week for the next todo
+ start.wday().upto 6 do |i|
+ return start + (i-start.wday()).days unless self.every_day[i,1] == ' '
+ end
+
+ raise Exception.new, "unable to find next weekly date (#{self.every_day})"
+ end
+
+ def get_monthly_date(previous)
+ if previous.nil?
+ start = self.start_from.nil? ? Time.now.utc : self.start_from
+ else
+ start = previous
+ end
+ day = self.every_other1
+ n = self.every_other2
+
+ case self.recurrence_selector
+ when 0 # specific day of the month
+ if start.mday >= day
+ # there is no next day n in this month, search in next month
+ start += n.months
+ # go back to day
+ end
+ return Time.utc(start.year, start.month, day)
+
+ when 1 # relative weekday of a month
+ the_next = get_xth_day_of_month(self.every_other3, self.every_count, start.month, start.year)
+ if the_next.nil? || the_next < start
+ # the nth day is already passed in this month, go to next month and try
+ # again
+ the_next = the_next+n.months
+ # TODO: if there is still no match, start will be set to nil. if we ever
+ # support 5th day of the month, we need to handle this case
+ the_next = get_xth_day_of_month(self.every_other3, self.every_count, the_next.month, the_next.year)
+ end
+ return the_next
+ else
+ raise Exception.new, "unknown monthly recurrence selection (#{self.recurrence_selector})"
+ end
+ return nil
+ end
+
+ def get_xth_day_of_month(x, weekday, month, year)
+ if x == 5
+ # last -> count backwards
+ last_day = Time.utc(year, month, Time.days_in_month(month))
+ while last_day.wday != weekday
+ last_day -= 1.day
+ end
+ return last_day
+ else
+ # 1-4th -> count upwards
+ start = Time.utc(year,month,1)
+ n = x
+ while n > 0
+ while start.wday() != weekday
+ start+= 1.day
+ end
+ n -= 1
+ start += 1.day unless n==0
+ end
+ return start
+ end
+ end
+
+ def get_yearly_date(previous)
+ if previous.nil?
+ start = self.start_from.nil? ? Time.now.utc : self.start_from
+ else
+ start = previous
+ end
+
+ day = self.every_other1
+ month = self.every_other2
+
+ case self.recurrence_selector
+ when 0 # specific day of a specific month
+ # if there is no next month n in this year, search in next year
+ if start.month >= month
+ start = Time.utc(start.year+1, month, 1) if start.day >= day
+ start = Time.utc(start.year, month, 1) if start.day <= day
+ end
+ return Time.utc(start.year, month, day)
+
+ when 1 # relative weekday of a specific month
+ # if there is no next month n in this year, search in next year
+ the_next = start.month > month ? Time.utc(start.year+1, month, 1) : start
+
+ # get the xth day of the month
+ the_next = get_xth_day_of_month(self.every_other3, self.every_count, month, the_next.year)
+
+ # if the_next is before previous, we went back into the past, so try next
+ # year
+ the_next = get_xth_day_of_month(self.every_other3, self.every_count, month, start.year+1) if the_next <= start
+
+ return the_next
+ else
+ raise Exception.new, "unknown monthly recurrence selection (#{self.recurrence_selector})"
+ end
+ return nil
+ end
+
+ def has_next_todo(previous)
+ unless self.number_of_occurences.nil?
+ return self.occurences_count < self.number_of_occurences
+ else
+ if self.end_date.nil?
+ return true
+ else
+ case self.target
+ when 'due_date'
+ return get_due_date(previous) <= self.end_date
+ when 'show_from_date'
+ return get_show_from_date(previous) <= self.end_date
+ else
+ raise Exception.new, "unexpected value of recurrence target '#{self.target}'"
+ end
+ end
+ end
+ end
+
+ def toggle_completion!
+ saved = false
+ if completed?
+ saved = activate!
+ else
+ saved = complete!
+ end
+ return saved
+ end
+
+ def toggle_star!
+ if starred?
+ delete_tags Todo::STARRED_TAG_NAME
+ tags.reload
+ else
+ add_tag Todo::STARRED_TAG_NAME
+ tags.reload
+ end
+ starred?
+ end
+
+ def inc_occurences
+ self.occurences_count += 1
+ self.save
+ end
+
+end
diff --git a/app/models/tag.rb b/app/models/tag.rb
index d3105eeb..81684c13 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -1,6 +1,6 @@
class Tag < ActiveRecord::Base
has_many_polymorphs :taggables,
- :from => [:todos],
+ :from => [:todos, :recurring_todos],
:through => :taggings,
:dependent => :destroy
diff --git a/app/models/todo.rb b/app/models/todo.rb
index cb820a7b..7a6f3892 100644
--- a/app/models/todo.rb
+++ b/app/models/todo.rb
@@ -3,6 +3,7 @@ class Todo < ActiveRecord::Base
belongs_to :context
belongs_to :project
belongs_to :user
+ belongs_to :recurring_todo
STARRED_TAG_NAME = "starred"
@@ -120,4 +121,8 @@ class Todo < ActiveRecord::Base
starred?
end
+ def from_recurring_todo?
+ return self.recurring_todo_id != nil
+ end
+
end
\ No newline at end of file
diff --git a/app/models/user.rb b/app/models/user.rb
index 8273e22a..8afb8e41 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -63,6 +63,9 @@ class User < ActiveRecord::Base
has_many :todos,
:order => 'todos.completed_at DESC, todos.created_at DESC',
:dependent => :delete_all
+ has_many :recurring_todos,
+ :order => 'recurring_todos.completed_at DESC, recurring_todos.created_at DESC',
+ :dependent => :delete_all
has_many :deferred_todos,
:class_name => 'Todo',
:conditions => [ 'state = ?', 'deferred' ],
diff --git a/app/views/layouts/standard.html.erb b/app/views/layouts/standard.html.erb
index 3f3e5b43..de66ec95 100644
--- a/app/views/layouts/standard.html.erb
+++ b/app/views/layouts/standard.html.erb
@@ -18,6 +18,7 @@ window.onload=function(){
Nifty("div#todo_new_action_container","normal");
Nifty("div#project_new_project_container","normal");
Nifty("div#context_new_container","normal");
+ Nifty("div#recurring_new_container","normal");
if ($('flash').visible()) { new Effect.Fade("flash",{duration:5.0}); }
}
@@ -56,6 +57,7 @@ window.onload=function(){
<% if current_user.is_admin? -%>
<%= navigation_link(image_tag("feed-icon.png", :size => "16X16", :border => 0), {:controller => "feedlist", :action => "index"}, :title => "See a list of available feeds" ) %>
<%= navigation_link(image_tag("menustar.gif", :size => "16X16", :border => 0), tag_path("starred"), :title => "See your starred actions" ) %>
+
+ <%= radio_button_tag('recurring_todo[recurring_target]', 'due_date', @recurring_todo.target == 'due_date')%> the date that the todo is due.
+ Show the todo <%=
+ text_field_tag( 'recurring_todo[recurring_show_days_before]', @recurring_todo.show_from_delta, {"size" => 3, "tabindex" => 12}) %>
+ days before the todo is due (0=show always)
+ <%= radio_button_tag('recurring_todo[recurring_target]', 'show_from_date', @recurring_todo.target == 'show_from_date')%> the date todo comes from tickler (no due date set)
+
+
+
+ <%= radio_button_tag('recurring_todo[recurring_target]', 'due_date', true)%> the date that the todo is due.
+ Show the todo <%=
+ text_field_tag( 'recurring_todo[recurring_show_days_before]', "0", {"size" => 3, "tabindex" => 12}) %>
+ days before the todo is due (0=show always)
+ <%= radio_button_tag('recurring_todo[recurring_target]', 'show_from_date', false)%> the date todo comes from tickler (no due date set)
+
+
<%
+
+# need to add behaviour for edit form here. Behaviour defined in partials are
+# not generated for
+apply_behaviour "#recurring_edit_period:click",
+ "TracksForm.hide_all_edit_recurring(); $('recurring_edit_'+TracksForm.get_edit_period()).show();"
+-%>
diff --git a/app/views/recurring_todos/new.html.erb b/app/views/recurring_todos/new.html.erb
new file mode 100644
index 00000000..7eb32683
--- /dev/null
+++ b/app/views/recurring_todos/new.html.erb
@@ -0,0 +1 @@
+<%= render :partial => "recurring_todo_form" %>
\ No newline at end of file
diff --git a/app/views/recurring_todos/show.html.erb b/app/views/recurring_todos/show.html.erb
new file mode 100644
index 00000000..586085f3
--- /dev/null
+++ b/app/views/recurring_todos/show.html.erb
@@ -0,0 +1,2 @@
+
RecurringTodo#show
+
Find me in app/views/recurring_todo/show.html.erb
diff --git a/app/views/recurring_todos/toggle_check.js.rjs b/app/views/recurring_todos/toggle_check.js.rjs
new file mode 100644
index 00000000..aa21b8c9
--- /dev/null
+++ b/app/views/recurring_todos/toggle_check.js.rjs
@@ -0,0 +1,30 @@
+if @saved
+ page[@recurring_todo].remove
+ page['badge_count'].replace_html @count
+
+ if @recurring_todo.completed?
+ # show completed recurring todo
+ page.insert_html :top, "completed_recurring_todos_container", :partial => 'recurring_todos/recurring_todo'
+ page.visual_effect :highlight, dom_id(@recurring_todo), :duration => 3
+
+ # set empty messages
+ page.show 'recurring-todos-empty-nd' if @remaining == 0
+ page.hide 'completed-empty-nd'
+ else
+ # recurring_todo is activated
+
+ # show completed recurring todo
+ page.insert_html :top, "recurring_todos_container", :partial => 'recurring_todos/recurring_todo'
+ page.visual_effect :highlight, dom_id(@recurring_todo), :duration => 3
+
+ # inform user if a new todo has been created because of the activation
+ page.notify :notice, "A new todo was added which belongs to this recurring todo", 3.0 unless @new_recurring_todo.nil?
+
+ # set empty messages
+ page.show 'completed-empty-nd' if @remaining == 0
+ page.hide 'recurring-todos-empty-nd'
+ end
+
+else
+ page.notify :error, "There was an error completing / activating the recurring todo #{@recurring_todo.description}", 8.0
+end
\ No newline at end of file
diff --git a/app/views/recurring_todos/toggle_star.js.rjs b/app/views/recurring_todos/toggle_star.js.rjs
new file mode 100644
index 00000000..f9846bdf
--- /dev/null
+++ b/app/views/recurring_todos/toggle_star.js.rjs
@@ -0,0 +1,3 @@
+if @saved
+ page[@recurring_todo].down('a.star_item').down('img').toggleClassName('starred_todo').toggleClassName('unstarred_todo')
+end
\ No newline at end of file
diff --git a/app/views/recurring_todos/update.js.rjs b/app/views/recurring_todos/update.js.rjs
new file mode 100644
index 00000000..4486c800
--- /dev/null
+++ b/app/views/recurring_todos/update.js.rjs
@@ -0,0 +1,22 @@
+if @saved
+ # hide overlayed edit form
+ page << "TracksForm.toggle_overlay();"
+
+ # show update message
+ status_message = 'Recurring action saved'
+ status_message = 'Added new project / ' + status_message if @new_project_created
+ status_message = 'Added new context / ' + status_message if @new_context_created
+ page.notify :notice, status_message, 5.0
+
+ # update auto completer arrays for context and project
+ page << "contextAutoCompleter.options.array = #{context_names_for_autocomplete}; contextAutoCompleter.changed = true" if @new_context_created
+ page << "projectAutoCompleter.options.array = #{project_names_for_autocomplete}; projectAutoCompleter.changed = true" if @new_project_created
+
+ # replace old recurring todo with updated todo
+ page.replace dom_id(@recurring_todo), :partial => 'recurring_todos/recurring_todo'
+ page.visual_effect :highlight, dom_id(@recurring_todo), :duration => 3
+
+else
+ page.show 'error_status'
+ page.replace_html 'error_status', "#{error_messages_for('todo')}"
+end
\ No newline at end of file
diff --git a/app/views/todos/_todo.html.erb b/app/views/todos/_todo.html.erb
index d4df682f..5ca9522d 100644
--- a/app/views/todos/_todo.html.erb
+++ b/app/views/todos/_todo.html.erb
@@ -13,6 +13,7 @@