Class: Asset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sga/asset.rb

Instance Method Summary collapse

Instance Method Details

#browser_safe?Boolean Also known as: web_safe?

Returns:

  • (Boolean)


29
30
31
# File 'lib/sga/asset.rb', line 29

def browser_safe?
  %w(jpg gif png).include?(url.split('.').last.sub(/\?.+/, "").downcase)
end

#content_typeObject



25
26
27
# File 'lib/sga/asset.rb', line 25

def content_type
  data_content_type
end

#detach(attached) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


49
50
51
52
53
# File 'lib/sga/asset.rb', line 49

def detach(attached)
  a = attachings.find(:first, :conditions => ["attachable_id = ? AND attachable_type = ?", attached, attached.class.to_s])
  raise ActiveRecord::RecordNotFound unless a
  a.destroy
end

#file_md5sumObject



59
60
61
# File 'lib/sga/asset.rb', line 59

def file_md5sum
  IO.popen("md5sum #{path} | awk '{print $1}'"){|f| f.gets.strip}       
end

#iconObject

This method assumes you have images that corespond to the filetypes. For example “image/png” becomes “image-png.png”



45
46
47
# File 'lib/sga/asset.rb', line 45

def icon
  "#{data_content_type.gsub(/[\/\.]/,'-')}.png"
end

#nameObject



21
22
23
# File 'lib/sga/asset.rb', line 21

def name
  data_file_name
end

#pathObject



55
56
57
# File 'lib/sga/asset.rb', line 55

def path
  File.join RAILS_ROOT, 'public', self.url_without_random
end

#replace_style(style, file) ⇒ Object

This method will replace one of the existing thumbnails with an file provided.



35
36
37
38
39
40
41
# File 'lib/sga/asset.rb', line 35

def replace_style(style, file)
  style = style.downcase.to_sym
  if data.styles.keys.include?(style)
    if File.exist?(RAILS_ROOT + '/public' + a.data(style))
    end
  end
end

#url(*args) ⇒ Object



17
18
19
# File 'lib/sga/asset.rb', line 17

def url(*args)
  data.url(*args)
end

#url_without_random(format = :original) ⇒ Object



63
64
65
# File 'lib/sga/asset.rb', line 63

def url_without_random format=:original
  self.url(format) =~ /^(.*)\?\d*$/ ? $1 : self.url(format)
end