diff --git a/tracks/app/models/context.rb b/tracks/app/models/context.rb index dd33c714..64ecf47b 100644 --- a/tracks/app/models/context.rb +++ b/tracks/app/models/context.rb @@ -1,11 +1,11 @@ class Context < ActiveRecord::Base - has_many :todos, :dependent => :delete_all, :order => "completed_at DESC" + has_many :todos, :dependent => :delete_all, :include => :project, :order => "completed_at DESC" belongs_to :user acts_as_list :scope => :user extend NamePartFinder - acts_as_todo_container :find_todos_include => :project + include Tracks::TodoList attr_protected :user diff --git a/tracks/app/models/project.rb b/tracks/app/models/project.rb index d8b73fe6..6bd833b4 100644 --- a/tracks/app/models/project.rb +++ b/tracks/app/models/project.rb @@ -1,5 +1,5 @@ class Project < ActiveRecord::Base - has_many :todos, :dependent => :delete_all + has_many :todos, :dependent => :delete_all, :include => :context has_many :notes, :dependent => true, :order => "created_at DESC" belongs_to :user @@ -13,7 +13,7 @@ class Project < ActiveRecord::Base acts_as_list :scope => :user acts_as_state_machine :initial => :active, :column => 'state' extend NamePartFinder - acts_as_todo_container :find_todos_include => :context + include Tracks::TodoList state :active state :hidden, :enter => :hide_todos, :exit => :unhide_todos diff --git a/tracks/config/environment.rb.tmpl b/tracks/config/environment.rb.tmpl index 2741e3eb..d2d3a842 100644 --- a/tracks/config/environment.rb.tmpl +++ b/tracks/config/environment.rb.tmpl @@ -63,13 +63,9 @@ SALT = "change-me" AUTHENTICATION_SCHEMES = ['database'] require 'name_part_finder' -require 'acts_as_todo_container' +require 'todo_list' require 'config' -ActiveRecord::Base.class_eval do - include Tracks::Acts::TodoContainer -end - if (AUTHENTICATION_SCHEMES.include? 'ldap') require 'net/ldap' #requires ruby-net-ldap gem be installed require 'simple_ldap_authenticator' diff --git a/tracks/db/schema.rb b/tracks/db/schema.rb index 2246ed71..dab4a41e 100644 --- a/tracks/db/schema.rb +++ b/tracks/db/schema.rb @@ -6,9 +6,9 @@ ActiveRecord::Schema.define(:version => 20) do create_table "contexts", :force => true do |t| t.column "name", :string, :default => "", :null => false + t.column "hide", :integer, :limit => 4, :default => 0, :null => false t.column "position", :integer, :default => 0, :null => false - t.column "hide", :boolean, :default => false - t.column "user_id", :integer, :default => 1 + t.column "user_id", :integer, :default => 0, :null => false end create_table "notes", :force => true do |t| @@ -56,7 +56,7 @@ ActiveRecord::Schema.define(:version => 20) do create_table "projects", :force => true do |t| t.column "name", :string, :default => "", :null => false t.column "position", :integer, :default => 0, :null => false - t.column "user_id", :integer, :default => 1 + t.column "user_id", :integer, :default => 0, :null => false t.column "description", :text t.column "state", :string, :limit => 20, :default => "active", :null => false end @@ -71,22 +71,22 @@ ActiveRecord::Schema.define(:version => 20) do create_table "todos", :force => true do |t| t.column "context_id", :integer, :default => 0, :null => false - t.column "project_id", :integer - t.column "description", :string, :default => "", :null => false + t.column "description", :string, :limit => 100, :default => "", :null => false t.column "notes", :text t.column "created_at", :datetime t.column "due", :date t.column "completed_at", :datetime - t.column "user_id", :integer, :default => 1 + t.column "project_id", :integer + t.column "user_id", :integer, :default => 0, :null => false t.column "show_from", :date t.column "state", :string, :limit => 20, :default => "immediate", :null => false end create_table "users", :force => true do |t| - t.column "login", :string, :limit => 80, :default => "", :null => false - t.column "password", :string, :limit => 40, :default => "", :null => false + t.column "login", :string, :limit => 80 + t.column "password", :string, :limit => 40 t.column "word", :string - t.column "is_admin", :boolean, :default => false, :null => false + t.column "is_admin", :integer, :limit => 4, :default => 0, :null => false t.column "first_name", :string t.column "last_name", :string t.column "auth_type", :string, :default => "database", :null => false diff --git a/tracks/lib/acts_as_todo_container.rb b/tracks/lib/acts_as_todo_container.rb deleted file mode 100644 index ffe24104..00000000 --- a/tracks/lib/acts_as_todo_container.rb +++ /dev/null @@ -1,76 +0,0 @@ -module Tracks - module Acts #:nodoc: - module TodoContainer #:nodoc: - - # This act provides the capabilities for finding todos that belong to the entity - - def self.included(base) #:nodoc: - base.extend ActMacro - end - - module ActMacro - def acts_as_todo_container(opts = {}) - - opts[:find_todos_include] = [] unless opts.key?(:find_todos_include) - opts[:find_todos_include] = [opts[:find_todos_include]] unless opts[:find_todos_include].is_a?(Array) - write_inheritable_attribute :find_todos_include, [base_class.name.singularize.downcase] + opts[:find_todos_include] - - class_inheritable_reader :find_todos_include - - class_eval "include Tracks::Acts::TodoContainer::InstanceMethods" - end - end - - module InstanceMethods - - def not_done_todos(opts={}) - @not_done_todos ||= self.find_not_done_todos(opts) - end - - def done_todos - @done_todos ||= self.find_done_todos - end - - def find_not_done_todos(opts={}) - where_state_sql = "todos.state = 'active'" - if opts.has_key?(:include_project_hidden_todos) && (opts[:include_project_hidden_todos] == true) - where_state_sql = "(todos.state = 'active' or todos.state = 'project_hidden')" - end - todos = Todo.find(:all, - :conditions => ["todos.#{self.class.base_class.name.singularize.downcase}_id = ? and #{where_state_sql}", id], - :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC", - :include => find_todos_include) - - end - - def find_done_todos - todos = Todo.find(:all, :conditions => ["todos.#{self.class.base_class.name.singularize.downcase}_id = ? AND todos.state = ?", id, "completed"], - :order => "completed_at DESC", - :include => find_todos_include, - :limit => self.user.preference.show_number_completed) - end - - def not_done_todo_count(opts={}) - where_state_sql = "todos.state = 'active'" - if opts.has_key?(:include_project_hidden_todos) && (opts[:include_project_hidden_todos] == true) - where_state_sql = "(todos.state = 'active' or todos.state = 'project_hidden')" - end - Todo.count(:conditions => ["todos.#{self.class.base_class.name.singularize.downcase}_id = ? and #{where_state_sql}", id], - :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC", - :include => find_todos_include) - - end - - def done_todo_count - Todo.count(:conditions => ["todos.#{self.class.base_class.name.singularize.downcase}_id = ? AND todos.state = ?", id, "completed"], - :order => "completed_at DESC", - :include => find_todos_include, - :limit => self.user.preference.show_number_completed) - end - - end - - - end - end -end diff --git a/tracks/lib/todo_list.rb b/tracks/lib/todo_list.rb new file mode 100644 index 00000000..c13a4420 --- /dev/null +++ b/tracks/lib/todo_list.rb @@ -0,0 +1,39 @@ +module Tracks + module TodoList + + def not_done_todos(opts={}) + @not_done_todos ||= self.find_not_done_todos(opts) + end + + def done_todos + @done_todos ||= self.find_done_todos + end + + def find_not_done_todos(opts={}) + self.todos.find(:all, :conditions => not_done_conditions(opts), + :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC") + end + + def not_done_conditions(opts) + conditions = ["todos.state = ?", 'active'] + if opts.has_key?(:include_project_hidden_todos) && (opts[:include_project_hidden_todos] == true) + conditions = ["(todos.state = ? or todos.state = ?)", 'active', 'project_hidden'] + end + conditions + end + + def find_done_todos + self.todos.find(:all, :conditions => ["todos.state = ?", "completed"], + :order => "todos.completed_at DESC", :limit => self.user.preference.show_number_completed) + end + + def not_done_todo_count(opts={}) + self.todos.count(not_done_conditions(opts)) + end + + def done_todo_count + self.todos.count_in_state(:completed) + end + + end +end