Module: Attachs::Attachment::Processing
- Extended by:
- ActiveSupport::Concern
- Included in:
- Attachs::Attachment
- Defined in:
- lib/attachs/attachment/processing.rb
Instance Method Summary collapse
- #_destroy=(value) ⇒ Object
- #assign(value) ⇒ Object (also: #value=)
- #destroy ⇒ Object
- #fix_missings ⇒ Object
- #persist ⇒ Object
- #reprocess ⇒ Object
- #save ⇒ Object
- #unpersist ⇒ Object
- #update_paths ⇒ Object
- #url(style = :original) ⇒ Object
Instance Method Details
#_destroy=(value) ⇒ Object
79 80 81 82 83 |
# File 'lib/attachs/attachment/processing.rb', line 79 def _destroy=(value) if ActiveRecord::Type::Boolean.new.type_cast_from_user(value) assign nil end end |
#assign(value) ⇒ Object Also known as: value=
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/attachs/attachment/processing.rb', line 15 def assign(value) source, new_attributes = normalize_value(value) if new_attributes unless changed? @original_attributes = attributes end @attributes = new_attributes write_record @source = source @value = value end end |
#destroy ⇒ Object
74 75 76 77 |
# File 'lib/attachs/attachment/processing.rb', line 74 def destroy assign nil save end |
#fix_missings ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/attachs/attachment/processing.rb', line 126 def fix_missings if changed? raise 'Save attachment before fix missings' elsif present? && styles missings = paths.slice(:original) paths.except(:original).each do |style, path| unless storage.exists?(path) missings[style] = path Rails.logger.info "Generating: #{style} => #{path}" end end if missings.size > 1 file = storage.get(paths[:original]) storage.process file, missings, styles end end end |
#persist ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/attachs/attachment/processing.rb', line 29 def persist if changed? && present? new_paths = generate_paths if paths != new_paths attributes[:paths] = new_paths attributes[:uploaded_at] = Time.now write_record end end end |
#reprocess ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/attachs/attachment/processing.rb', line 108 def reprocess if changed? raise 'Save attachment before reprocess' elsif present? && styles file = storage.get(paths[:original]) paths.each do |style, path| if storage.exists?(path) storage.delete path end Rails.logger.info "Regenerating: #{style} => #{path}" end new_paths = generate_paths storage.process file, new_paths, styles attributes[:paths] = new_paths update_record end end |
#save ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/attachs/attachment/processing.rb', line 48 def save if changed? original_paths = (original_attributes[:paths] || {}) original_old_paths = (original_attributes[:old_paths] || []) if original_paths.any? || original_old_paths.any? Jobs::DeleteJob.perform_later (original_paths.values + original_old_paths) end if source.is_a?(Attachment) file = storage.get(source.paths[:original]) storage.process file, paths, styles elsif source.is_a?(ActionDispatch::Http::UploadedFile) storage.process source, paths, styles elsif source.class.name == 'Upload' source.file.paths.each do |style, path| storage.copy path, paths[style] end end @source = @value = nil @original_attributes = @attributes elsif present? if paths != generate_paths Jobs::UpdateJob.perform_later record, record_attribute.to_s end end end |
#unpersist ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/attachs/attachment/processing.rb', line 40 def unpersist if changed? && present? attributes[:paths] = original_attributes[:paths] attributes[:uploaded_at] = original_attributes[:uploaded_at] write_record end end |
#update_paths ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/attachs/attachment/processing.rb', line 85 def update_paths if changed? raise 'Save attachment before update paths' else new_paths = generate_paths if paths != new_paths new_paths.each do |style, new_path| original_path = paths[style] if original_path != new_path unless storage.exists?(new_path) storage.copy original_path, new_path end attributes[:paths] ||= {} attributes[:paths][style] = new_path attributes[:old_paths] ||= [] attributes[:old_paths] |= [original_path] end end update_record end end end |
#url(style = :original) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/attachs/attachment/processing.rb', line 6 def url(style=:original) if path = paths[style] storage.url paths[style] elsif template = [:default_path] path = generate_path(template, style) storage.url path end end |