Sha256: 27bd2f2bc23dc7257cbd375b946edbb32b171639fe481c5ae2a24e50b798bc30

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

gem 'paperclip', ">=2.3.1.1"
require 'paperclip'
require 'digest/md5'

class Asset < ActiveRecord::Base
  has_many :attachings, :dependent => :destroy

  has_attached_file :data,
                    :styles => {
                                 :tiny => "64x64>",
                                 :small => "176x112>",
                                 :medium => "630x630>",
                                 :large => "1024x1024>"
                               } 

  def url(*args)
    data.url(*args)
  end
  
  def name
    data_file_name
  end
  
  def content_type
    data_content_type
  end
  
  def browser_safe?
    %w(jpg gif png).include?(url.split('.').last.sub(/\?.+/, "").downcase)
  end
  alias_method :web_safe?, :browser_safe?
  
  # This method will replace one of the existing thumbnails with an file provided.
  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
  
  # This method assumes you have images that corespond to the filetypes.
  # For example "image/png" becomes "image-png.png"
  def icon
    "#{data_content_type.gsub(/[\/\.]/,'-')}.png"
  end
    
  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

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

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

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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sga_paperclippolymorph-0.2.1 lib/sga/asset.rb
sga_paperclippolymorph-0.2.0 lib/sga/asset.rb