1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #31956 from fatkodima/has_attached-presence-validation

has_(one/many)_attached presence validation
This commit is contained in:
Eileen M. Uchitelle 2018-04-27 14:35:21 -04:00 committed by GitHub
commit ad0220a71a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View file

@ -13,6 +13,10 @@ module ActiveStorage
record.public_send("#{name}_attachment")
end
def blank?
attachment.blank?
end
# Associates a given attachment with the current record, saving it to the database.
#
# person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object

View file

@ -0,0 +1,30 @@
# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::PresenceValidationTest < ActiveSupport::TestCase
class Admin < User; end
teardown do
Admin.clear_validators!
end
test "validates_presence_of has_one_attached" do
Admin.validates_presence_of :avatar
a = Admin.new
assert_predicate a, :invalid?
a.avatar.attach create_blob(filename: "funky.jpg")
assert_predicate a, :valid?
end
test "validates_presence_of has_many_attached" do
Admin.validates_presence_of :highlights
a = Admin.new
assert_predicate a, :invalid?
a.highlights.attach create_blob(filename: "funky.jpg")
assert_predicate a, :valid?
end
end