Improvements to image upload validation and creation (#10431)

* Check if image value is nil? before creating an image

Check if uploaded images aren't nil before creating SiteUpload models
for them.

* Validate presence of file in SiteUpload

* Fix file presence validation

* Fabricate SiteUpload#file

* Add link to Creative Commons license
This commit is contained in:
slice 2019-03-31 22:30:46 -07:00 committed by Eugen Rochko
parent abdf225353
commit 85973f4f37
5 changed files with 9 additions and 1 deletions

View File

@ -67,7 +67,7 @@ class Form::AdminSettings
KEYS.each do |key| KEYS.each do |key|
value = instance_variable_get("@#{key}") value = instance_variable_get("@#{key}")
if UPLOAD_KEYS.include?(key) if UPLOAD_KEYS.include?(key) && !value.nil?
upload = SiteUpload.where(var: key).first_or_initialize(var: key) upload = SiteUpload.where(var: key).first_or_initialize(var: key)
upload.update(file: value) upload.update(file: value)
else else

View File

@ -18,6 +18,7 @@ class SiteUpload < ApplicationRecord
has_attached_file :file has_attached_file :file
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/ validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/
validates :file, presence: true
validates :var, presence: true, uniqueness: true validates :var, presence: true, uniqueness: true
before_save :set_meta before_save :set_meta

View File

@ -0,0 +1,6 @@
This "Utah teapot" photograph is licensed under the Creative Commons
Attribution-Share Alike 3.0 Unported license:
https://creativecommons.org/licenses/by-sa/3.0/deed.en
Original source of work:
https://commons.wikimedia.org/wiki/File:Utah_teapot_simple_2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

View File

@ -1,2 +1,3 @@
Fabricator(:site_upload) do Fabricator(:site_upload) do
file { File.open(File.join(Rails.root, 'spec', 'fabricators', 'assets', 'utah_teapot.png')) }
end end