Module: Attachs::Attachment::Processing

Extended by:
ActiveSupport::Concern
Included in:
Attachs::Attachment
Defined in:
lib/attachs/attachment/processing.rb

Instance Method Summary collapse

Instance Method Details

#_destroy=(value) ⇒ Object



82
83
84
85
86
# File 'lib/attachs/attachment/processing.rb', line 82

def _destroy=(value)
  if ActiveRecord::Type::Boolean.new.type_cast_from_user(value)
    assign nil
  end
end

#assign(value) ⇒ Object Also known as: value=



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/attachs/attachment/processing.rb', line 17

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

#destroyObject



77
78
79
80
# File 'lib/attachs/attachment/processing.rb', line 77

def destroy
  assign nil
  save
end

#fix_missingsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/attachs/attachment/processing.rb', line 127

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

#persistObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/attachs/attachment/processing.rb', line 31

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

#reprocessObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/attachs/attachment/processing.rb', line 109

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

#saveObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/attachs/attachment/processing.rb', line 50

def save
  if changed?
    if original_attributes[:paths].any? || original_attributes[:old_paths].any?
      Jobs::DeleteJob.perform_later(
        original_attributes[:paths].values +
        original_attributes[: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

#unpersistObject



42
43
44
45
46
47
48
# File 'lib/attachs/attachment/processing.rb', line 42

def unpersist
  if changed? && present?
    attributes[:paths] = original_attributes[:paths]
    attributes[:uploaded_at] = original_attributes[:uploaded_at]
    write_record
  end
end

#update_pathsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/attachs/attachment/processing.rb', line 88

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][style] = new_path
          attributes[:old_paths] |= [original_path]
        end
      end
      update_record
    end
  end
end

#url(style = :original) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/attachs/attachment/processing.rb', line 6

def url(style=:original)
  paths = attributes[:paths]
  if paths.has_key?(style)
    storage.url paths[style]
  elsif options.has_key?(:default_path)
    template = options[:default_path]
    path = generate_path(template, style)
    storage.url path
  end
end