2872: Move project_tree code into helper_methods, fix up tests.
authorTom Clegg <tom@curoverse.com>
Fri, 13 Jun 2014 00:53:05 +0000 (20:53 -0400)
committerTom Clegg <tom@curoverse.com>
Fri, 13 Jun 2014 00:53:05 +0000 (20:53 -0400)
apps/workbench/app/controllers/application_controller.rb
apps/workbench/app/controllers/projects_controller.rb
apps/workbench/app/views/layouts/application.html.erb
apps/workbench/app/views/projects/_choose.html.erb
apps/workbench/app/views/projects/index.html.erb
apps/workbench/test/integration/collections_test.rb
apps/workbench/test/integration/projects_test.rb

index 3a9ccf2044e0bdb0f486e5a90a90004944875733..ebd8661f55800674b1bfb653b84e39f342bf16e3 100644 (file)
@@ -550,7 +550,8 @@ class ApplicationController < ActionController::Base
 
   helper_method :all_projects
   def all_projects
-    @all_projects ||= Group.filter([['group_class','in',['project','folder']]])
+    @all_projects ||= Group.
+      filter([['group_class','in',['project','folder']]]).order('name')
   end
 
   helper_method :my_projects
@@ -594,6 +595,57 @@ class ApplicationController < ActionController::Base
     end
   end
 
+  helper_method :my_project_tree
+  def my_project_tree
+    build_project_trees
+    @my_project_tree
+  end
+
+  helper_method :shared_project_tree
+  def shared_project_tree
+    build_project_trees
+    @shared_project_tree
+  end
+
+  def build_project_trees
+    return if @my_project_tree and @shared_project_tree
+    parent_of = {current_user.uuid => 'me'}
+    all_projects.each do |ob|
+      parent_of[ob.uuid] = ob.owner_uuid
+    end
+    children_of = {false => [], 'me' => [current_user]}
+    all_projects.each do |ob|
+      if ob.owner_uuid != current_user.uuid and
+          not parent_of.has_key? ob.owner_uuid
+        parent_of[ob.uuid] = false
+      end
+      children_of[parent_of[ob.uuid]] ||= []
+      children_of[parent_of[ob.uuid]] << ob
+    end
+    buildtree = lambda do |children_of, root_uuid=false|
+      tree = {}
+      children_of[root_uuid].andand.each do |ob|
+        tree[ob] = buildtree.call(children_of, ob.uuid)
+      end
+      tree
+    end
+    sorted_paths = lambda do |tree, depth=0|
+      paths = []
+      tree.keys.sort_by { |ob|
+        ob.is_a?(String) ? ob : ob.friendly_link_name
+      }.each do |ob|
+        paths << {object: ob, depth: depth}
+        paths += sorted_paths.call tree[ob], depth+1
+      end
+      paths
+    end
+    @my_project_tree =
+      sorted_paths.call buildtree.call(children_of, 'me')
+    @shared_project_tree =
+      sorted_paths.call({'Shared with me' =>
+                          buildtree.call(children_of, false)})
+  end
+
   helper_method :get_object
   def get_object uuid
     if @get_object.nil? and @objects
index cd32c65e17624fa2c7fd4aed8bed3b026a701b6c..8f6b96e73ce2d698b2f2aa0f349ed4462e46388d 100644 (file)
@@ -64,45 +64,8 @@ class ProjectsController < ApplicationController
   end
 
   def find_objects_for_index
-    @objects = Group.
-      filter([['group_class','in',['project','folder']]]).
-      order('name')
+    @objects = all_projects
     super
-    parent_of = {current_user.uuid => 'me'}
-    @objects.each do |ob|
-      parent_of[ob.uuid] = ob.owner_uuid
-    end
-    children_of = {false => [], 'me' => [current_user]}
-    @objects.each do |ob|
-      if ob.owner_uuid != current_user.uuid and
-          not parent_of.has_key? ob.owner_uuid
-        parent_of[ob.uuid] = false
-      end
-      children_of[parent_of[ob.uuid]] ||= []
-      children_of[parent_of[ob.uuid]] << ob
-    end
-    buildtree = lambda do |children_of, root_uuid=false|
-      tree = {}
-      children_of[root_uuid].andand.each do |ob|
-        tree[ob] = buildtree.call(children_of, ob.uuid)
-      end
-      tree
-    end
-    sorted_paths = lambda do |tree, depth=0|
-      paths = []
-      tree.keys.sort_by { |ob|
-        ob.is_a?(String) ? ob : ob.friendly_link_name
-      }.each do |ob|
-        paths << {object: ob, depth: depth}
-        paths += sorted_paths.call tree[ob], depth+1
-      end
-      paths
-    end
-    @my_project_tree =
-      sorted_paths.call buildtree.call(children_of, 'me')
-    @shared_project_tree =
-      sorted_paths.call({'Shared with me' =>
-                          buildtree.call(children_of, false)})
   end
 
   def show
index ef9bdee3dda62584c4b88fca16786911bf57e31b..5ec45ef340c9404fd1f97b835af5a6200dc44356 100644 (file)
                 <% end %>
                 My projects
               </li>
-              <% @my_project_tree.each do |pnode| %>
+              <% my_project_tree.each do |pnode| %>
                 <% next if pnode[:object].class != Group %>
                 <li style="padding-left: <%= pnode[:depth]-1 %>em">
                   <%= link_to(pnode[:object].name, project_path(pnode[:object].uuid)) %>
               <li role="presentation" class="dropdown-header">
                 Projects shared with me
               </li>
-              <% @shared_project_tree.each do |pnode| %>
+              <% shared_project_tree.each do |pnode| %>
                 <% next if pnode[:object].class != Group %>
                 <li style="padding-left: <%= pnode[:depth]-1 %>em">
                   <%= link_to project_path(pnode[:object].uuid) do %>
index 3481fddfd2e54a05ccc2fd30555d2c7b272bcd02..93c0c49c84c7d9bbbc3309962331261a6a0de7f7 100644 (file)
@@ -9,7 +9,7 @@
 
       <div class="modal-body">
         <div class="selectable-container" style="height: 15em; overflow-y: scroll">
-          <% [@my_project_tree, @shared_project_tree].each do |tree| %>
+          <% [my_project_tree, shared_project_tree].each do |tree| %>
             <% tree.each do |projectnode| %>
               <% if projectnode[:object].is_a? String %>
                 <div class="row" style="padding-left: <%= 1 + projectnode[:depth] %>em;">
index 832b5d41d40b8904d2456d0ea8f4a1047a42ce9a..2c764335f6b7c1911180589fbe530b9b6c8fa4c0 100644 (file)
@@ -37,7 +37,7 @@
           </h3>
         </div>
         <div class="panel-body scroll-20em">
-          <%= render partial: 'index_projects', locals: {tree: @my_project_tree, show_root_node: false} %>
+          <%= render partial: 'index_projects', locals: {tree: my_project_tree, show_root_node: false} %>
         </div>
       </div>
     </div>
@@ -49,7 +49,7 @@
           </h3>
         </div>
         <div class="panel-body scroll-20em">
-          <%= render partial: 'index_projects', locals: {tree: @shared_project_tree, show_root_node: false} %>
+          <%= render partial: 'index_projects', locals: {tree: shared_project_tree, show_root_node: false} %>
         </div>
       </div>
     </div>
index 8978f73a5434d8da14904bab0b774c83a7768e21..8ac8fe4fb360c1a95683de09bbe8f621879fd301 100644 (file)
@@ -41,7 +41,8 @@ class CollectionsTest < ActionDispatch::IntegrationTest
   test "Collection page renders default name links" do
     uuid = api_fixture('collections')['foo_file']['uuid']
     coll_name = api_fixture('links')['foo_collection_name_in_aproject']['name']
-    visit page_with_token('active', "/collections/#{uuid}")
+    name_uuid = api_fixture('links')['foo_collection_name_in_aproject']['uuid']
+    visit page_with_token('active', "/collections/#{name_uuid}")
     assert(page.has_text?(coll_name), "Collection page did not include name")
     # Now check that the page is otherwise normal, and the collection name
     # isn't only showing up in an error message.
index 76a61b42a072339ee50dd1211a75c3cf7d5564ef..5758fc7410510d041adea87261f7c71712633d79 100644 (file)
@@ -70,7 +70,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     end
     wait_for_ajax
 
-    click_link 'Move to...'
+    click_link 'Move project...'
     find('.selectable', text: 'Project 1234').click
     find('.modal-footer a,button', text: 'Move').click
     wait_for_ajax