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
AttachmentUploader, Cw::AttachmentUploader, Cw::OpenDataUploader, ImageUploader
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
80 81 82 83 84 |
# File 'app/uploaders/decidim/application_uploader.rb', line 80 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
76 77 78 |
# File 'app/uploaders/decidim/application_uploader.rb', line 76 def variants @variants ||= {} end |
Instance Method Details
#attached? ⇒ Boolean
35 36 37 |
# File 'app/uploaders/decidim/application_uploader.rb', line 35 def attached? model.send(mounted_as).attached? end |
#path(options = {}) ⇒ Object
53 54 55 56 57 58 |
# File 'app/uploaders/decidim/application_uploader.rb', line 53 def path( = {}) representable = model.send(mounted_as) return super() unless representable.is_a? ActiveStorage::Attached variant_path(.delete(:variant), **) end |
#remote_url=(url) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'app/uploaders/decidim/application_uploader.rb', line 64 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: 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
39 40 41 42 43 44 |
# File 'app/uploaders/decidim/application_uploader.rb', line 39 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 |
# 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 end |
#variant_path(key, options = {}) ⇒ Object
60 61 62 |
# File 'app/uploaders/decidim/application_uploader.rb', line 60 def variant_path(key, = {}) variant_url(key, **.merge(only_path: true)) end |
#variant_url(key, options = {}) ⇒ Object
46 47 48 49 50 51 |
# File 'app/uploaders/decidim/application_uploader.rb', line 46 def variant_url(key, = {}) return unless attached? representable = variant(key) AssetRouter.new(representable).url(**) end |