Skip to content

Commit

Permalink
Fix the time record feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesu committed Oct 2, 2011
1 parent 3527f8e commit b7b255c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 26 deletions.
70 changes: 56 additions & 14 deletions app/assets/javascripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,42 @@ function stopLoadingForm(evt) {
.find('input, select, textarea, button').attr('disabled', null);
}

function replaceTask(data, content) {
var task = $('#task_item_' + data.id);
var in_list = task.parents('.taskItems:first').parent();

// Replace or insert into correct list
if (data.task_class != null && !in_list.hasClass(data.task_class)) {
var task_list = task.parents('.taskList:first');
in_list.children('.taskItems').insert(task);
}

if (content)
task.html(data.content);
else
task.replaceWith(data.content);
}

function reloadTask(data) {
$.get('/projects/' + PROJECT_ID + '/tasks/' + data.id, {}, function(data){
replaceTask(data, false);
JustRebind();
}, 'json');
}

function updateRunningTimes() {
var time_list = $('#running_times_menu ul li');
var count = time_list.length;
if (count > 0) {
$('#running_times_count span').html('You have ' + count + ' running times');
$('#running_times_count').show();
} else {
$('#running_times_count').hide();
$('#running_times_menu').hide();
time_list.hide();
}
}

function bindDynamic() {

// Popup form for Add Item
Expand Down Expand Up @@ -197,7 +233,7 @@ function bindDynamic() {
.bind('ajax:complete', stopLoading)
.bind('ajax:success',
function(evt, data, status, xhr) {
$('#task_item_' + data.id).replaceWith(data.content);
replaceTask(data, false);
JustRebind();
return false;
});
Expand All @@ -207,7 +243,7 @@ function bindDynamic() {
.bind('ajax:complete', stopLoadingForm)
.bind('ajax:success',
function(evt, data, status, xhr) {
$('#task_item_' + data.id).replaceWith(data.content);
replaceTask(data, false);
JustRebind();
return false;
});
Expand All @@ -220,12 +256,7 @@ function bindDynamic() {
'task[completed]': evt.target.checked
},
function(data, status, xhr) {
// Remove previous
var task_list = $(evt.target).parents('.taskList:first');
$('#task_item_' + data.id).remove();

// Add content in the correct location
task_list.find('.' + data.task_class + ' .taskItems').append(data.content);
replaceTask(data, false);
JustRebind();
return false;
},
Expand All @@ -239,8 +270,7 @@ function bindDynamic() {
.bind('ajax:complete', stopLoading)
.bind('ajax:success',
function(evt, data, status, xhr) {
console.log("WTF", evt, data, status, xhr)
$('#task_item_' + data.id).html(data.content);
replaceTask(data, true);
JustRebind();
return false;
});
Expand All @@ -250,7 +280,6 @@ function bindDynamic() {
.bind('ajax:complete', stopLoading)
.bind('ajax:success',
function(evt, data, status, xhr) {
console.log("WTF", evt, data, status, xhr)
$('#task_item_' + data.id).remove();
return false;
});
Expand All @@ -270,7 +299,6 @@ function bindDynamic() {
});

$('.doSortTaskList').click(function(evt) {
console.log('....');
var el = $(evt.target);
var url = el.attr('href');
var list = el.parents('.taskList:first');
Expand Down Expand Up @@ -319,7 +347,16 @@ function bindDynamic() {
'time[open_task_id]': el.attr('task_id'),
'time[assigned_to_id]': LOGGED_USER_ID,
},
JustRebind, 'script');
function(data){
reloadTask(data.task, false);
var listed_time = $('#listed_time_' + data.id);
if (listed_time.length == 0)
$('#running_times_menu ul').append(data.content);
else
listed_time.replaceWith(data.content);
updateRunningTimes();
JustRebind();
}, 'json');

return false;
});
Expand All @@ -330,7 +367,12 @@ function bindDynamic() {
'time[open_task_id]': el.attr('task_id'),
'time[assigned_to_id]': LOGGED_USER_ID,
},
JustRebind, 'script');
function(data){
reloadTask(data.task, false);
$('#listed_time_' + data.id).remove();
updateRunningTimes();
JustRebind();
}, 'json');

return false;
});
Expand Down
22 changes: 16 additions & 6 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class TasksController < ApplicationController
helper 'project_items'

before_filter :process_session
before_filter :grab_list
before_filter :grab_list, :except => [:create, :new]
before_filter :grab_list_required, :only => [:index, :create, :new]
after_filter :user_track, :only => [:index, :show]

# GET /tasks
Expand All @@ -47,14 +48,14 @@ def index
# GET /tasks/1.xml
def show
begin
@task = @task_list.tasks.find(params[:id])
@task = (@task_list||@active_project).tasks.find(params[:id])
rescue
return error_status(true, :invalid_task)
end

respond_to do |format|
format.html { }
f.js { respond_with_task(@task) }
format.js { respond_with_task(@task) }
format.xml { render :xml => @task.to_xml(:root => 'task') }
end
end
Expand Down Expand Up @@ -186,15 +187,24 @@ def status
protected

def respond_with_task(task, partial='show')
task_class = @task.is_completed? ? 'completedTasks' : 'openTasks'
task_class = task.is_completed? ? 'completedTasks' : 'openTasks'
if task.errors
render :json => {:task_class => task_class, :id => @task.id, :content => render_to_string({:partial => partial, :collection => [@task]})}
render :json => {:task_class => task_class, :id => task.id, :content => render_to_string({:partial => partial, :collection => [task]})}
else
render :json => {:task_class => task_class, :id => @task.id, :errors => @task.errors.to_json}, :status => :unprocessable_entity
render :json => {:task_class => task_class, :id => task.id, :errors => task.errors}, :status => :unprocessable_entity
end
end

def grab_list_required
if params[:task_list_id].nil?
error_status(true, :invalid_task)
return false
end
grab_list
end

def grab_list
return if params[:task_list_id].nil?
begin
@task_list = @active_project.task_lists.find(params[:task_list_id])
authorize! :show, @task_list
Expand Down
18 changes: 12 additions & 6 deletions app/controllers/times_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ def create
error_status(false, :success_added_time)
redirect_back_or_default(@time)
}

format.js { respond_with_time(@time) }
format.xml { render :xml => @time.to_xml(:root => 'time'), :status => :created, :location => @time }
else
@open_task_lists = @active_project.task_lists.is_open
@open_task_lists = @open_task_lists.is_public unless @logged_user.member_of_owner?
@task_filter = Proc.new {|task| task.is_completed? }
format.html { render :action => "new" }

format.js { respond_with_time(@time) }
format.xml { render :xml => @time.errors, :status => :unprocessable_entity }
end
end
Expand All @@ -129,15 +129,13 @@ def update
error_status(false, :success_edited_time)
redirect_back_or_default(@time)
}

format.xml { head :ok }
else
@open_task_lists = @active_project.task_lists.is_open
@open_task_lists = @open_task_lists.is_public unless @logged_user.member_of_owner?
@open_task_lists << @time.task_list unless @time.task_list.nil? || @open_task_lists.include?(@time.task_list)
@task_filter = Proc.new {|task| task.is_completed? && task != @time.task}
format.html { render :action => "edit" }

format.xml { render :xml => @time.errors, :status => :unprocessable_entity }
end
end
Expand All @@ -158,7 +156,7 @@ def stop
error_status(false, :success_stopped_time)
redirect_back_or_default(@time)
}

format.js { respond_with_time(@time) }
format.xml { head :ok }
end

Expand All @@ -177,13 +175,21 @@ def destroy
error_status(false, :success_deleted_time)
redirect_back_or_default(times_url)
}

format.js { respond_with_time(@time) }
format.xml { head :ok }
end
end

private

def respond_with_time(time)
if time.errors
render :json => {:id => time.id, :time => time, :task => time.task, :content => render_to_string({:partial => 'listed', :collection => [time]})}
else
render :json => {:id => time.id, :time => time, :task => time.task, :errors => time.errors}, :status => :unprocessable_entity
end
end

def obtain_time
begin
@time = @active_project.time_records.find(params[:id])
Expand Down
1 change: 1 addition & 0 deletions config/initializers/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ActiveRecord::Base.include_root_in_json = false

0 comments on commit b7b255c

Please sign in to comment.