-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/dalyons/redmine-todos-scrum…
…-plugin into dalyons/master Conflicts: app/views/todos/_new_todo.html.erb
- Loading branch information
Showing
30 changed files
with
491 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,55 @@ | ||
#require 'ruby-debug' | ||
|
||
class MytodosController < ApplicationController | ||
#Inherits from the Todos controller to save repition of all the todo methods. | ||
#Since todos attach through the Todoable interface, all you have to do is override | ||
#the parent_object method, which finds/loads the object that the todos belong to (User/Project/etc). | ||
#TODO: This could be done as a mixin module, something like 'acts_as_todoable'. | ||
class MytodosController < TodosController | ||
|
||
#put in a filthy hack to reload the patches to core redmine models. | ||
#If cache_classes is off, the patches are dropped when the classes reload on every request. | ||
#So, we reapply the patches here - for some reason it doesnt work in the Todo model. | ||
#TODO: you are very welcome to find a better way to do this! | ||
#unless Rails.configuration.cache_classes | ||
# unloadable | ||
# User.send(:include, TodosUserPatch) | ||
#end | ||
|
||
|
||
before_filter :authorize | ||
helper :todos | ||
before_filter :set_user | ||
|
||
def index | ||
#@user = User.current | ||
|
||
#get all the root level todos belonging to current user | ||
#@todos = User.current.todos.select{|t| t.parent_id == nil } | ||
@personal_todos = Todo.personal_todos.for_user(User.current.id).roots | ||
@personal_todos = @user.todos.roots | ||
|
||
#find the roots of any project todo that belongs to or was authored by the user | ||
#(The root itself may not belong to the user, but we still want to display it!) | ||
@project_todos = Todo.project_todos.for_user(User.current.id).collect{|t| t.root}.uniq | ||
@project_todos = Todo.project_todos.for_user(@user.id).collect{|t| t.root}.uniq | ||
|
||
#group the results by project, into a hash keyed on project. | ||
#this line is so beautiful it nearly made me cry! | ||
@grouped_project_todos = Set.new(@project_todos).classify{|t| t.project } | ||
|
||
|
||
@new_todo = Todo.new(:author_id => User.current.id) | ||
end | ||
|
||
def new | ||
@todo = Todo.new | ||
@todo.parent_id = Todo.find(params[:parent_id]).id | ||
@todo.assigned_to = User.current | ||
render :partial => 'new_todo', | ||
:locals => { :todo => @todo} | ||
end | ||
|
||
def create | ||
@todo = Todo.new(params[:todo]) | ||
@todo.author = User.current | ||
@grouped_project_todos = Set.new(@project_todos).classify{|t| t.todoable } | ||
|
||
#debugger | ||
if @todo.save | ||
|
||
if (request.xhr?) | ||
@element_html = render_to_string :partial => 'todos/todo_li', | ||
:locals => { :todo => @todo, :editable => true } | ||
render :template => 'todos/create.rjs' #using rjs | ||
else | ||
flash[:notice] = @todo.errors.collect{|k,m| m}.join | ||
redirect_to :action => "index" | ||
end | ||
else | ||
flash.now[:error] = @todo.errors.collect{|k,m| m}.join | ||
respond_to do |format| | ||
format.html { redirect_to :action => "index" } | ||
format.js { render :template => 'todos/create.rjs' } | ||
end | ||
end | ||
@new_todo = @user.todos.new(:author_id => @user.id) | ||
end | ||
|
||
def destroy | ||
@todo = Todo.find_by_user(params[:id], User.current.id) | ||
|
||
if @todo.destroy | ||
render :text => "" | ||
else | ||
render :text => @todo.errors.collect{|k,m| m}.join | ||
end | ||
end | ||
|
||
def show | ||
begin | ||
@todo = Todo.for_user(User.current.id).find(params[:id]) | ||
rescue ActiveRecord::RecordNotFound => ex | ||
raise ex, l(:todo_not_found_error) | ||
end | ||
|
||
if @todo | ||
respond_to do |format| | ||
format.html { render :template => 'todos/show'} | ||
#format.js { render :template => 'todos/show' } | ||
end | ||
else | ||
flash.now[:error] = @todo.errors.collect{|k,m| m}.join | ||
respond_to do |format| | ||
format.html { redirect_to :action => 'index' } | ||
#format.js { render :action => 'edit' } | ||
end | ||
end | ||
end | ||
|
||
def toggle_complete | ||
@todo = Todo.for_user(User.current.id).find(params[:id]) | ||
@todo.set_done !@todo.done | ||
if (request.xhr?) | ||
@element_html = render_to_string :partial => 'todos/todo', | ||
:locals => {:todo => @todo, :editable => true} | ||
render :template => 'todos/todo.rjs' | ||
else | ||
redirect_to :action => "index", :project_id => params[:project_id] | ||
end | ||
end | ||
|
||
def sort | ||
|
||
@todos = Todo.for_user(User.current) | ||
protected | ||
def parent_object | ||
todoable = User.current #you can only ever view your own mytodos. | ||
raise ActiveRecord::RecordNotFound, "TODO association not FOUND! " if !todoable | ||
|
||
params.keys.select{|k| k.include? TodosController::UL_ID }.each do |key| | ||
Todo.sort_todos(@todos,params[key]) | ||
end | ||
|
||
render :nothing => true | ||
#render :template => 'todos/sort.rjs' | ||
return todoable | ||
end | ||
|
||
def edit | ||
if request.post? | ||
@todo = Todo.for_user(User.current).find(params[:id]) | ||
if @todo.update_attributes(:text => params[:text]) | ||
respond_to do |format| | ||
format.html { redirect_to :controller => 'mytodos', :action => 'index' } | ||
format.js { render :action => 'update' } | ||
end | ||
else | ||
flash.now[:error] = @todo.errors.collect{|k,m| m}.join | ||
respond_to do |format| | ||
format.html { redirect_to :action => 'index' } | ||
format.js { render :action => 'edit' } | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
#override the usual authenitcation to something more appropriate for global, personal todos. | ||
def authorize | ||
action = {:controller => params[:controller], :action => params[:action]} | ||
allowed = User.current.allowed_to?(action, project = nil, options={:global => true}) | ||
allowed ? true : deny_access | ||
end | ||
|
||
def set_user | ||
@user = parent_object | ||
end | ||
|
||
|
||
end |
Oops, something went wrong.