Sha256: ae7c130ee8b7ec487067a770afd0cec7e770e537951a5b8a48733094366f67df

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

gem 'paperclip', ">=2.3.3"
require 'paperclip'
require 'digest/md5'
include Paperclip

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

1 entries across 1 versions & 1 rubygems

Version Path
sga_paperclippolymorph-0.2.4 lib/sga/asset.rb