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
- #protocol_option ⇒ 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
90 91 92 93 94 |
# File 'app/uploaders/decidim/application_uploader.rb', line 90 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
86 87 88 |
# File 'app/uploaders/decidim/application_uploader.rb', line 86 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
57 58 59 60 61 62 |
# File 'app/uploaders/decidim/application_uploader.rb', line 57 def path( = {}) representable = model.send(mounted_as) return super() unless representable.is_a? ActiveStorage::Attached variant_path(.delete(:variant), **) end |
#protocol_option ⇒ Object
77 78 79 80 81 |
# File 'app/uploaders/decidim/application_uploader.rb', line 77 def protocol_option return {} unless Rails.application.config.force_ssl { protocol: "https" } end |
#remote_url=(url) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'app/uploaders/decidim/application_uploader.rb', line 68 def remote_url=(url) uri = URI.parse(url) filename = File.basename(uri.path) file = URI.open(url) 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
64 65 66 |
# File 'app/uploaders/decidim/application_uploader.rb', line 64 def variant_path(key, = {}) variant_url(key, **.merge(only_path: true)) end |
#variant_url(key, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'app/uploaders/decidim/application_uploader.rb', line 46 def variant_url(key, = {}) return unless attached? representable = variant(key) if representable.is_a? ActiveStorage::Attached Rails.application.routes.url_helpers.rails_blob_url(representable.blob, **protocol_option.merge()) else Rails.application.routes.url_helpers.rails_representation_url(representable, **protocol_option.merge()) end end |