Class: CarrierWave::Mounter

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/mounter.rb

Overview

this is an internal class, used by CarrierWave::Mount so that we don’t pollute the model with a lot of methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, column, options = {}) ⇒ Mounter

Returns a new instance of Mounter.



10
11
12
13
14
# File 'lib/carrierwave/mounter.rb', line 10

def initialize(record, column, options={})
  @record = record
  @column = column
  @options = record.class.uploader_options[column]
end

Instance Attribute Details

#columnObject (readonly)

:nodoc:



6
7
8
# File 'lib/carrierwave/mounter.rb', line 6

def column
  @column
end

#download_errorObject (readonly)

:nodoc:



6
7
8
# File 'lib/carrierwave/mounter.rb', line 6

def download_error
  @download_error
end

#integrity_errorObject (readonly)

:nodoc:



6
7
8
# File 'lib/carrierwave/mounter.rb', line 6

def integrity_error
  @integrity_error
end

#processing_errorObject (readonly)

:nodoc:



6
7
8
# File 'lib/carrierwave/mounter.rb', line 6

def processing_error
  @processing_error
end

#recordObject (readonly)

:nodoc:



6
7
8
# File 'lib/carrierwave/mounter.rb', line 6

def record
  @record
end

#remote_request_headersObject

Returns the value of attribute remote_request_headers.



8
9
10
# File 'lib/carrierwave/mounter.rb', line 8

def remote_request_headers
  @remote_request_headers
end

#remote_urlsObject

:nodoc:



6
7
8
# File 'lib/carrierwave/mounter.rb', line 6

def remote_urls
  @remote_urls
end

#removeObject

Returns the value of attribute remove.



8
9
10
# File 'lib/carrierwave/mounter.rb', line 8

def remove
  @remove
end

#uploader_optionsObject

Returns the value of attribute uploader_options.



155
156
157
# File 'lib/carrierwave/mounter.rb', line 155

def uploader_options
  @uploader_options
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/carrierwave/mounter.rb', line 108

def blank?
  uploaders.none?(&:present?)
end

#blank_uploaderObject



20
21
22
# File 'lib/carrierwave/mounter.rb', line 20

def blank_uploader
  uploader_class.new(record, column)
end

#cache(new_files) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/carrierwave/mounter.rb', line 40

def cache(new_files)
  return if not new_files or new_files == ""
  @uploaders = new_files.map do |new_file|
    uploader = blank_uploader
    uploader.cache!(new_file)
    uploader
  end

  @integrity_error = nil
  @processing_error = nil
rescue CarrierWave::IntegrityError => e
  @integrity_error = e
  raise e unless option(:ignore_integrity_errors)
rescue CarrierWave::ProcessingError => e
  @processing_error = e
  raise e unless option(:ignore_processing_errors)
end

#cache_namesObject



58
59
60
# File 'lib/carrierwave/mounter.rb', line 58

def cache_names
  uploaders.map(&:cache_name).compact
end

#cache_names=(cache_names) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/carrierwave/mounter.rb', line 62

def cache_names=(cache_names)
  return if not cache_names or cache_names == "" or uploaders.any?(&:cached?)
  @uploaders = cache_names.map do |cache_name|
    uploader = blank_uploader
    uploader.retrieve_from_cache!(cache_name)
    uploader
  end
rescue CarrierWave::InvalidParameter
end

#identifiersObject



24
25
26
# File 'lib/carrierwave/mounter.rb', line 24

def identifiers
  uploaders.map(&:identifier)
end

#read_identifiersObject



28
29
30
# File 'lib/carrierwave/mounter.rb', line 28

def read_identifiers
  [record.read_uploader(serialization_column)].flatten.reject(&:blank?)
end

#remove!Object



116
117
118
119
# File 'lib/carrierwave/mounter.rb', line 116

def remove!
  uploaders.reject(&:blank?).each(&:remove!)
  @uploaders = []
end

#remove?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/carrierwave/mounter.rb', line 112

def remove?
  remove.present? && remove !~ /\A0|false$\z/
end

#remove_previous(before = nil, after = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/carrierwave/mounter.rb', line 125

def remove_previous(before=nil, after=nil)
  after ||= []
  return unless before

  # both 'before' and 'after' can be string when 'mount_on' option is set
  before = before.reject(&:blank?).map do |value|
    if value.is_a?(String)
      uploader = blank_uploader
      uploader.retrieve_from_store!(value)
      uploader
    else
      value
    end
  end
  after_paths = after.reject(&:blank?).map do |value|
    if value.is_a?(String)
      uploader = blank_uploader
      uploader.retrieve_from_store!(value)
      uploader
    else
      value
    end.path
  end
  before.each do |uploader|
    if uploader.remove_previously_stored_files_after_update and not after_paths.include?(uploader.path)
      uploader.remove!
    end
  end
end

#serialization_columnObject



121
122
123
# File 'lib/carrierwave/mounter.rb', line 121

def serialization_column
  option(:mount_on) || column
end

#store!Object



96
97
98
99
100
101
102
# File 'lib/carrierwave/mounter.rb', line 96

def store!
  if remove?
    remove!
  else
    uploaders.reject(&:blank?).each(&:store!)
  end
end

#uploader_classObject



16
17
18
# File 'lib/carrierwave/mounter.rb', line 16

def uploader_class
  record.class.uploaders[column]
end

#uploadersObject



32
33
34
35
36
37
38
# File 'lib/carrierwave/mounter.rb', line 32

def uploaders
  @uploaders ||= read_identifiers.map do |identifier|
    uploader = blank_uploader
    uploader.retrieve_from_store!(identifier) if identifier.present?
    uploader
  end
end

#urls(*args) ⇒ Object



104
105
106
# File 'lib/carrierwave/mounter.rb', line 104

def urls(*args)
  uploaders.map { |u| u.url(*args) }
end