2872: Add "move to project" button, fix "copy to project" behavior
authorTom Clegg <tom@curoverse.com>
Thu, 12 Jun 2014 15:36:21 +0000 (11:36 -0400)
committerTom Clegg <tom@curoverse.com>
Thu, 12 Jun 2014 15:36:21 +0000 (11:36 -0400)
(really copy, instead of adding a link).

apps/workbench/app/assets/javascripts/selection.js
apps/workbench/app/controllers/actions_controller.rb

index 84d65cda2d84419c20d5bf6b00a96773aaae9680..0fa2e9a0729bf90e9a2759d0c992fd5bf00fc5a7 100644 (file)
@@ -57,8 +57,10 @@ jQuery(function($){
         $("#persistent-selection-count").text(lst.length);
         if (lst.length > 0) {
             html = '<li><a href="#" class="btn btn-xs btn-info" id="clear_selections_button"><i class="fa fa-fw fa-ban"></i> Clear selections</a></li>';
-            if (this_object_uuid.match('-j7d0g-'))
-                html += '<li><button class="btn btn-xs btn-info" type="submit" name="copy_selections_into_project" id="copy_selections_into_project"><i class="fa fa-fw fa-folder-open"></i> Copy selections into this project</button></li>';
+            if (this_object_uuid.match('-j7d0g-')) {
+                html += '<li><button class="btn btn-xs btn-info" type="submit" name="copy_selections_into_project" id="copy_selections_into_project"><i class="fa fa-fw fa-copy"></i> Copy selections into this project</button></li>';
+                html += '<li><button class="btn btn-xs btn-info" type="submit" name="move_selections_into_project" id="move_selections_into_project"><i class="fa fa-fw fa-truck"></i> Move selections into this project</button></li>';
+           }
             html += '<li><button class="btn btn-xs btn-info" type="submit" name="combine_selected_files_into_collection" '
                 + ' id="combine_selected_files_into_collection">'
                 + '<i class="fa fa-fw fa-archive"></i> Combine selected collections and files into a new collection</button></li>'
@@ -200,8 +202,8 @@ function enable_disable_selection_actions() {
     var $checked = $('.persistent-selection:checkbox:checked');
     $('[data-selection-action]').
         closest('div.btn-group-sm').
-        find('*').
-        prop('disabled', ($checked.length == 0));
+        find('ul li').
+        toggleClass('disabled', ($checked.length == 0));
     $('[data-selection-action=compare]').
         closest('li').
         toggleClass('disabled',
index 06775fc924521bdfa6a7796098bbed1cabaaa8c0..6fbdf29f46ae4e624ed55752bce9a6a7a8d0ac6e 100644 (file)
@@ -20,43 +20,51 @@ class ActionsController < ApplicationController
   end
 
   expose_action :copy_selections_into_project do
-    link_selections = Link.filter([['uuid','in',params["selection"]]])
-    link_uuids = link_selections.collect(&:uuid)
-
-    # Given a link uuid, we'll add the link's head_uuid. Given another
-    # type, we'll add the object itself.
-    uuids_to_add = params["selection"] - link_uuids
-    uuids_to_add += link_selections.collect(&:head_uuid)
-
-    # Skip anything that's already here.
-    already_named = Link.
-      filter([['tail_uuid','=',@object.uuid],
-              ['head_uuid','in',uuids_to_add],
-              ['link_class','=','name']]).
-      collect(&:head_uuid)
-    uuids_to_add -= already_named
-
-    # Given a name link, we'll try to add the linked object using the
-    # same name.
-    name_for = {}
-    link_selections.
-      select { |x| x.link_class == 'name' }.
-      each do |link|
-      name_for[link.head_uuid] = link.name
-    end
+    move_or_copy :copy
+  end
+
+  expose_action :move_selections_into_project do
+    move_or_copy :move
+  end
 
-    uuids_to_add.each do |s|
-      name = name_for[s] || s
-      begin
-        Link.create(tail_uuid: @object.uuid,
-                    head_uuid: s,
-                    link_class: 'name',
-                    name: name)
-      rescue
-        Link.create(tail_uuid: @object.uuid,
-                    head_uuid: s,
-                    link_class: 'name',
-                    name: name + " (#{Time.now.localtime})")
+  def move_or_copy action
+    uuids_to_add = params["selection"]
+    uuids_to_add.
+      collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
+      uniq.
+      each do |resource_class|
+      resource_class.filter([['uuid','in',uuids_to_add]]).each do |src|
+        if resource_class == Collection
+          dst = Link.new(owner_uuid: @object.uuid,
+                         tail_uuid: @object.uuid,
+                         head_uuid: src.uuid,
+                         link_class: 'name',
+                         name: @object.uuid)
+        else
+          case action
+          when :copy
+            dst = src.dup
+            if dst.respond_to? :'name='
+              if dst.name
+                dst.name = "Copy of #{dst.name}"
+              else
+                dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
+              end
+            end
+          when :move
+            dst = src
+          else
+            raise ArgumentError.new "Unsupported action #{action}"
+          end
+          dst.owner_uuid = @object.uuid
+          dst.tail_uuid = @object.uuid if dst.class == Link
+        end
+        begin
+          dst.save!
+        rescue
+          dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
+          dst.save!
+        end
       end
     end
     redirect_to @object