diff --git a/.gitignore b/.gitignore index 35dee02b..48cd1291 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,8 @@ .dotest /.emacs-project config/database.yml +config/site.yml config/deploy.rb -config/environment.rb db/*.sqlite3 db/data.yml db/schema.rb diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 8a329f56..4c63b89f 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -20,7 +20,7 @@ class CannotAccessContext < RuntimeError; end class ApplicationController < ActionController::Base - protect_from_forgery :secret => SALT + protect_from_forgery :secret => SITE_CONFIG['salt'] helper :application include LoginSystem diff --git a/app/controllers/contexts_controller.rb b/app/controllers/contexts_controller.rb index 2412ba95..98ddc344 100644 --- a/app/controllers/contexts_controller.rb +++ b/app/controllers/contexts_controller.rb @@ -138,7 +138,7 @@ class ContextsController < ApplicationController @active_contexts = @contexts.active @hidden_contexts = @contexts.hidden @down_count = @active_contexts.size + @hidden_contexts.size - cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url]= {:value => request.request_uri, :secure => SITE_CONFIG['secure_cookies']} render :action => 'index_mobile' end end @@ -148,7 +148,7 @@ class ContextsController < ApplicationController @page_title = "TRACKS::List actions in "+@context.name @not_done = @not_done_todos.select {|t| t.context_id == @context.id } @down_count = @not_done.size - cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url]= {:value => request.request_uri, :secure => SITE_CONFIG['secure_cookies']} @mobile_from_context = @context.id render :action => 'mobile_show_context' end diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index 91cfc261..360a8455 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -21,10 +21,10 @@ class LoginController < ApplicationController session['noexpiry'] = params['user_noexpiry'] msg = (should_expire_sessions?) ? "will expire after 1 hour of inactivity." : "will not expire." notify :notice, "Login successful: session #{msg}" - cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year, :secure => TRACKS_COOKIES_SECURE } + cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year, :secure => SITE_CONFIG['secure_cookies'] } unless should_expire_sessions? @user.remember_me - cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at, :secure => TRACKS_COOKIES_SECURE } + cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at, :secure => SITE_CONFIG['secure_cookies'] } end redirect_back_or_home return @@ -100,10 +100,10 @@ class LoginController < ApplicationController session['user_id'] = @user.id msg = (should_expire_sessions?) ? "will expire after 1 hour of inactivity." : "will not expire." notify :notice, "Login successful: session #{msg}" - cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year, :secure => TRACKS_COOKIES_SECURE } + cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year, :secure => SITE_CONFIG['secure_cookies'] } unless should_expire_sessions? @user.remember_me - cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at, :secure => TRACKS_COOKIES_SECURE } + cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at, :secure => SITE_CONFIG['secure_cookies'] } end redirect_back_or_home else diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 1c8dcb5f..4e739fe9 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -106,9 +106,9 @@ class ProjectsController < ApplicationController def update params['project'] ||= {} if params['project']['state'] - @state_changed = @project.state != params['project']['state'] + @new_state = params['project']['state'] + @state_changed = @project.state != @new_state logger.info "@state_changed: #{@project.state} == #{params['project']['state']} != #{@state_changed}" - @project.transition_to(params['project']['state']) params['project'].delete('state') end success_text = if params['field'] == 'name' && params['value'] @@ -117,6 +117,7 @@ class ProjectsController < ApplicationController end @project.attributes = params['project'] if @project.save + @project.transition_to(@new_state) if @state_changed if boolean_param('wants_render') if (@project.hidden?) @project_project_hidden_todo_counts = Hash.new @@ -217,7 +218,7 @@ class ProjectsController < ApplicationController @hidden_projects = @projects.hidden @completed_projects = @projects.completed @down_count = @active_projects.size + @hidden_projects.size + @completed_projects.size - cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url]= {:value => request.request_uri, :secure => SITE_CONFIG['secure_cookies']} render :action => 'index_mobile' end end @@ -230,7 +231,7 @@ class ProjectsController < ApplicationController @project_default_context = "The default context for this project is "+ @project.default_context.name end - cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url]= {:value => request.request_uri, :secure => SITE_CONFIG['secure_cookies']} @mobile_from_project = @project.id render :action => 'project_mobile' end diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 93bdb23e..0c604533 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -11,7 +11,13 @@ class SearchController < ApplicationController @found_notes = current_user.notes.find(:all, :conditions => ["body LIKE ?", terms]) @found_contexts = current_user.contexts.find(:all, :conditions => ["name LIKE ?", terms]) # TODO: limit search to tags on todos - @found_tags = current_user.tags.find(:all, :conditions => ["name LIKE ?", terms]) + @found_tags = Tagging.find_by_sql([ + "SELECT DISTINCT tags.name as name "+ + "FROM tags "+ + "LEFT JOIN taggings ON tags.id = taggings.tag_id "+ + "LEFT JOIN todos ON taggings.taggable_id = todos.id "+ + "WHERE todos.user_id=? "+ + "AND tags.name LIKE ? ", current_user.id, terms]) @count = @found_todos.size + @found_projects.size + @found_notes.size + @found_contexts.size + @found_tags.size diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 2a7009e4..3847c47a 100755 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -652,7 +652,7 @@ class StatsController < ApplicationController "AND tags.id = taggings.tag_id " + "AND taggings.taggable_id = todos.id ", current_user.id]) tags_ids_s = tag_ids.map(&:id).sort.join(",") - return {} if tags_ids_s.blank? # return empty array for .size to work + return {} if tags_ids_s.blank? # return empty hash for .size to work return Tag.find(:all, :conditions => "id in (" + tags_ids_s + ")") end diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 70f149b2..ca1e37c1 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -65,6 +65,7 @@ class TodosController < ApplicationController @todo.context_id = context.id end + @todo.update_state_from_project @saved = @todo.save unless (@saved == false) || p.tag_list.blank? @todo.tag_with(p.tag_list) @@ -231,7 +232,9 @@ class TodosController < ApplicationController @todo.activate! end - @saved = @todo.update_attributes params["todo"] + @todo.attributes = params["todo"] + @saved = @todo.save + @context_changed = @original_item_context_id != @todo.context_id @todo_was_activated_from_deferred_state = @original_item_was_deferred && @todo.active? @@ -255,7 +258,11 @@ class TodosController < ApplicationController end @project_changed = @original_item_project_id != @todo.project_id - if (@project_changed && !@original_item_project_id.nil?) then @remaining_undone_in_project = current_user.projects.find(@original_item_project_id).not_done_todo_count; end + if (@project_changed && !@original_item_project_id.nil?) then + @todo.update_state_from_project + @todo.save! + @remaining_undone_in_project = current_user.projects.find(@original_item_project_id).not_done_todo_count + end determine_down_count respond_to do |format| format.js @@ -263,7 +270,7 @@ class TodosController < ApplicationController format.m do if @saved if cookies[:mobile_url] - cookies[:mobile_url] = {:value => nil, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url] = {:value => nil, :secure => SITE_CONFIG['secure_cookies']} redirect_to cookies[:mobile_url] else redirect_to formatted_todos_path(:m) @@ -416,7 +423,7 @@ class TodosController < ApplicationController @default_project_context_name_map = build_default_project_context_name_map(@projects).to_json } format.m { - cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url]= {:value => request.request_uri, :secure => SITE_CONFIG['secure_cookies']} render :action => "mobile_tag" } end @@ -714,7 +721,7 @@ class TodosController < ApplicationController lambda do @page_title = "All actions" @home = true - cookies[:mobile_url]= { :value => request.request_uri, :secure => TRACKS_COOKIES_SECURE} + cookies[:mobile_url]= { :value => request.request_uri, :secure => SITE_CONFIG['secure_cookies']} determine_down_count render :action => 'index' diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 549f2fbc..ff01fcae 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -24,7 +24,7 @@ module ApplicationHelper end def days_from_today(date) - date.to_date - user_time.to_date + date.in_time_zone.to_date - user_time.to_date end # Check due date in comparison to today's date Flag up date appropriately with diff --git a/app/models/todo.rb b/app/models/todo.rb index 97a1b984..5f231221 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -1,176 +1,184 @@ -class Todo < ActiveRecord::Base - - belongs_to :context - belongs_to :project - belongs_to :user - belongs_to :recurring_todo - - named_scope :active, :conditions => { :state => 'active' } - named_scope :not_completed, :conditions => ['NOT state = ? ', 'completed'] - named_scope :are_due, :conditions => ['NOT todos.due IS NULL'] - - STARRED_TAG_NAME = "starred" - - acts_as_state_machine :initial => :active, :column => 'state' - - # when entering active state, also remove completed_at date. Looks like :exit - # of state completed is not run, see #679 - state :active, :enter => Proc.new { |t| t[:show_from], t.completed_at = nil, nil } - state :project_hidden - state :completed, :enter => Proc.new { |t| t.completed_at = Time.zone.now }, :exit => Proc.new { |t| t.completed_at = nil } - state :deferred - - event :defer do - transitions :to => :deferred, :from => [:active] - end - - event :complete do - transitions :to => :completed, :from => [:active, :project_hidden, :deferred] - end - - event :activate do - transitions :to => :active, :from => [:project_hidden, :completed, :deferred] - end - - event :hide do - transitions :to => :project_hidden, :from => [:active, :deferred] - end - - event :unhide do - transitions :to => :deferred, :from => [:project_hidden], :guard => Proc.new{|t| !t.show_from.blank? } - transitions :to => :active, :from => [:project_hidden] - end - - attr_protected :user - - # Description field can't be empty, and must be < 100 bytes Notes must be < - # 60,000 bytes (65,000 actually, but I'm being cautious) - validates_presence_of :description - validates_length_of :description, :maximum => 100 - validates_length_of :notes, :maximum => 60000, :allow_nil => true - validates_presence_of :show_from, :if => :deferred? - validates_presence_of :context - - def validate - if !show_from.blank? && show_from < user.date - errors.add("show_from", "must be a date in the future") - end - end - - def toggle_completion! - saved = false - if completed? - saved = activate! - else - saved = complete! - end - return saved - end - - def show_from - self[:show_from] - end - - def show_from=(date) - # parse Date objects into the proper timezone - date = user.at_midnight(date) if (date.is_a? Date) - activate! if deferred? && date.blank? - defer! if active? && !date.blank? && date > user.date - self[:show_from] = date - end - - alias_method :original_project, :project - - def project - original_project.nil? ? Project.null_object : original_project - end - - alias_method :original_set_initial_state, :set_initial_state - - def set_initial_state - if show_from && (show_from > user.date) - write_attribute self.class.state_column, 'deferred' - else - original_set_initial_state - end - end - - alias_method :original_run_initial_state_actions, :run_initial_state_actions - - def run_initial_state_actions - # only run the initial state actions if the standard initial state hasn't - # been changed - if self.class.initial_state.to_sym == current_state - original_run_initial_state_actions - end - end - - def self.feed_options(user) - { - :title => 'Tracks Actions', - :description => "Actions for #{user.display_name}" - } - end - - def starred? - tags.any? {|tag| tag.name == STARRED_TAG_NAME} - end - - def toggle_star! - if starred? - _remove_tags STARRED_TAG_NAME - tags.reload - else - _add_tags(STARRED_TAG_NAME) - tags.reload - end - starred? - end - - def from_recurring_todo? - return self.recurring_todo_id != nil - end - - # Rich Todo API - - def self.from_rich_message(user, default_context_id, description, notes) - fields = description.match(/([^>@]*)@?([^>]*)>?(.*)/) - description = fields[1].strip - context = fields[2].strip - project = fields[3].strip - - context = nil if context == "" - project = nil if project == "" - - context_id = default_context_id - unless(context.nil?) - found_context = user.active_contexts.find_by_namepart(context) - found_context = user.contexts.find_by_namepart(context) if found_context.nil? - context_id = found_context.id unless found_context.nil? - end - - unless user.contexts.exists? context_id - raise(CannotAccessContext, "Cannot access a context that does not belong to this user.") - end - - project_id = nil - unless(project.blank?) - if(project[0..3].downcase == "new:") - found_project = user.projects.build - found_project.name = project[4..255+4].strip - found_project.save! - else - found_project = user.active_projects.find_by_namepart(project) - found_project = user.projects.find_by_namepart(project) if found_project.nil? - end - project_id = found_project.id unless found_project.nil? - end - - todo = user.todos.build - todo.description = description - todo.notes = notes - todo.context_id = context_id - todo.project_id = project_id unless project_id.nil? - return todo - end -end +class Todo < ActiveRecord::Base + + belongs_to :context + belongs_to :project + belongs_to :user + belongs_to :recurring_todo + + named_scope :active, :conditions => { :state => 'active' } + named_scope :not_completed, :conditions => ['NOT state = ? ', 'completed'] + named_scope :are_due, :conditions => ['NOT todos.due IS NULL'] + + STARRED_TAG_NAME = "starred" + + acts_as_state_machine :initial => :active, :column => 'state' + + # when entering active state, also remove completed_at date. Looks like :exit + # of state completed is not run, see #679 + state :active, :enter => Proc.new { |t| t[:show_from], t.completed_at = nil, nil } + state :project_hidden + state :completed, :enter => Proc.new { |t| t.completed_at = Time.zone.now }, :exit => Proc.new { |t| t.completed_at = nil } + state :deferred + + event :defer do + transitions :to => :deferred, :from => [:active] + end + + event :complete do + transitions :to => :completed, :from => [:active, :project_hidden, :deferred] + end + + event :activate do + transitions :to => :active, :from => [:project_hidden, :completed, :deferred] + end + + event :hide do + transitions :to => :project_hidden, :from => [:active, :deferred] + end + + event :unhide do + transitions :to => :deferred, :from => [:project_hidden], :guard => Proc.new{|t| !t.show_from.blank? } + transitions :to => :active, :from => [:project_hidden] + end + + attr_protected :user + + # Description field can't be empty, and must be < 100 bytes Notes must be < + # 60,000 bytes (65,000 actually, but I'm being cautious) + validates_presence_of :description + validates_length_of :description, :maximum => 100 + validates_length_of :notes, :maximum => 60000, :allow_nil => true + validates_presence_of :show_from, :if => :deferred? + validates_presence_of :context + + def validate + if !show_from.blank? && show_from < user.date + errors.add("show_from", "must be a date in the future") + end + end + + def update_state_from_project + if state == 'project_hidden' and !project.hidden? + self.state = 'active' + elsif state == 'active' and project.hidden? + self.state = 'project_hidden' + end + end + + def toggle_completion! + saved = false + if completed? + saved = activate! + else + saved = complete! + end + return saved + end + + def show_from + self[:show_from] + end + + def show_from=(date) + # parse Date objects into the proper timezone + date = user.at_midnight(date) if (date.is_a? Date) + activate! if deferred? && date.blank? + defer! if active? && !date.blank? && date > user.date + self[:show_from] = date + end + + alias_method :original_project, :project + + def project + original_project.nil? ? Project.null_object : original_project + end + + alias_method :original_set_initial_state, :set_initial_state + + def set_initial_state + if show_from && (show_from > user.date) + write_attribute self.class.state_column, 'deferred' + else + original_set_initial_state + end + end + + alias_method :original_run_initial_state_actions, :run_initial_state_actions + + def run_initial_state_actions + # only run the initial state actions if the standard initial state hasn't + # been changed + if self.class.initial_state.to_sym == current_state + original_run_initial_state_actions + end + end + + def self.feed_options(user) + { + :title => 'Tracks Actions', + :description => "Actions for #{user.display_name}" + } + end + + def starred? + tags.any? {|tag| tag.name == STARRED_TAG_NAME} + end + + def toggle_star! + if starred? + _remove_tags STARRED_TAG_NAME + tags.reload + else + _add_tags(STARRED_TAG_NAME) + tags.reload + end + starred? + end + + def from_recurring_todo? + return self.recurring_todo_id != nil + end + + # Rich Todo API + + def self.from_rich_message(user, default_context_id, description, notes) + fields = description.match(/([^>@]*)@?([^>]*)>?(.*)/) + description = fields[1].strip + context = fields[2].strip + project = fields[3].strip + + context = nil if context == "" + project = nil if project == "" + + context_id = default_context_id + unless(context.nil?) + found_context = user.active_contexts.find_by_namepart(context) + found_context = user.contexts.find_by_namepart(context) if found_context.nil? + context_id = found_context.id unless found_context.nil? + end + + unless user.contexts.exists? context_id + raise(CannotAccessContext, "Cannot access a context that does not belong to this user.") + end + + project_id = nil + unless(project.blank?) + if(project[0..3].downcase == "new:") + found_project = user.projects.build + found_project.name = project[4..255+4].strip + found_project.save! + else + found_project = user.active_projects.find_by_namepart(project) + found_project = user.projects.find_by_namepart(project) if found_project.nil? + end + project_id = found_project.id unless found_project.nil? + end + + todo = user.todos.build + todo.description = description + todo.notes = notes + todo.context_id = context_id + todo.project_id = project_id unless project_id.nil? + return todo + end +end diff --git a/app/views/contexts/show.html.erb b/app/views/contexts/show.html.erb index a22e4728..41268330 100644 --- a/app/views/contexts/show.html.erb +++ b/app/views/contexts/show.html.erb @@ -8,5 +8,5 @@