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
15
16
17
# File 'lib/carrierwave/mounter.rb', line 10

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

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column


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

def column
  @column
end

#download_errorsObject (readonly)

Returns the value of attribute download_errors


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

def download_errors
  @download_errors
end

#integrity_errorsObject (readonly)

Returns the value of attribute integrity_errors


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

def integrity_errors
  @integrity_errors
end

#processing_errorsObject (readonly)

Returns the value of attribute processing_errors


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

def processing_errors
  @processing_errors
end

#recordObject (readonly)

Returns the value of attribute record


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

Returns the value of attribute remote_urls


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


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

def uploader_options
  @uploader_options
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)

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

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

#blank_uploaderObject


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

def blank_uploader
  uploader_class.new(record, column)
end

#cache(new_files) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/carrierwave/mounter.rb', line 43

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 { |uploader| 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.compact
end

#cache_namesObject


70
71
72
# File 'lib/carrierwave/mounter.rb', line 70

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

#cache_names=(cache_names) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/carrierwave/mounter.rb', line 74

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

#clear!Object


125
126
127
# File 'lib/carrierwave/mounter.rb', line 125

def clear!
  @uploaders = []
end

#identifiersObject


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

def identifiers
  uploaders.map(&:identifier)
end

#read_identifiersObject


31
32
33
# File 'lib/carrierwave/mounter.rb', line 31

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

#remove!Object


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

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

#remove?Boolean

Returns:

  • (Boolean)

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

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

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


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/carrierwave/mounter.rb', line 133

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|
    uploader.remove! if uploader.remove_previously_stored_files_after_update && !after_paths.include?(uploader.path)
  end
end

#serialization_columnObject


129
130
131
# File 'lib/carrierwave/mounter.rb', line 129

def serialization_column
  option(:mount_on) || column
end

#store!Object


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

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

#uploader_classObject


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

def uploader_class
  record.class.uploaders[column]
end

#uploadersObject


35
36
37
38
39
40
41
# File 'lib/carrierwave/mounter.rb', line 35

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


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

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