Class: Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Dis::Model, PagesCore::Sweepable
Defined in:
app/models/attachment.rb

Instance Attribute Summary

Attributes included from PagesCore::Sweepable

#cache_swept

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.formatsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/attachment.rb', line 26

def formats
  {
    "audio/mpeg"      => :mp3,
    "image/gif"       => :gif,
    "image/jpeg"      => :jpg,
    "image/jpg"       => :jpg,
    "image/pjpeg"     => :jpg,
    "image/png"       => :png,
    "application/pdf" => :pdf
  }
end

.verifierObject



20
21
22
23
24
# File 'app/models/attachment.rb', line 20

def verifier
  @verifier ||= PagesCore::DigestVerifier.new(
    Rails.application.key_generator.generate_key("attachments")
  )
end

Instance Method Details

#digestObject



39
40
41
42
43
# File 'app/models/attachment.rb', line 39

def digest
  return unless id

  self.class.verifier.generate(id.to_s)
end

#filename_extensionObject



53
54
55
56
57
58
59
# File 'app/models/attachment.rb', line 53

def filename_extension
  if filename_extension?
    filename.match(/\.([^\.]+)$/)[1]
  else
    ""
  end
end

#filename_extension?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/attachment.rb', line 61

def filename_extension?
  filename =~ /\./
end

#formatObject



49
50
51
# File 'app/models/attachment.rb', line 49

def format
  self.class.formats[content_type]
end

#format?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/attachment.rb', line 45

def format?
  content_type && self.class.formats.key?(content_type)
end

#to_paramObject

Includes a timestamp fingerprint in the URL param, so that rendered images can be cached indefinitely.



67
68
69
# File 'app/models/attachment.rb', line 67

def to_param
  [id, updated_at.utc.to_s(cache_timestamp_format)].join("-")
end