aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@users.noreply.github.com>2018-04-27 14:35:21 -0400
committerGitHub <noreply@github.com>2018-04-27 14:35:21 -0400
commitad0220a71a2d42191a3acf6118321b504f1ad70c (patch)
treee93585a7a5c5569ac1a3470986a592bd07cbc1a7 /activestorage
parent1230253153edcccc1316f1ab9bc16684ba02b0ae (diff)
parent42259ce904eb2538761b32a793cbe390fb8272b7 (diff)
downloadrails-ad0220a71a2d42191a3acf6118321b504f1ad70c.tar.gz
rails-ad0220a71a2d42191a3acf6118321b504f1ad70c.tar.bz2
rails-ad0220a71a2d42191a3acf6118321b504f1ad70c.zip
Merge pull request #31956 from fatkodima/has_attached-presence-validation
has_(one/many)_attached presence validation
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/lib/active_storage/attached/one.rb4
-rw-r--r--activestorage/test/models/presence_validation_test.rb30
2 files changed, 34 insertions, 0 deletions
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index a582ed0137..f992cb5f84 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -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
diff --git a/activestorage/test/models/presence_validation_test.rb b/activestorage/test/models/presence_validation_test.rb
new file mode 100644
index 0000000000..aa804506dd
--- /dev/null
+++ b/activestorage/test/models/presence_validation_test.rb
@@ -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