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.

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



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

.variantsObject

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

Returns:

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

  variant_path(options.delete(:variant), **options)
end

#protocol_optionObject



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_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



39
40
41
42
43
44
# File 'app/uploaders/decidim/application_uploader.rb', line 39

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
# 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, options = {})
  variant_url(key, **options.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, options = {})
  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(options))
  else
    Rails.application.routes.url_helpers.rails_representation_url(representable, **protocol_option.merge(options))
  end
end