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.

Direct Known Subclasses

Multiple, Single

Defined Under Namespace

Classes: Multiple, Single

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, column) ⇒ Mounter

Returns a new instance of Mounter.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/carrierwave/mounter.rb', line 38

def initialize(record, column)
  @record = record
  @column = column
  @options = record.class.uploader_options[column]
  @download_errors = []
  @processing_errors = []
  @integrity_errors = []

  @removed_uploaders = []
  @added_uploaders = []
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def column
  @column
end

#download_errorsObject (readonly)

Returns the value of attribute download_errors.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def download_errors
  @download_errors
end

#integrity_errorsObject (readonly)

Returns the value of attribute integrity_errors.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def integrity_errors
  @integrity_errors
end

#processing_errorsObject (readonly)

Returns the value of attribute processing_errors.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def processing_errors
  @processing_errors
end

#recordObject (readonly)

Returns the value of attribute record.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def record
  @record
end

#remote_request_headersObject

Returns the value of attribute remote_request_headers.



36
37
38
# File 'lib/carrierwave/mounter.rb', line 36

def remote_request_headers
  @remote_request_headers
end

#remote_urlsObject

Returns the value of attribute remote_urls.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def remote_urls
  @remote_urls
end

#removeObject

Returns the value of attribute remove.



34
35
36
# File 'lib/carrierwave/mounter.rb', line 34

def remove
  @remove
end

#uploader_optionsObject

Returns the value of attribute uploader_options.



36
37
38
# File 'lib/carrierwave/mounter.rb', line 36

def uploader_options
  @uploader_options
end

Class Method Details

.build(record, column) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/carrierwave/mounter.rb', line 26

def self.build(record, column)
  if record.class.uploader_options[column][:multiple]
    Multiple.new(record, column)
  else
    Single.new(record, column)
  end
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/carrierwave/mounter.rb', line 165

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

#blank_uploaderObject



54
55
56
# File 'lib/carrierwave/mounter.rb', line 54

def blank_uploader
  uploader_class.new(record, column)
end

#cache(new_files) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/carrierwave/mounter.rb', line 74

def cache(new_files)
  return if !new_files.is_a?(Array) && new_files.blank?
  old_uploaders = uploaders
  @uploaders = new_files.map do |new_file|
    handle_error do
      if new_file.is_a?(String)
        if (uploader = old_uploaders.detect { |old_uploader| old_uploader.identifier == new_file })
          uploader.staged = true
          uploader
        else
          begin
            uploader = blank_uploader
            uploader.retrieve_from_cache!(new_file)
            uploader
          rescue CarrierWave::InvalidParameter
            nil
          end
        end
      else
        uploader = blank_uploader
        uploader.cache!(new_file)
        uploader
      end
    end
  end.reject(&:blank?)
  @removed_uploaders += (old_uploaders - @uploaders)
  write_temporary_identifier
end

#cache_namesObject



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

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

#cache_names=(cache_names) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/carrierwave/mounter.rb', line 107

def cache_names=(cache_names)
  cache_names = cache_names.reject(&:blank?)
  return if cache_names.blank?
  clear_unstaged
  cache_names.each do |cache_name|
    uploader = blank_uploader
    uploader.retrieve_from_cache!(cache_name)
    @uploaders << uploader
  rescue CarrierWave::InvalidParameter
    # ignore
  end
  write_temporary_identifier
end

#clear!Object



183
184
185
186
187
# File 'lib/carrierwave/mounter.rb', line 183

def clear!
  @removed_uploaders += uploaders
  @remove = nil
  @uploaders = []
end

#identifiersObject



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

def identifiers
  uploaders.map(&:identifier)
end

#read_identifiersObject



62
63
64
# File 'lib/carrierwave/mounter.rb', line 62

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

#remove!Object



178
179
180
181
# File 'lib/carrierwave/mounter.rb', line 178

def remove!
  uploaders.each(&:remove!)
  clear!
end

#remove?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/carrierwave/mounter.rb', line 174

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

#remove_addedObject



206
207
208
209
210
211
212
# File 'lib/carrierwave/mounter.rb', line 206

def remove_added
  current_paths = (@removed_uploaders + uploaders.select(&:staged)).map(&:path)
  @added_uploaders
    .reject {|uploader| current_paths.include?(uploader.path) }
    .each { |uploader| uploader.remove! }
  reset_changes!
end

#remove_previousObject



198
199
200
201
202
203
204
# File 'lib/carrierwave/mounter.rb', line 198

def remove_previous
  current_paths = uploaders.map(&:path)
  @removed_uploaders
    .reject {|uploader| current_paths.include?(uploader.path) }
    .each { |uploader| uploader.remove! if uploader.remove_previously_stored_files_after_update }
  reset_changes!
end

#reset_changes!Object



189
190
191
192
# File 'lib/carrierwave/mounter.rb', line 189

def reset_changes!
  @removed_uploaders = []
  @added_uploaders = []
end

#serialization_columnObject



194
195
196
# File 'lib/carrierwave/mounter.rb', line 194

def serialization_column
  option(:mount_on) || column
end

#store!Object



141
142
143
# File 'lib/carrierwave/mounter.rb', line 141

def store!
  uploaders.each(&:store!)
end

#uploader_classObject



50
51
52
# File 'lib/carrierwave/mounter.rb', line 50

def uploader_class
  record.class.uploaders[column]
end

#uploadersObject



66
67
68
69
70
71
72
# File 'lib/carrierwave/mounter.rb', line 66

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

#urls(*args) ⇒ Object



161
162
163
# File 'lib/carrierwave/mounter.rb', line 161

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

#write_identifierObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/carrierwave/mounter.rb', line 145

def write_identifier
  return if record.frozen?

  clear! if remove?

  additions, remains = uploaders.partition(&:cached?)
  existing_identifiers = (@removed_uploaders + remains).map(&:identifier)
  additions.each do |uploader|
    uploader.deduplicate(existing_identifiers)
    existing_identifiers << uploader.identifier
  end
  @added_uploaders += additions

  record.write_uploader(serialization_column, identifier)
end