<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>app/views/projects/actionize.js.rjs</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -51,6 +51,21 @@ class User &lt; ActiveRecord::Base
                 self.update_positions(projects.map{ |p| p.id })
                 return projects
               end
+              def actionize(user_id, scope_conditions = {})
+                @state = scope_conditions[:state]
+                query_state = ''
+                query_state = 'AND project.state = &quot;' + @state +'&quot; 'if @state
+                projects = Project.find_by_sql([
+                    &quot;SELECT project.id, count(todo.id) as p_count &quot; +
+                      &quot;FROM projects as project &quot; +
+                      &quot;LEFT OUTER JOIN todos as todo ON todo.project_id = project.id &quot;+
+                      &quot;WHERE project.user_id = ? AND NOT todo.state='completed' &quot; +
+                      query_state +
+                      &quot; GROUP BY project.id ORDER by p_count DESC&quot;,user_id])
+                self.update_positions(projects.map{ |p| p.id })
+                projects = find(:all, :conditions =&gt; scope_conditions)
+                return projects
+              end
             end
   has_many :active_projects,
            :class_name =&gt; 'Project',</diff>
      <filename>app/models/user.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,7 +1,8 @@
 &lt;div class=&quot;project-state-group&quot; id=&quot;list-&lt;%= state %&gt;-projects-container&quot; &lt;%= &quot; style=\&quot;display:none\&quot;&quot; if project_state_group.empty? %&gt;&gt;
   &lt;h2&gt;&lt;span id=&quot;&lt;%= state %&gt;-projects-count&quot; class=&quot;badge&quot;&gt;&lt;%= project_state_group.length %&gt;&lt;/span&gt;&lt;%= state.titlecase %&gt; Projects&lt;/h2&gt;
+  &lt;div class=&quot;menu_sort&quot;&gt;&lt;span class=&quot;sort_separator&quot;&gt;Sort&amp;nbsp;&lt;/span&gt;
   &lt;div class=&quot;alpha_sort&quot;&gt;
-    &lt;%= link_to(&quot;Sort Alphabetically&quot;, alphabetize_projects_path(:state =&gt; state),
+    &lt;%= link_to(&quot;Alphabetically&quot;, alphabetize_projects_path(:state =&gt; state),
                  :class =&gt; &quot;alphabetize_link&quot;, :title =&gt; &quot;Sort these projects alphabetically&quot;) %&gt;
     &lt;%  apply_behavior '.alphabetize_link:click', :prevent_default =&gt; true do |page, element|
           page.confirming 'Are you sure that you want to sort these projects alphabetically? This will replace the existing sort order.' do
@@ -10,10 +11,21 @@
             page &lt;&lt; remote_to_href(:complete =&gt; &quot;alphaSort.stopWaiting()&quot;)
           end
         end
+     %&gt;&lt;/div&gt;&lt;span class=&quot;sort_separator&quot;&gt;&amp;nbsp;|&amp;nbsp;&lt;/span&gt;&lt;div class=&quot;tasks_sort&quot;&gt;
+    &lt;%= link_to(&quot;By number of tasks&quot;, actionize_projects_path(:state =&gt; state),
+                 :class =&gt; &quot;actionize_link&quot;, :title =&gt; &quot;Sort these projects by number of tasks&quot;) %&gt;
+    &lt;%  apply_behavior '.actionize_link:click', :prevent_default =&gt; true do |page, element|
+          page.confirming 'Are you sure that you want to sort these projects by the number of tasks? This will replace the existing sort order.' do
+            page &lt;&lt; &quot;tasksSort = this.up('.tasks_sort');
+                     tasksSort.startWaiting();&quot;
+            page &lt;&lt; remote_to_href(:complete =&gt; &quot;tasksSort.stopWaiting()&quot;)
+          end
+        end
      %&gt;&lt;/div&gt;
+  &lt;/div&gt;
   
   &lt;div id=&quot;list-&lt;%= state %&gt;-projects&quot; class=&quot;project-list&quot;&gt;
     &lt;%= render :partial =&gt; 'project_listing', :collection =&gt; project_state_group %&gt;
   &lt;/div&gt;
   &lt;%= sortable_element &quot;list-#{state}-projects&quot;, get_listing_sortable_options(&quot;list-#{state}-projects&quot;) %&gt;
-&lt;/div&gt;
\ No newline at end of file
+&lt;/div&gt;</diff>
      <filename>app/views/projects/_project_state_group.rhtml</filename>
    </modified>
    <modified>
      <diff>@@ -28,6 +28,10 @@ ActionController::Routing::Routes.draw do |map|
     projects.resources :todos, :name_prefix =&gt; &quot;project_&quot;
   end
 
+  map.resources :projects, :collection =&gt; {:order =&gt; :post, :actionize =&gt; :post} do |projects|
+    projects.resources :todos, :name_prefix =&gt; &quot;project_&quot;
+  end
+
   map.resources :todos,
                 :member =&gt; {:toggle_check =&gt; :put, :toggle_star =&gt; :put},
                 :collection =&gt; {:check_deferred =&gt; :post, :filter_to_context =&gt; :post, :filter_to_project =&gt; :post}</diff>
      <filename>config/routes.rb</filename>
    </modified>
    <modified>
      <diff>@@ -692,11 +692,14 @@ div#list-active-projects, div#list-hidden-projects, div#list-completed-projects,
     margin:20px 0px 8px 13px
 }
 
-div.alpha_sort {
+div.menu_sort {
     margin-top:-20px;
     float:right;
 }
 
+div.alpha_sort, div.tasks_sort,span.sort_separator {
+    float:left;
+}
 
 .container td {
     border: none;</diff>
      <filename>public/stylesheets/standard.css</filename>
    </modified>
    <modified>
      <diff>@@ -218,6 +218,15 @@ class ProjectsControllerTest &lt; TodoContainerControllerTestBase
     get :index, { :format =&gt; &quot;txt&quot;, :token =&gt; users(:admin_user).token }
     assert_response :ok
   end
+
+  def test_actionize_sorts_active_projects_by_number_of_tasks
+    login_as :admin_user
+    u = users(:admin_user)
+    post :actionize, :state =&gt; &quot;active&quot;, :format =&gt; 'js'
+    assert_equal 1, projects(:gardenclean).position 
+    assert_equal 2, projects(:timemachine).position 
+    assert_equal 3, projects(:moremoney).position
+  end
   
   def test_alphabetize_sorts_active_projects_alphabetically
     login_as :admin_user</diff>
      <filename>test/functional/projects_controller_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>a3f23c60e49c2c4bb50938209fe88c5b78918f1f</id>
    </parent>
    <parent>
      <id>6d212cc21acd5c7284d7be8ed310b8e3757af661</id>
    </parent>
  </parents>
  <author>
    <name>Reinier Balt</name>
    <email>lrbalt@gmail.com</email>
  </author>
  <url>http://github.com/bsag/tracks/commit/76d72b54bff23b0f178ec7e4ec3a60ec630133d3</url>
  <id>76d72b54bff23b0f178ec7e4ec3a60ec630133d3</id>
  <committed-date>2008-10-28T12:59:07-07:00</committed-date>
  <authored-date>2008-10-28T12:59:07-07:00</authored-date>
  <message>Merge branch 'sort_projects_by_todos' of git://github.com/waltercruz/tracks</message>
  <tree>b60a2298a174d0778a943dae66386356d2a0fedc</tree>
  <committer>
    <name>Reinier Balt</name>
    <email>lrbalt@gmail.com</email>
  </committer>
</commit>
