Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / models / arvados_base.rb
index 9925d46128410ca0730572cbdec6622d1adb3069..c5e1a4ed2240075691fd6e03827c746be950f3aa 100644 (file)
@@ -104,11 +104,17 @@ class ArvadosBase
     attributes.symbolize_keys.each do |name, value|
       send("#{name}=", value)
     end
-end
+  end
+
+  # The ActiveModel::Dirty API was changed on Rails 5.2
+  # See: https://github.com/rails/rails/commit/c3675f50d2e59b7fc173d7b332860c4b1a24a726#diff-aaddd42c7feb0834b1b5c66af69814d3
+  def mutations_from_database
+    @mutations_from_database ||= ActiveModel::NullMutationTracker.instance
+  end
 
   def self.columns
+    @discovered_columns = [] if !defined?(@discovered_columns)
     return @discovered_columns if @discovered_columns.andand.any?
-    @discovered_columns = []
     @attribute_info ||= {}
     schema = arvados_api_client.discovery[:schemas][self.to_s.to_sym]
     return @discovered_columns if schema.nil?
@@ -123,7 +129,6 @@ end
         else
           # Hash, Array
           @discovered_columns << column(k, coldef[:type], coldef[:type].constantize.new)
-          # serialize k, coldef[:type].constantize
         end
         attr_reader k
         @attribute_info[k] = coldef
@@ -133,7 +138,14 @@ end
   end
 
   def new_record?
-    (uuid == nil) ? true : false
+    # dup method doesn't reset the uuid attr
+    @uuid.nil? || @new_record || false
+  end
+
+  def initialize_dup(other)
+    super
+    @new_record = true
+    @created_at = nil
   end
 
   def self.column(name, sql_type = nil, default = nil, null = true)
@@ -152,6 +164,8 @@ end
                 ArvadosBase::Type::Hash
               when 'Array'
                 ArvadosBase::Type::Array
+              when 'jsonb'
+                ArvadosBase::Type::Hash
               else
                 raise ArvadosBase::Error.new("Type unknown: #{sql_type}")
             end
@@ -172,6 +186,7 @@ end
     begin
       send(attr_name)
     rescue
+      Rails.logger.debug "BUG: access non-loaded attribute #{attr_name}"
       nil
     end
   end
@@ -316,7 +331,10 @@ end
       obdata.delete :uuid
       resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
     else
-      postdata.merge!(@create_params) if @create_params
+      if @create_params
+        @create_params = @create_params.to_unsafe_hash if @create_params.is_a? ActionController::Parameters
+        postdata.merge!(@create_params)
+      end
       resp = arvados_api_client.api(self.class, '', postdata)
     end
     return false if !resp[:etag] || !resp[:uuid]
@@ -347,7 +365,7 @@ end
   end
 
   def destroyed?
-    !(etag || uuid)
+    !(new_record? || etag || uuid)
   end
 
   def destroy
@@ -527,17 +545,17 @@ end
     if opts[:class].is_a? Class
       return opts[:class]
     end
-    if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
+    if uuid.match(/^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/)
       return Collection
     end
     resource_class = nil
-    uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
+    uuid.match(/^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/) do |re|
       resource_class ||= arvados_api_client.
         kind_class(self.uuid_infix_object_kind[re[1]])
     end
     if opts[:referring_object] and
         opts[:referring_attr] and
-        opts[:referring_attr].match /_uuid$/
+        opts[:referring_attr].match(/_uuid$/)
       resource_class ||= arvados_api_client.
         kind_class(opts[:referring_object].
                    attributes[opts[:referring_attr].