Class: Decidim::ApplicationUploader
- Inherits:
-
Object
- Object
- Decidim::ApplicationUploader
- Defined in:
- app/uploaders/decidim/application_uploader.rb
Overview
This class deals with uploading files to Decidim. It is intended to just hold the uploads configuration, so you should inherit from this class and then tweak any configuration you need.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content_type_allowlist ⇒ Object
readonly
Returns the value of attribute content_type_allowlist.
-
#content_type_denylist ⇒ Object
readonly
Returns the value of attribute content_type_denylist.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#mounted_as ⇒ Object
readonly
Returns the value of attribute mounted_as.
-
#validable_dimensions ⇒ Object
readonly
Returns the value of attribute validable_dimensions.
Class Method Summary collapse
- .set_variants ⇒ Object
-
.variants ⇒ Object
Each class inherits variants from parents and can define their own variants with the set_variants class method.
Instance Method Summary collapse
- #attached? ⇒ Boolean
-
#initialize(model, mounted_as) ⇒ ApplicationUploader
constructor
A new instance of ApplicationUploader.
- #path(options = {}) ⇒ Object
- #remote_url=(url) ⇒ Object
-
#store_dir ⇒ Object
Override the directory where uploaded files will be stored.
- #url(options = {}) ⇒ Object
- #variant(key) ⇒ Object
- #variant_path(key, options = {}) ⇒ Object
- #variant_url(key, options = {}) ⇒ Object
Constructor Details
#initialize(model, mounted_as) ⇒ ApplicationUploader
Returns a new instance of ApplicationUploader.
8 9 10 11 |
# File 'app/uploaders/decidim/application_uploader.rb', line 8 def initialize(model, mounted_as) @model = model @mounted_as = mounted_as end |
Instance Attribute Details
#content_type_allowlist ⇒ Object (readonly)
Returns the value of attribute content_type_allowlist.
13 14 15 |
# File 'app/uploaders/decidim/application_uploader.rb', line 13 def content_type_allowlist @content_type_allowlist end |
#content_type_denylist ⇒ Object (readonly)
Returns the value of attribute content_type_denylist.
13 14 15 |
# File 'app/uploaders/decidim/application_uploader.rb', line 13 def content_type_denylist @content_type_denylist end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
13 14 15 |
# File 'app/uploaders/decidim/application_uploader.rb', line 13 def model @model end |
#mounted_as ⇒ Object (readonly)
Returns the value of attribute mounted_as.
13 14 15 |
# File 'app/uploaders/decidim/application_uploader.rb', line 13 def mounted_as @mounted_as end |
#validable_dimensions ⇒ Object (readonly)
Returns the value of attribute validable_dimensions.
13 14 15 |
# File 'app/uploaders/decidim/application_uploader.rb', line 13 def validable_dimensions @validable_dimensions end |
Class Method Details
.set_variants ⇒ Object
82 83 84 85 86 |
# File 'app/uploaders/decidim/application_uploader.rb', line 82 def set_variants return unless block_given? variants.merge!(yield) end |
.variants ⇒ Object
Each class inherits variants from parents and can define their own variants with the set_variants class method
78 79 80 |
# File 'app/uploaders/decidim/application_uploader.rb', line 78 def variants @variants ||= {} end |
Instance Method Details
#attached? ⇒ Boolean
37 38 39 |
# File 'app/uploaders/decidim/application_uploader.rb', line 37 def attached? model.send(mounted_as).attached? end |
#path(options = {}) ⇒ Object
55 56 57 58 59 60 |
# File 'app/uploaders/decidim/application_uploader.rb', line 55 def path( = {}) representable = model.send(mounted_as) return super() unless representable.is_a? ActiveStorage::Attached variant_path(.delete(:variant), **) end |
#remote_url=(url) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/uploaders/decidim/application_uploader.rb', line 66 def remote_url=(url) uri = URI.parse(url) filename = File.basename(uri.path) file = URI.parse(url).open model.send(mounted_as).attach(io: file, filename:) rescue URI::InvalidURIError model.errors.add(mounted_as, :invalid) end |
#store_dir ⇒ Object
Override the directory where uploaded files will be stored. This is a sensible default for uploaders that are meant to be mounted:
19 20 21 22 23 24 25 |
# File 'app/uploaders/decidim/application_uploader.rb', line 19 def store_dir default_path = "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" return File.join(Decidim.base_uploads_path, default_path) if Decidim.base_uploads_path.present? default_path end |
#url(options = {}) ⇒ Object
41 42 43 44 45 46 |
# File 'app/uploaders/decidim/application_uploader.rb', line 41 def url( = {}) representable = model.send(mounted_as) return super unless representable.is_a? ActiveStorage::Attached variant_url(.delete(:variant), **) end |
#variant(key) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'app/uploaders/decidim/application_uploader.rb', line 27 def variant(key) if key && variants[key].present? model.send(mounted_as).variant(variants[key]) else model.send(mounted_as) end rescue ActiveStorage::InvariableError model.send(mounted_as) end |
#variant_path(key, options = {}) ⇒ Object
62 63 64 |
# File 'app/uploaders/decidim/application_uploader.rb', line 62 def variant_path(key, = {}) variant_url(key, **.merge(only_path: true)) end |
#variant_url(key, options = {}) ⇒ Object
48 49 50 51 52 53 |
# File 'app/uploaders/decidim/application_uploader.rb', line 48 def variant_url(key, = {}) return unless attached? representable = variant(key) AssetRouter::Storage.new(representable).url(**) end |