about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJeong Arm <kjwonmail@gmail.com>2022-12-16 01:11:14 +0900
committerGitHub <noreply@github.com>2022-12-15 17:11:14 +0100
commitd412147d02e84cb76b252706a5357fe5d434c3db (patch)
treec5e642abcd64f12ad0879332b40d2c5afaafef15
parent8f8c0fe88c19b38602d3f9de7742211b1b690af0 (diff)
Save avatar or header correctly even if other one fails (#18465)
* Save avatar or header correctly if other one fails

* Fix test
-rw-r--r--app/models/account.rb12
-rw-r--r--spec/models/account_spec.rb2
2 files changed, 10 insertions, 4 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index fc7359cfc..a7bda15d3 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -341,9 +341,15 @@ class Account < ApplicationRecord
 
   def save_with_optional_media!
     save!
-  rescue ActiveRecord::RecordInvalid
-    self.avatar = nil
-    self.header = nil
+  rescue ActiveRecord::RecordInvalid => e
+    errors = e.record.errors.errors
+    errors.each do |err|
+      if err.attribute == :avatar
+        self.avatar = nil
+      elsif err.attribute == :header
+        self.header = nil
+      end
+    end
 
     save!
   end
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index edae05f9d..c9d782cee 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -160,7 +160,7 @@ RSpec.describe Account, type: :model do
         expect(account.avatar_remote_url).to eq 'https://remote.test/invalid_avatar'
         expect(account.header_remote_url).to eq expectation.header_remote_url
         expect(account.avatar_file_name).to  eq nil
-        expect(account.header_file_name).to  eq nil
+        expect(account.header_file_name).to  eq expectation.header_file_name
       end
     end
   end