Class: CarrierWave::Mounter
- Inherits:
-
Object
- Object
- CarrierWave::Mounter
- 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
-
#column ⇒ Object
readonly
:nodoc:.
-
#download_errors ⇒ Object
readonly
:nodoc:.
-
#integrity_errors ⇒ Object
readonly
:nodoc:.
-
#processing_errors ⇒ Object
readonly
:nodoc:.
-
#record ⇒ Object
readonly
:nodoc:.
-
#remote_request_headers ⇒ Object
Returns the value of attribute remote_request_headers.
-
#remote_urls ⇒ Object
:nodoc:.
-
#remove ⇒ Object
Returns the value of attribute remove.
-
#uploader_options ⇒ Object
Returns the value of attribute uploader_options.
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #blank_uploader ⇒ Object
- #cache(new_files) ⇒ Object
- #cache_names ⇒ Object
- #cache_names=(cache_names) ⇒ Object
- #clear! ⇒ Object
- #identifiers ⇒ Object
-
#initialize(record, column, options = {}) ⇒ Mounter
constructor
A new instance of Mounter.
- #read_identifiers ⇒ Object
- #remove! ⇒ Object
- #remove? ⇒ Boolean
- #remove_previous(before = nil, after = nil) ⇒ Object
- #serialization_column ⇒ Object
- #store! ⇒ Object
- #uploader_class ⇒ Object
- #uploaders ⇒ Object
- #urls(*args) ⇒ Object
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, ={}) @record = record @column = column @options = record.class.[column] @download_errors = [] @processing_errors = [] @integrity_errors = [] end |
Instance Attribute Details
#column ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def column @column end |
#download_errors ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def download_errors @download_errors end |
#integrity_errors ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def integrity_errors @integrity_errors end |
#processing_errors ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def processing_errors @processing_errors end |
#record ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def record @record end |
#remote_request_headers ⇒ Object
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_urls ⇒ Object
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def remote_urls @remote_urls end |
#remove ⇒ Object
Returns the value of attribute remove.
8 9 10 |
# File 'lib/carrierwave/mounter.rb', line 8 def remove @remove end |
#uploader_options ⇒ Object
Returns the value of attribute uploader_options.
160 161 162 |
# File 'lib/carrierwave/mounter.rb', line 160 def @uploader_options end |
Instance Method Details
#blank? ⇒ Boolean
111 112 113 |
# File 'lib/carrierwave/mounter.rb', line 111 def blank? uploaders.none?(&:present?) end |
#blank_uploader ⇒ Object
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_names ⇒ Object
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 |
# File 'lib/carrierwave/mounter.rb', line 74 def cache_names=(cache_names) return if cache_names.blank? clear_unstaged cache_names.map do |cache_name| begin uploader = blank_uploader uploader.retrieve_from_cache!(cache_name) @uploaders << uploader rescue CarrierWave::InvalidParameter # ignore end end end |
#clear! ⇒ Object
124 125 126 |
# File 'lib/carrierwave/mounter.rb', line 124 def clear! @uploaders = [] end |
#identifiers ⇒ Object
27 28 29 |
# File 'lib/carrierwave/mounter.rb', line 27 def identifiers uploaders.map(&:identifier) end |
#read_identifiers ⇒ Object
31 32 33 |
# File 'lib/carrierwave/mounter.rb', line 31 def read_identifiers [record.read_uploader(serialization_column)].flatten.reject(&:blank?) end |
#remove! ⇒ Object
119 120 121 122 |
# File 'lib/carrierwave/mounter.rb', line 119 def remove! uploaders.reject(&:blank?).each(&:remove!) @uploaders = [] end |
#remove? ⇒ Boolean
115 116 117 |
# File 'lib/carrierwave/mounter.rb', line 115 def remove? remove.present? && remove !~ /\A0|false$\z/ end |
#remove_previous(before = nil, after = nil) ⇒ Object
132 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 |
# File 'lib/carrierwave/mounter.rb', line 132 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_column ⇒ Object
128 129 130 |
# File 'lib/carrierwave/mounter.rb', line 128 def serialization_column option(:mount_on) || column end |
#store! ⇒ Object
103 104 105 |
# File 'lib/carrierwave/mounter.rb', line 103 def store! uploaders.reject(&:blank?).each(&:store!) end |
#uploader_class ⇒ Object
19 20 21 |
# File 'lib/carrierwave/mounter.rb', line 19 def uploader_class record.class.uploaders[column] end |
#uploaders ⇒ Object
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
107 108 109 |
# File 'lib/carrierwave/mounter.rb', line 107 def urls(*args) uploaders.map { |u| u.url(*args) } end |