Class: Decidim::ApplicationUploader

Inherits:
Object
  • Object
show all
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, ImageUploader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_allowlistObject (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_denylistObject (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

#modelObject (readonly)

Returns the value of attribute model.



13
14
15
# File 'app/uploaders/decidim/application_uploader.rb', line 13

def model
  @model
end

#mounted_asObject (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_dimensionsObject (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_variantsObject



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

.variantsObject

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

Returns:

  • (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(options = {})
  representable = model.send(mounted_as)
  return super() unless representable.is_a? ActiveStorage::Attached

  variant_path(options.delete(:variant), **options)
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_dirObject

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(options = {})
  representable = model.send(mounted_as)
  return super unless representable.is_a? ActiveStorage::Attached

  variant_url(options.delete(:variant), **options)
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, options = {})
  variant_url(key, **options.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, options = {})
  return unless attached?

  representable = variant(key)
  AssetRouter::Storage.new(representable).url(**options)
end