Sha256: cf2cfd353ad240bcf75280108cb0ec3142c1e8250c743d4c8498fc5b97db57af

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'mime/types'

module Transit
  class Asset
  
    include Mongoid::Document
    include Mongoid::Timestamps
    include Paperclip::Glue
    store_in :assets
    auto_increment
  
    field :name,                type: String
    field :meta,                type: Hash
    field :file_file_name,      type: String
    field :file_content_type,   type: String
    field :file_file_size,      type: Integer
    field :file_updated_at,     type: DateTime
    field :file_fingerprint,    type: String
    field :file_type,           type: String
  
    belongs_to    :assetable, :polymorphic => true
    before_save   :set_default_name
    before_create :set_file_type
    
    def self.asset_config_with_default
      styles = Transit::Config.assets
      return styles unless styles[:styles].present?
      styles[:styles].reverse_merge!(preview: '50x50#')
      styles
    end
    
    has_attached_file :file, Transit::Asset.asset_config_with_default
    before_post_process :skip_processing_unless_image
    
    # Determine if this asset is an image
    def image?
      (self.file_content_type =~ %r{^(image|(x-)?application)/(x-png|pjpeg|jpeg|jpg|png|gif)$})
    end

    # Provide a way to get a list of all urls for the attachment
    def file_urls
      return { :original => file.url(:original) } unless image?
      file.styles.keys.inject({}){ |hash, style|  hash.merge!(style => file.url(style)) }
    end

    def timestamp
      self.created_at.strftime("%B %d, %Y")
    end
    
    def skip_processing_unless_image
      if !(file.content_type =~ %r{^(image|(x-)?application)/(x-png|pjpeg|jpeg|jpg|png|gif)$})
        return false 
      end
    end

    private

    def set_default_name
      return true unless self.name.to_s.blank?
      self.name = self.file_file_name
    end
    
    def set_file_type
      self.file_type = ::MIME::Types.type_for(self.file_file_name.to_s).first.media_type
    end
  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transit-0.0.2 app/models/transit/asset.rb