allow re-saving an existing collection without complaint
authorTom Clegg <tom@clinicalfuture.com>
Tue, 12 Feb 2013 21:10:20 +0000 (14:10 -0700)
committerTom Clegg <tom@clinicalfuture.com>
Tue, 12 Feb 2013 21:10:20 +0000 (14:10 -0700)
app/controllers/application_controller.rb
app/controllers/orvos/v1/collections_controller.rb

index 39703e7439e1ca5b1cd9979a85b2f9ffdab4c75f..4fda8f941931d32cf9d8797c66760d25571eadee 100644 (file)
@@ -4,12 +4,13 @@ class ApplicationController < ActionController::Base
   protect_from_forgery
   before_filter :uncamelcase_params_hash_keys
   around_filter :thread_with_auth_info, :except => [:render_error, :render_not_found]
-  before_filter :find_object_by_uuid, :except => :index
+  before_filter :find_object_by_uuid, :except => [:index, :create]
 
   before_filter :remote_ip
   before_filter :login_required, :except => :render_not_found
 
   before_filter :catch_redirect_hint
+  attr_accessor :resource_attrs
 
   def catch_redirect_hint
     if !current_user
@@ -108,29 +109,33 @@ class ApplicationController < ActionController::Base
   end
 
   def create
-    @attrs = params[resource_name]
-    if @attrs.nil?
-      raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
-    end
-    if @attrs.class == String
-      @attrs = uncamelcase_hash_keys(Oj.load @attrs)
-    end
-    @object = model_class.new @attrs
+    @object = model_class.new resource_attrs
     @object.save
     show
   end
 
   def update
+    @object.update_attributes resource_attrs
+    show
+  end
+
+  protected
+
+  def resource_attrs
+    return @attrs if @attrs
     @attrs = params[resource_name]
     if @attrs.is_a? String
       @attrs = uncamelcase_hash_keys(Oj.load @attrs)
     end
-    @object.update_attributes @attrs
-    show
+    if @attrs.nil?
+      raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
+    end
+    %w(created_at modified_by_client modified_by_user modified_at).each do |x|
+      @attrs.delete x
+    end
+    @attrs
   end
 
-  protected
-
   # Authentication
   def login_required
     if !current_user
index 33a59a06ca0c962b7bd4f8603cde8330cb801d5f..a1afd1e7155f748e6282771d928d92321ccb1e56 100644 (file)
@@ -1,2 +1,21 @@
 class Orvos::V1::CollectionsController < ApplicationController
+  def create
+    # It's not an error for a client to re-register a manifest that we
+    # already know about.
+    @object = model_class.new resource_attrs
+    begin
+      @object.save!
+    rescue ActiveRecord::RecordNotUnique
+      logger.debug resource_attrs.inspect
+      if resource_attrs['manifest_text'] and resource_attrs['uuid']
+        @existing_object = model_class.
+          where('uuid=? and manifest_text=?',
+                resource_attrs['uuid'],
+                resource_attrs['manifest_text']).
+          first
+        @object = @existing_object || @object
+      end
+    end
+    show
+  end
 end