Permalink
Browse files

started the migration to the Todoable interface

  • Loading branch information...
dalyons committed Sep 30, 2009
1 parent 13f80c8 commit ae21fd7defffeaa1d36b49516dcca80a6ded477a
@@ -1,4 +1,4 @@
-#require 'ruby-debug'
+require 'ruby-debug'
class TodosController < ApplicationController
@@ -26,7 +26,8 @@ def index
@allowed_to_edit = User.current.allowed_to?(:edit_todos, @project)
- @new_todo = Todo.new
+ @new_todo = parent_object.todos.new(:assigned_to => User.current) #Todo.new
+
end
@@ -64,11 +65,13 @@ def show
end
def new
- @todo = Todo.new
+ @todo = parent_object.todos.new
@todo.parent_id = Todo.for_project(@project.id).find(params[:parent_id]).id
- @todo.issue_id = Issue.find(params[:issue_id]).id
- @todo.project = @project
+ @todo.issue = Issue.find(params[:issue_id]).id if params[:issue_id]
@todo.assigned_to = User.current
+
+ #@todo.todoable = parent_object
+
render :partial => 'new_todo', :locals => { :todo => @todo}
end
@@ -87,10 +90,11 @@ def toggle_complete
def create
- @todo = Todo.new(params[:todo])
- @todo.project = @project
+ @todo = @project.todos.new(params[:todo])
+ #@todo.todoable = @project
@todo.author = User.current
+
if @todo.save
if (request.xhr?)
@element_html = render_to_string :partial => 'todo_li',
@@ -147,4 +151,22 @@ def find_project
@project = Project.find(params[:project_id])
raise ActiveRecord::RecordNotFound, l(:todo_project_not_found_error) + " id:" + params[:project_id] unless @project
end
+
+
+ #TODO: there may be a better way...
+ def parent_object
+ todoable =
+ case
+ when params[:user_id] then User.find(params[:user_id])
+ when params[:project_id] then Project.find(params[:project_id])
+ #when params[:todo_template_id] then TodoTemplate.find(params[:todo_template_id])
+ end
+ raise ActiveRecord::RecordNotFound, "TODO association not FOUND! " if !todoable
+ return todoable
+ end
+
+ def find_todo
+ @todo = parent_object.todos.find(params[:id])
+ raise ActiveRecord::RecordNotFound, "TODO NOT FOUND! id:" + params[:id] unless @todo
+ end
end
View
@@ -25,12 +25,16 @@ class Todo < ActiveRecord::Base
belongs_to :project
belongs_to :refers_to, :class_name => 'Issue', :foreign_key => 'issue_id'
+ #todos can belong to many different objects using the polymorphic interface.
+ belongs_to :todoable, :polymorphic => true
+
named_scope :roots, :conditions => {:parent_id => nil }
- named_scope :personal_todos, :conditions => {:project_id => nil}
- named_scope :project_todos, :conditions => ["project_id is not null"]
- named_scope :for_project, lambda {|*args| {:conditions => {:project_id => args.first}} }
- named_scope :for_user, lambda {|*args|
- { :conditions => ["author_id = ? OR assigned_to_id = ?",args.first, args.first] }
+ named_scope :personal_todos, :conditions => {:todoable_type => User.to_s}
+ named_scope :project_todos, :conditions => {:todoable_type => Project.to_s}
+ named_scope :for_project, lambda {|project_id| {:conditions => {:todoable_type => Project.to_s, :todoable_id => project_id}} }
+ named_scope :for_user, lambda {|user_id|
+ #{:conditions => {:todoable_type => User.to_s, :todoable_id => args.first}}
+ { :conditions => ["author_id = ? OR assigned_to_id = ?",user_id, user_id] }
}
acts_as_event :title => Proc.new {|o|
@@ -2,10 +2,14 @@
#New todo: partial for creating a new todo.
#requires:
# todo : a new Todo object
+ parent_object = todo.todoable
+ raise unless parent_object
+
%>
<div class="new-todo">
- <% remote_form_for :todo, todo, :url => {:controller => "todos", :action=> "create", :project_id => todo.project },
+ <% remote_form_for [parent_object,todo],
+ #:todo, todo, :url => {:controller => "todos", :action=> "create", :project_id => todo.project },
#:update => TodosController::UL_ID + todo.parent_or_root_id, :position => :bottom,
:success => "elements['todo[text]'].activate().clear();" do |f| %>
@@ -33,7 +37,7 @@
<%= f.hidden_field :parent_id %>
<br />
- <%= f.select :assigned_to_id, todo.project.users.collect{|m| [m.name,m.id]},
+ <%= f.select :assigned_to_id, todo.todoable.users.collect{|m| [m.name,m.id]},
{:prompt=> l(:todo_new_assign_to_tooltip) },
{:title => l(:todo_new_assign_to_tooltip) }
%>
@@ -1,5 +1,6 @@
<% todos_controller = (controller.controller_name == 'issues') ? 'todos' : controller.controller_name %>
<% editable ||= false %>
+<% raise unless parent_object %>
<ul class="todos root" id="<%= TodosController::UL_ID + 'root' %><%='not-editable' unless editable %>">
<% for todo in todos.sort{|a,b| a.position <=> b.position} %>
@@ -10,7 +11,8 @@
if editable then
sortable_element(
TodosController::UL_ID + 'root',
- :url => {:controller => todos_controller, :action => 'sort', :project_id => project_id},
+ #:url => {:controller => todos_controller, :action => 'sort', :project_id => project_id},
+ :url => polymorphic_url([parent_object, :todos], :action =>:sort), :method => :post,
:handle => "todo-content", :tree => true
)
end
@@ -1,4 +1,8 @@
<% todos_controller = (controller.controller_name == 'issues') ? 'todos' : controller.controller_name %>
+<%
+ parent_object = todo.todoable
+ raise unless parent_object
+%>
<% editable ||= false %>
@@ -14,18 +18,18 @@
%>
<%= link_to_remote( todo_img,
- :url => { :controller => todos_controller, :action => 'toggle_complete', :id => todo.id, :project_id => todo.project},
- :html => {:class => todo_class} #,
- # :update => "todo_#{todo.id}"
+ :url => polymorphic_url([parent_object,todo], :action => :toggle_complete), :method => :post,
+ :html => {:class => todo_class}
)
%><%= link_to_remote( image_tag('add.png', :title => l(:todo_add_child_tooltip)),
- :url => { :controller => "#{'projects/' + todo.project.identifier.to_s + '/' if todo.project}#{'issues/' + todo_issue.to_s + '/' if todo_issue}#{todos_controller}/#{todo.id}",
- :action => 'new' },
+ :url => new_polymorphic_url( [parent_object,:todo], :parent_id => todo, :issue_id => todo_issue), :method => :get,
+
+ #{ :controller => "#{'projects/' + todo.project.identifier.to_s + '/' if todo.project}#{'issues/' + todo_issue.to_s + '/' if todo_issue}#{todos_controller}/#{todo.id}", :action => 'new' },
:html => {:class => 'icon-new'},
- :update => "todo#{todo.id}-new-box" ) #, :position => :top
+ :update => "todo#{todo.id}-new-box" )
%><%= link_to_remote( image_tag('delete.png', :title => l(:todo_delete_tooltip)),
- :url => { :controller => todos_controller, :action => 'destroy', :id => todo,:project_id => todo.project},
+ :url => polymorphic_url([ parent_object,todo]), :method => :delete,
:confirm => l(:text_are_you_sure), :html => {:class => 'icon-delete'},
:update => "todo_#{todo.id}" )
%>
@@ -1,4 +1,5 @@
-todos_controller = (controller.controller_name == 'issues') ? 'todos' : controller.controller_name
+raise unless @todo.todoable
+
#render a newly created todo, and also recreate the sorter helper for the parents container
#if you dont use page.sortable to recreate, the sorting doesnt work for new elements
@@ -10,7 +11,7 @@ page.insert_html :bottom, parent_ul_id, @element_html
root_ul_id = TodosController::UL_ID + @todo.root.parent_or_root_id
page.sortable(root_ul_id,
{
- :url => {:controller => todos_controller, :action => 'sort', :project_id => @todo.project},
+ :url => polymorphic_url([@todo.todoable, :todos], :action =>:sort), :method => :post,
:handle => "todo-content", :tree => true
})
@@ -6,17 +6,17 @@
<div id=project-todos>
<%= render :partial => 'todos/link_bar' %>
- <h2><%= l(:todos_project_heading) %> <%= @project.name %></h2>
- <div style="" >
-
- <%= render(:partial => 'new_todo',
- :locals => {:todo => Todo.new(:project => @project, :assigned_to => User.current)}) if @allowed_to_edit
- %>
-
- </div>
+ <h2><%= l(:todos_project_heading) %> <%= @project.name %></h2>
+ <div style="" >
+
+ <%= render(:partial => 'new_todo',
+ :locals => {:todo => @new_todo}) if @allowed_to_edit
+ %>
- <%= render :partial => 'root_todos',
- :locals => {:todos => @todos, :project_id => @project, :editable => @allowed_to_edit} %>
+ </div>
+
+ <%= render :partial => 'root_todos',
+ :locals => {:todos => @todos, :parent_object => @project, :editable => @allowed_to_edit} %>
</div>
View
@@ -1,4 +1,5 @@
-todos_controller = (controller.controller_name == 'issues') ? 'todos' : controller.controller_name
+raise unless @todo.todoable
+
#re-render a todo, and also recreate the sorter helper for the parents container
#if you dont use page.sortable to recreate, the sorting doesnt work for reloaded elements
@@ -11,7 +12,7 @@ page.replace_html li_id, @element_html
root_ul_id = TodosController::UL_ID + @todo.root.parent_or_root_id
page.sortable(root_ul_id,
{
- :url => {:controller => todos_controller, :action => 'sort', :project_id => @todo.project},
+ :url => polymorphic_url([@todo.todoable, :todos], :action =>:sort), :method => :post,
:handle => "todo-content", :tree => true
})
@@ -0,0 +1,11 @@
+class AddTodoableFields < ActiveRecord::Migration
+ def self.up
+ add_column :todos, :todoable_id, :integer
+ add_column :todos, :todoable_type, :string
+ end
+
+ def self.down
+ remove_column :todos, :todoable_id
+ remove_column :todos, :todoable_type
+ end
+end
@@ -0,0 +1,22 @@
+class RemoveOldTodoAssociations < ActiveRecord::Migration
+ def self.up
+
+ #turn existing project associated todos into proper Project todos
+ Todo.find(:all,:conditions => 'project_id is not null').each do |todo|
+ todo.update_attributes!(:todoable_type => 'Project', :todoable_id => todo.project_id)
+ end
+
+ #Turn personal todos(authored, no project) into proper User todos
+ Todo.find(:all,:conditions => 'project_id is null').each do |todo|
+ todo.update_attributes!(:todoable_type => 'User', :todoable_id => todo.author_id)
+ end
+
+ remove_column :todos, :project_id
+
+ end
+
+ def self.down
+ add_column :todos, :project_id, :integer
+
+ end
+end
@@ -17,7 +17,7 @@ def self.included(base) # :nodoc:
# Same as typing in the class
Project.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
- has_many :todos
+ has_many :todos, :as => :todoable
#raise ActiveRecord::RecordNotFound, "pie"
#puts "ARRRRGH!!!!!!!!!!" + base.to_s
end
View
@@ -1,14 +1,25 @@
#custom routes for this plugin
-ActionController::Routing::Routes.draw do |map|
- map.with_options :controller => 'todos' do |todos_routes|
- todos_routes.connect "projects/:project_id/todos/:action/:id"
- todos_routes.connect "projects/:project_id/todos/:action/:id.:format"
- todos_routes.connect 'projects/:project_id/issues/:issue_id/todos/:parent_id/new', :action => 'new'
- todos_routes.connect 'projects/:project_id/issues/:issue_id/todos/:parent_id/new.:format', :action => 'new'
- end
+
+ #map.with_options :controller => 'todos' do |todos_routes|
+
+ #todos_routes.new_issue_todo 'projects/:project_id/issues/:issue_id/todos/:parent_id/new', :action => 'new'
+ #todos_routes.new_project_todo 'projects/:project_id/todos/:parent_id/new', :action => 'new'
+ #todos_routes.connect 'projects/:project_id/issues/:issue_id/todos/:parent_id/new(.:format)', :action => 'new'
+ #todos_routes.connect "projects/:project_id/todos/:action/:id"
+ #todos_routes.connect "projects/:project_id/todos/:action/:id.:format"
+
+ #end
+ map.resources :todos, :name_prefix => 'project_', :path_prefix => '/projects/:project_id',
+ :member => {:toggle_complete => :post }, :collection => {:sort => :post}
+
+ #map.resources :mytodos, :name_prefix => 'project_', :path_prefix => '/projects/:project_id'
+
map.with_options :controller => 'mytodos' do |mytodos_routes|
- mytodos_routes.connect 'mytodos/:parent_id/new', :action => 'new'
+ mytodos_routes.new_personal_todo 'mytodos/:parent_id/new', :action => 'new'
mytodos_routes.connect 'mytodos/:parent_id/new.:format', :action => 'new'
end
-end
+
+ # map.resources :comments, :path_prefix => '/articles/:article_id'
+
+

0 comments on commit ae21fd7

Please sign in to comment.