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.


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

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


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

def remove
  @remove
end

#uploader_optionsObject

Returns the value of attribute uploader_options


144
145
146
# File 'lib/carrierwave/mounter.rb', line 144

def uploader_options
  @uploader_options
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)

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

def blank?
  uploaders.empty?
end

#blank_uploaderObject


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

def blank_uploader
  uploader_class.new(record, column)
end

#cache(new_files) ⇒ Object


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

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


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

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

#cache_names=(cache_names) ⇒ Object


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

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


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

def identifiers
  uploaders.map(&:identifier)
end

#read_identifiersObject


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

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

#remove!Object


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

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

#remove?Boolean

Returns:

  • (Boolean)

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

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

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


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/carrierwave/mounter.rb', line 124

def remove_previous(before=nil, after=nil)
  if before
    before = before.reject(&:blank?).map do |value|
      if value.is_a?(String)
        uploader = blank_uploader
        uploader.retrieve_from_store!(before)
        uploader
      else
        value
      end
    end
    after_paths = after.reject(&:blank?).map { |value| value.respond_to?(:path) ? value.path : value }
    before.each do |uploader|
      if uploader.remove_previously_stored_files_after_update and not after_paths.include?(uploader.path)
        uploader.remove!
      end
    end
  end
end

#serialization_columnObject


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

def serialization_column
  option(:mount_on) || column
end

#store!Object


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

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

#uploader_classObject


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

def uploader_class
  record.class.uploaders[column]
end

#uploadersObject


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

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


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

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