Sha256: 41c75f3ba78a371491288e65a8baacecd03195b9430cd26c6ca1c31a9555d015

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Smithy
  class Asset < ActiveRecord::Base
    validates_presence_of :file, :name
    belongs_to :asset_source, inverse_of: :assets
    has_many :images, :dependent => :destroy

    attachment :file

    before_validation :set_name

    default_scope -> { order(:name) }

    def file_type
      case file_content_type
      when /^image\/svg/
        :direct_image
      when /^image\/?/
        :image
      when /pdf/
        :pdf
      when /msword|wordprocessing/
        :word
      when /excel|spreadsheet/
        :excel
      when /powerpoint|presentation/
        :powerpoint
      when /txt/
        :text
      when /rtf/
        :document
      else
        :default
      end
    end

    def to_liquid
      {
        'id' => self.id,
        'name' => self.name,
        'content_type' => self.file_content_type,
        'file' => self.file,
        'file_name' => self.file_filename,
        'file_width' => self.file_width,
        'file_height' => self.file_height,
        'file_size' => self.file_size,
        'remote_url' => self.url,
        'url' => self.url
      }
    end

    def url
      Refile.attachment_url(self, :file)
    end

    def data
      file.read
    end

    private
      def set_name
        if self.uploaded_file_url?
          self.name = File.basename(self.uploaded_file_url, '.*').titleize unless self.name?
        elsif self.file.present?
          self.name = File.basename(self.file_filename, '.*').titleize unless self.name?
        end
     end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smithycms-0.8.1 app/models/smithy/asset.rb