aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/app/models/active_storage/filename.rb
diff options
context:
space:
mode:
authorJason Lee <huacnlee@gmail.com>2018-02-08 10:15:55 +0800
committerJason Lee <huacnlee@gmail.com>2018-02-08 10:15:55 +0800
commit0625a2ba80476bf0139f2ecb9019dc2c82e4a7de (patch)
treec55afd7aa065c6ab062da03550932feff130793f /activestorage/app/models/active_storage/filename.rb
parent01c54e29bd288bf250b6e277046594f06b211a6f (diff)
downloadrails-0625a2ba80476bf0139f2ecb9019dc2c82e4a7de.tar.gz
rails-0625a2ba80476bf0139f2ecb9019dc2c82e4a7de.tar.bz2
rails-0625a2ba80476bf0139f2ecb9019dc2c82e4a7de.zip
Fix `blob.service_url` for supports string or nil `:filename` option.
- Make sure `blob.service_url` present a `ActiveStorage::Filename` type to `serivce.url`. - Add `ActiveStorage::Filename.wrap` method. before: ```rb blob.service_url(filename: ActiveStorage::Filename.new("new.txt")) blob.service_url(filename: "new.txt") => NoMethodError: undefined method `parameters' for "new.txt":String params = {} blob.service_url(filename: params[:filename]) => NoMethodError: undefined method `parameters' for nil:NilClass ``` after: ```rb blob.service_url(filename: "new.txt") blob.service_url(filename: nil) ```
Diffstat (limited to 'activestorage/app/models/active_storage/filename.rb')
-rw-r--r--activestorage/app/models/active_storage/filename.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/activestorage/app/models/active_storage/filename.rb b/activestorage/app/models/active_storage/filename.rb
index b9413dec95..2b8880716e 100644
--- a/activestorage/app/models/active_storage/filename.rb
+++ b/activestorage/app/models/active_storage/filename.rb
@@ -5,6 +5,14 @@
class ActiveStorage::Filename
include Comparable
+ class << self
+ # Returns a Filename instance based on the given filename. If the filename is a Filename, it is
+ # returned unmodified. If it is a String, it is passed to ActiveStorage::Filename.new.
+ def wrap(filename)
+ filename.kind_of?(self) ? filename : new(filename)
+ end
+ end
+
def initialize(filename)
@filename = filename
end