Sha256: 1d71d8d37a602605eb29366a43dd3c2bfad8c87c55375b02e338d0a29c15e67f

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8

class PageFile < ActiveRecord::Base
  include Dis::Model

  belongs_to :page

  validates_data_presence
  validates :content_type, presence: true
  validates :filename, presence: true
  validates :content_length, presence: true

  acts_as_list scope: :page

  localizable do
    attribute :description
  end

  before_validation :set_name_from_filename

  after_save ThinkingSphinx::RealTime.callback_for(:page_file)

  class << self
    def formats
      {
        "audio/mpeg"      => :mp3,
        "image/gif"       => :gif,
        "image/jpeg"      => :jpg,
        "image/jpg"       => :jpg,
        "image/pjpeg"     => :jpg,
        "image/png"       => :png,
        "application/pdf" => :pdf
      }
    end
  end

  def format?
    content_type && self.class.formats.keys.include?(content_type)
  end

  def format
    self.class.formats[content_type]
  end

  def filename_extension
    if filename_extension?
      filename.match(/\.([^\.]+)$/)[1]
    else
      ""
    end
  end

  def filename_extension?
    filename =~ /\./
  end

  def to_param
    if filename_extension?
      "#{id}-#{content_hash}.#{filename_extension}"
    else
      "#{id}-#{content_hash}"
    end
  end

  delegate :published, to: :page

  private

  def set_name_from_filename
    self.name = File.basename(filename, ".*") if filename? && !name?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 app/models/page_file.rb