Module: Carrierwave::Base64::MountingHelper

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

Overview

Module with helper functions for mounting uploaders

Class Method Summary collapse

Class Method Details

.check_for_deprecations(options) ⇒ void

This method returns an undefined value.

Checks for deprecations and prints a warning if found any.

Parameters:

  • options (Hash{Symbol => Object})

    the uploader options



12
13
14
15
16
17
18
19
20
# File 'lib/carrierwave/base64/mounting_helper.rb', line 12

def check_for_deprecations(options)
  return unless 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_writer(klass, attr, options) ⇒ Symbol

Defines an attribute writer method on the class with mounted uploader.

Parameters:

  • klass (Class)

    the class with mounted uploader

  • attr (Symbol)

    the attribute for which the writer will be defined

  • options (Hash{Symbol => Object})

    a set of options

Returns:

  • (Symbol)

    the defined writer method name



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/carrierwave/base64/mounting_helper.rb', line 43

def define_writer(klass, attr, options)
  klass.send(:define_method, "#{attr}=") do |data|
    # rubocop:disable Lint/NonLocalExitFromIterator
    return if data.to_s.empty? || data == send(attr).to_s

    # rubocop:enable Lint/NonLocalExitFromIterator

    send("#{attr}_will_change!") if respond_to?("#{attr}_will_change!")

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

    super Carrierwave::Base64::Base64StringIO.new(
      data.strip,
      Carrierwave::Base64::MountingHelper.file_name(self, options)
    )
  end
end

.file_name(model_instance, options) ⇒ String

Returns a file name for the uploaded file.

Parameters:

  • model_instance (Object)

    the model instance object

  • options (Hash{Symbol => Object})

    the uploader options

Returns:

  • (String)

    File name without extension



28
29
30
31
32
33
34
# File 'lib/carrierwave/base64/mounting_helper.rb', line 28

def file_name(model_instance, options)
  if options[:file_name].respond_to?(:call)
    options[:file_name].call(model_instance)
  else
    options[:file_name]
  end.to_s
end