Class: CarrierWave::Mounter
- Inherits:
-
Object
- Object
- CarrierWave::Mounter
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.
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
#column ⇒ Object
Returns the value of attribute column.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def column
@column
end
|
#download_errors ⇒ Object
Returns the value of attribute download_errors.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def download_errors
@download_errors
end
|
#integrity_errors ⇒ Object
Returns the value of attribute integrity_errors.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def integrity_errors
@integrity_errors
end
|
#processing_errors ⇒ Object
Returns the value of attribute processing_errors.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def processing_errors
@processing_errors
end
|
#record ⇒ Object
Returns the value of attribute record.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def record
@record
end
|
Returns the value of attribute remote_request_headers.
36
37
38
|
# File 'lib/carrierwave/mounter.rb', line 36
def
@remote_request_headers
end
|
#remote_urls ⇒ Object
Returns the value of attribute remote_urls.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def remote_urls
@remote_urls
end
|
#remove ⇒ Object
Returns the value of attribute remove.
34
35
36
|
# File 'lib/carrierwave/mounter.rb', line 34
def remove
@remove
end
|
#uploader_options ⇒ Object
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
166
167
168
|
# File 'lib/carrierwave/mounter.rb', line 166
def blank?
uploaders.none?(&:present?)
end
|
#blank_uploader ⇒ Object
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_names ⇒ Object
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
end
write_temporary_identifier
end
|
#clear! ⇒ Object
184
185
186
187
188
|
# File 'lib/carrierwave/mounter.rb', line 184
def clear!
@removed_uploaders += uploaders
@remove = nil
@uploaders = []
end
|
#identifiers ⇒ Object
58
59
60
|
# File 'lib/carrierwave/mounter.rb', line 58
def identifiers
uploaders.map(&:identifier)
end
|
#read_identifiers ⇒ Object
62
63
64
|
# File 'lib/carrierwave/mounter.rb', line 62
def read_identifiers
[record.read_uploader(serialization_column)].flatten.reject(&:blank?)
end
|
#remove! ⇒ Object
179
180
181
182
|
# File 'lib/carrierwave/mounter.rb', line 179
def remove!
uploaders.each(&:remove!)
clear!
end
|
#remove? ⇒ Boolean
175
176
177
|
# File 'lib/carrierwave/mounter.rb', line 175
def remove?
remove.present? && (remove.to_s !~ /\A0|false$\z/)
end
|
#remove_added ⇒ Object
207
208
209
210
211
212
213
|
# File 'lib/carrierwave/mounter.rb', line 207
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_previous ⇒ Object
199
200
201
202
203
204
205
|
# File 'lib/carrierwave/mounter.rb', line 199
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
190
191
192
193
|
# File 'lib/carrierwave/mounter.rb', line 190
def reset_changes!
@removed_uploaders = []
@added_uploaders = []
end
|
#serialization_column ⇒ Object
195
196
197
|
# File 'lib/carrierwave/mounter.rb', line 195
def serialization_column
option(:mount_on) || column
end
|
#store! ⇒ Object
142
143
144
|
# File 'lib/carrierwave/mounter.rb', line 142
def store!
uploaders.each(&:store!)
end
|
#uploader_class ⇒ Object
50
51
52
|
# File 'lib/carrierwave/mounter.rb', line 50
def uploader_class
record.class.uploaders[column]
end
|
#uploaders ⇒ Object
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
162
163
164
|
# File 'lib/carrierwave/mounter.rb', line 162
def urls(*args)
uploaders.map { |u| u.url(*args) }
end
|
#write_identifier ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/carrierwave/mounter.rb', line 146
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
|