Module: Carrierwave::Base64::Adapter

Defined in:
lib/carrierwave/base64/adapter.rb

Instance Method Summary collapse

Instance Method Details

#mount_base64_uploader(attribute, uploader_class, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/carrierwave/base64/adapter.rb', line 7

def mount_base64_uploader(attribute, uploader_class, options = {})
  mount_uploader attribute, uploader_class, options
  options[:file_name] ||= proc { attribute }

  if options[:file_name].is_a?(String)
    warn(
      '[Deprecation warning] Setting `file_name` option to a string is '\
      'deprecated and will be removed in 3.0.0. If you want to keep the '\
      'existing behaviour, wrap the string in a Proc'
    )
  end

  define_method "#{attribute}=" do |data|
    return if data == send(attribute).to_s

    if respond_to?("#{attribute}_will_change!") && data.present?
      send "#{attribute}_will_change!"
    end

    return super(data) unless data.is_a?(String) &&
                              data.strip.start_with?('data')

    filename = if options[:file_name].respond_to?(:call)
                 options[:file_name].call(self)
               else
                 options[:file_name]
               end.to_s

    super Carrierwave::Base64::Base64StringIO.new(data.strip, filename)
  end
  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
  # rubocop:enable Metrics/CyclomaticComplexity
  # rubocop:enable Metrics/PerceivedComplexity
end