Skip to content

Commit

Permalink
Save avatar or header correctly even if other one fails (#18465)
Browse files Browse the repository at this point in the history
* Save avatar or header correctly if other one fails

* Fix test
  • Loading branch information
tribela committed Dec 15, 2022
1 parent 8f8c0fe commit d412147
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,15 @@ def build_fields

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
Expand Down
2 changes: 1 addition & 1 deletion spec/models/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
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
Expand Down

0 comments on commit d412147

Please sign in to comment.