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_error ⇒ Object
readonly
:nodoc:.
-
#integrity_error ⇒ Object
readonly
:nodoc:.
-
#processing_error ⇒ 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
- #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 |
# File 'lib/carrierwave/mounter.rb', line 10 def initialize(record, column, ={}) @record = record @column = column @options = record.class.[column] end |
Instance Attribute Details
#column ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def column @column end |
#download_error ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def download_error @download_error end |
#integrity_error ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def integrity_error @integrity_error end |
#processing_error ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/carrierwave/mounter.rb', line 6 def processing_error @processing_error 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.
155 156 157 |
# File 'lib/carrierwave/mounter.rb', line 155 def @uploader_options end |
Instance Method Details
#blank? ⇒ Boolean
108 109 110 |
# File 'lib/carrierwave/mounter.rb', line 108 def blank? uploaders.none?(&:present?) end |
#blank_uploader ⇒ Object
20 21 22 |
# File 'lib/carrierwave/mounter.rb', line 20 def blank_uploader uploader_class.new(record, column) end |
#cache(new_files) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/carrierwave/mounter.rb', line 40 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_names ⇒ Object
58 59 60 |
# File 'lib/carrierwave/mounter.rb', line 58 def cache_names uploaders.map(&:cache_name).compact end |
#cache_names=(cache_names) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/carrierwave/mounter.rb', line 62 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 |
#identifiers ⇒ Object
24 25 26 |
# File 'lib/carrierwave/mounter.rb', line 24 def identifiers uploaders.map(&:identifier) end |
#read_identifiers ⇒ Object
28 29 30 |
# File 'lib/carrierwave/mounter.rb', line 28 def read_identifiers [record.read_uploader(serialization_column)].flatten.reject(&:blank?) end |
#remove! ⇒ Object
116 117 118 119 |
# File 'lib/carrierwave/mounter.rb', line 116 def remove! uploaders.reject(&:blank?).each(&:remove!) @uploaders = [] end |
#remove? ⇒ Boolean
112 113 114 |
# File 'lib/carrierwave/mounter.rb', line 112 def remove? remove.present? && remove !~ /\A0|false$\z/ end |
#remove_previous(before = nil, after = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/carrierwave/mounter.rb', line 125 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| if uploader.remove_previously_stored_files_after_update and not after_paths.include?(uploader.path) uploader.remove! end end end |
#serialization_column ⇒ Object
121 122 123 |
# File 'lib/carrierwave/mounter.rb', line 121 def serialization_column option(:mount_on) || column end |
#store! ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/carrierwave/mounter.rb', line 96 def store! if remove? remove! else uploaders.reject(&:blank?).each(&:store!) end end |
#uploader_class ⇒ Object
16 17 18 |
# File 'lib/carrierwave/mounter.rb', line 16 def uploader_class record.class.uploaders[column] end |
#uploaders ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/carrierwave/mounter.rb', line 32 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
104 105 106 |
# File 'lib/carrierwave/mounter.rb', line 104 def urls(*args) uploaders.map { |u| u.url(*args) } end |