fix #1395 and speedup context.autocomplete a lot

This commit is contained in:
Reinier Balt 2013-02-18 16:39:43 +01:00
parent 4f6e6e3e4d
commit c61d0cbdb7
2 changed files with 63 additions and 59 deletions

View file

@ -8,14 +8,10 @@ class ContextsController < ApplicationController
prepend_before_filter :login_or_feed_token_required, :only => [:index]
def index
@all_contexts = current_user.contexts
@active_contexts = current_user.contexts.active
@hidden_contexts = current_user.contexts.hidden
@new_context = current_user.contexts.build
init_not_done_counts(['context'])
# save all contexts here as @new_context will add an empty one to current_user.contexts
@all_contexts = @active_contexts + @hidden_contexts
@count = @all_contexts.size
init_not_done_counts(['context']) unless request.format == :autocomplete
respond_to do |format|
format.html &render_contexts_html
@ -207,6 +203,9 @@ class ContextsController < ApplicationController
@no_hidden_contexts = @hidden_contexts.empty?
@active_count = @active_contexts.size
@hidden_count = @hidden_contexts.size
@count = @active_count + @hidden_count
@new_context = current_user.contexts.build
render
end
end
@ -235,9 +234,14 @@ class ContextsController < ApplicationController
def render_autocomplete
lambda do
# first get active contexts with todos then those without
filled_contexts = @active_contexts.reject { |ctx| ctx.todos.count == 0 } + @hidden_contexts.reject { |ctx| ctx.todos.count == 0 }
empty_contexts = @active_contexts.find_all { |ctx| ctx.todos.count == 0 } + @hidden_contexts.find_all { |ctx| ctx.todos.count == 0 }
# find contexts and the todos count. use a join to prevent all count(todos) of each context to be fetched
context_and_todo_count = current_user.contexts
.select('contexts.*, count(todos.id) as todos_count')
.joins('left outer join todos on context_id=contexts.id')
.group('context_id')
filled_contexts = context_and_todo_count.reject { |ctx| ctx.todos.size == 0 }
empty_contexts = context_and_todo_count.find_all { |ctx| ctx.todos.size == 0 }
render :text => for_autocomplete(filled_contexts + empty_contexts, params[:term])
end
end

View file

@ -1,4 +1,10 @@
<% if @saved -%>
<% unless @saved -%>
TracksPages.show_errors(html_for_error_messages());
function html_for_error_messages() {
return "<%= escape_javascript(get_list_of_error_messages_for(@todo)) %>";
}
<% else -%>
TracksPages.page_notify('notice', "<%=escape_javascript @status_message%>", 8);
TracksPages.hide_errors();
TracksPages.set_page_badge(<%= @down_count %>);
@ -11,59 +17,53 @@
<% end -%>
update_predecessors();
clear_form();
<% else -%>
TracksPages.show_errors(html_for_error_messages());
<% end -%>
function clear_form() {
$('#todo-form-new-action').clearForm();
$('#todo-form-new-action').clearDeps();
TracksForm.set_context_name('<%=escape_javascript @initial_context_name%>');
TracksForm.set_project_name('<%=escape_javascript @initial_project_name%>');
TracksForm.set_tag_list('<%=escape_javascript @initial_tags%>');
$('#todo-form-new-action input:text:first').focus();
$('#new_todo_starred_link .todo_star').removeClass('starred');
$('#new_todo_starred').val('false');
}
function clear_form() {
$('#todo-form-new-action').clearForm();
$('#todo-form-new-action').clearDeps();
TracksForm.set_context_name('<%=escape_javascript @initial_context_name%>');
TracksForm.set_project_name('<%=escape_javascript @initial_project_name%>');
TracksForm.set_tag_list('<%=escape_javascript @initial_tags%>');
$('#todo-form-new-action input:text:first').focus();
$('#new_todo_starred_link .todo_star').removeClass('starred');
$('#new_todo_starred').val('false');
}
function insert_new_context_with_new_todo() {
<%-
empty_id = '#no_todos_in_view'
empty_id = '#tickler-empty-nd' if source_view_is :tickler
-%>
$('<%=empty_id%>').slideUp(100);
$('#display_box').prepend(html_for_new_context());
}
function insert_new_context_with_new_todo() {
<%-
empty_id = '#no_todos_in_view'
empty_id = '#tickler-empty-nd' if source_view_is :tickler
-%>
$('<%=empty_id%>').slideUp(100);
$('#display_box').prepend(html_for_new_context());
}
function add_todo_to_existing_context() {
<% if source_view_is_one_of(:todo, :deferred, :tag) -%>
<% unless source_view_is_one_of(:todo, :tag) && (@todo.deferred?||@todo.hidden?) -%>
$('#c<%= @todo.context_id %>').fadeIn(500, function() {});
$('#no_todos_in_view').slideUp(100);
<%= "$('#tickler-empty-nd').slideUp(100);".html_safe if source_view_is(:deferred) && @todo.deferred? %>
function add_todo_to_existing_context() {
<% if source_view_is_one_of(:todo, :deferred, :tag) -%>
<% unless source_view_is_one_of(:todo, :tag) && (@todo.deferred?||@todo.hidden?) -%>
$('#c<%= @todo.context_id %>').fadeIn(500, function() {});
$('#no_todos_in_view').slideUp(100);
<%= "$('#tickler-empty-nd').slideUp(100);".html_safe if source_view_is(:deferred) && @todo.deferred? %>
<% end -%>
<% end -%>
<% end -%>
$('#<%=empty_container_msg_div_id%>').hide();
$('#<%=item_container_id(@todo)%>').append(html_for_new_todo());
$('#<%= dom_id(@todo)%>').effect('highlight', {}, 2000 );
}
$('#<%=empty_container_msg_div_id%>').hide();
$('#<%=item_container_id(@todo)%>').append(html_for_new_todo());
$('#<%= dom_id(@todo)%>').effect('highlight', {}, 2000 );
}
function update_predecessors() {
<% @todo.uncompleted_predecessors.each do |p| -%>
if ($('<%=item_container_id(p)%>')) {
$('#<%=dom_id(p)%>').html('<%= escape_javascript(render(:partial => p, :locals => { :parent_container_type => parent_container_type, :source_view => @source_view }))%>');
}
<% end -%>
}
function update_predecessors() {
<% @todo.uncompleted_predecessors.each do |p| -%>
if ($('<%=item_container_id(p)%>')) {
$('#<%=dom_id(p)%>').html('<%= escape_javascript(render(:partial => p, :locals => { :parent_container_type => parent_container_type, :source_view => @source_view }))%>');
}
<% end -%>
}
function html_for_error_messages() {
return "<%= escape_javascript(get_list_of_error_messages_for(@todo)) %>";
}
function html_for_new_context() {
return "<%= @saved && @new_context_created ? escape_javascript(render(:partial => @todo.context, :locals => { :collapsible => true })) : "" %>";
}
function html_for_new_context() {
return "<%= @saved && @new_context_created ? escape_javascript(render(:partial => @todo.context, :locals => { :collapsible => true })) : "" %>";
}
function html_for_new_todo() {
return "<%= @saved ? escape_javascript(render(:partial => @todo, :locals => { :parent_container_type => parent_container_type, :source_view => @source_view })) : "" %>";
}
function html_for_new_todo() {
return "<%= @saved ? escape_javascript(render(:partial => @todo, :locals => { :parent_container_type => parent_container_type, :source_view => @source_view })) : "" %>";
}
<% end -%>