Class: Attachs::Attachment

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/attachs/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, record_attribute, options = {}, attributes = {}) ⇒ Attachment

Returns a new instance of Attachment.



7
8
9
10
11
12
# File 'lib/attachs/attachment.rb', line 7

def initialize(record, record_attribute, options={}, attributes={})
  @record = record
  @record_attribute = record_attribute
  @options = options
  @original_attributes = @attributes = normalize_attributes(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/attachs/attachment.rb', line 192

def method_missing(name, *args, &block)
  if attributes.has_key?(name)
    attributes[name]
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def attributes
  @attributes
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def options
  @options
end

#original_attributesObject (readonly)

Returns the value of attribute original_attributes.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def original_attributes
  @original_attributes
end

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def record
  @record
end

#record_attributeObject (readonly)

Returns the value of attribute record_attribute.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def record_attribute
  @record_attribute
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def source
  @source
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/attachs/attachment.rb', line 5

def value
  @value
end

Instance Method Details

#_destroy=(value) ⇒ Object



96
97
98
99
100
# File 'lib/attachs/attachment.rb', line 96

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

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



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/attachs/attachment.rb', line 82

def assign(value)
  source, new_attributes = process_value(value)
  if new_attributes
    unless changed?
      @original_attributes = attributes
    end
    @attributes = new_attributes
    write_record
    @source = source
    @value = value
  end
end

#basenameObject



20
21
22
23
24
# File 'lib/attachs/attachment.rb', line 20

def basename
  if filename
    File.basename filename, extension
  end
end

#blank?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/attachs/attachment.rb', line 36

def blank?
  !present?
end

#changed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/attachs/attachment.rb', line 44

def changed?
  @original_attributes != @attributes
end

#destroyObject



187
188
189
190
# File 'lib/attachs/attachment.rb', line 187

def destroy
  assign nil
  save
end

#extensionObject



26
27
28
29
30
# File 'lib/attachs/attachment.rb', line 26

def extension
  if filename
    File.extname filename
  end
end

#fix_missingsObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/attachs/attachment.rb', line 169

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



204
205
206
207
208
209
210
211
212
213
# File 'lib/attachs/attachment.rb', line 204

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

#persisted?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/attachs/attachment.rb', line 40

def persisted?
  record.persisted? && paths.any? && uploaded_at
end

#positionObject



69
70
71
72
73
# File 'lib/attachs/attachment.rb', line 69

def position
  if options[:multiple]
    attributes[:position]
  end
end

#position=(value) ⇒ Object



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

def position=(value)
  if options[:multiple]
    attributes[:position] = value
    write_record
  end
end

#present?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/attachs/attachment.rb', line 32

def present?
  filename.present? && content_type.present? && size.present?
end

#reprocessObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/attachs/attachment.rb', line 151

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

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/attachs/attachment.rb', line 200

def respond_to_missing?(name, include_private=false)
  attributes.has_key?(name) || super
end

#saveObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/attachs/attachment.rb', line 102

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
    case source.class.name
    when 'Attachs::Attachment'
      file = storage.get(source.paths[:original])
      storage.process file, paths, styles
    when 'Upload'
      source.file.paths.each do |style, path|
        storage.copy path, paths[style]
      end
    when 'ActionDispatch::Http::UploadedFile'
      storage.process source, paths, styles
    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

#stylesObject



223
224
225
226
227
228
229
230
231
232
# File 'lib/attachs/attachment.rb', line 223

def styles
  case value = options[:styles]
  when Proc
    value.call(record) || {}
  when Hash
    value
  else
    {}
  end
end

#typeObject



48
49
50
51
52
53
54
55
56
# File 'lib/attachs/attachment.rb', line 48

def type
  if content_type
    if content_type.starts_with?('image/')
      'image'
    else
      'file'
    end
  end
end

#unpersistObject



215
216
217
218
219
220
221
# File 'lib/attachs/attachment.rb', line 215

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

#update_pathsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/attachs/attachment.rb', line 130

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/attachs/attachment.rb', line 58

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