aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorMatt Tanous <mtanous22@gmail.com>2018-07-25 17:04:34 -0400
committerMatt Tanous <mtanous22@gmail.com>2019-05-23 12:11:39 -0400
commitf002be148e1377709ed28b8e80c5db76ee2fa410 (patch)
treeeb368b86d475a4cd3449de50bc8af3d40e4b48fa /activestorage
parenteba859d867454cd441f29ff15d8dbf442e51919c (diff)
downloadrails-f002be148e1377709ed28b8e80c5db76ee2fa410.tar.gz
rails-f002be148e1377709ed28b8e80c5db76ee2fa410.tar.bz2
rails-f002be148e1377709ed28b8e80c5db76ee2fa410.zip
Add :allow_nil option to delegate_missing_to; use in ActiveStorage
attachment
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/CHANGELOG.md15
-rw-r--r--activestorage/lib/active_storage/attached/one.rb2
2 files changed, 16 insertions, 1 deletions
diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md
index 1a03ead870..351d8687a4 100644
--- a/activestorage/CHANGELOG.md
+++ b/activestorage/CHANGELOG.md
@@ -1,3 +1,18 @@
+* Method calls on singular attachments return `nil` when no file is attached.
+
+ Previously, assuming the following User model, `user.avatar.filename` would
+ raise a `Module::DelegationError` if no avatar was attached:
+
+ ```ruby
+ class User < ApplicationRecord
+ has_one_attached :avatar
+ end
+ ```
+
+ They now return `nil`.
+
+ *Matthew Tanous*
+
* The mirror service supports direct uploads.
New files are directly uploaded to the primary service. When a
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index c039226fcd..003be1cb43 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -3,7 +3,7 @@
module ActiveStorage
# Representation of a single attachment to a model.
class Attached::One < Attached
- delegate_missing_to :attachment
+ delegate_missing_to :attachment, allow_nil: true
# Returns the associated attachment record.
#