Sha256: 1c8b602128c76ebbe727a4342b5ddb9dff3efad8d4bd5c425341d5651216043f

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Assetabler
  class Asset < ActiveRecord::Base

    has_many :asset_attachments, :as => :assetable, :dependent => :destroy
    has_many :assetable, :through => :asset_attachments
    
    before_save :update_asset_attributes



    # File Type Helpers
    # ---------------------------------------------------------------------

    # Is the asset a image?
    def image?
      self.type == "Assetabler::Image"
    end

    # Is the asset a document?
    def document?
      self.type == "Assetabler::Document"
    end

    # Is the asset a video?
    def video?
      self.type == "Assetabler::Video"
    end

    def external_service?
      self.type == "Assetabler::ExternalService"
    end


    # File meta and helpers
    # ---------------------------------------------------------------------

    # Get the file type extension from the filename
    def extension
      filename = File.extname(self.filename.to_s)
      filename[0] = '' # remove the dot, i.e. (.docx or .pptx)
      filename
    end

    # Add some custom attributes to the asset
    def update_asset_attributes
      if !self.external_service? and self.present? and self.changed?
        self.content_type = self.filename.file.content_type
        self.file_size = self.filename.file.size
        self.width, self.height = `identify -format "%wx%h" #{self.filename.file.path}`.split(/x/) unless self.document? or self.width? or self.height?
        self.ratio = self.width.to_f / self.height.to_f if self.width.present? and self.height.present?
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assetable-0.3.1 app/models/assetabler/asset.rb
assetable-0.3.0 app/models/assetabler/asset.rb