Sha256: a4fdb672ae287f345c60452da2c2e386834a9a2e5ec898b0aa8b23f953e06cb4

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module Cambium
  class Document < ActiveRecord::Base

    extend Dragonfly::Model

    # ------------------------------------------ Plugins

    include PgSearch

    multisearchable :against => [:title]
    has_paper_trail

    dragonfly_accessor :upload

    # ------------------------------------------ Scopes

    scope :last_created, -> { order(:created_at => :desc) }

    # ------------------------------------------ Validations

    validates :title, :upload, :presence => true

    # ------------------------------------------ Instance Methods

    def to_s
      title
    end

    def image?
      ['jpg','jpeg','gif','png'].include?(upload.ext.downcase)
    end

    def pdf?
      upload.ext.downcase == 'pdf'
    end

    def has_thumb?
      thumb_url.present?
    end

    def thumb_url
      return image_url(300, 300)
      return png_cover_image_url(300, 300) if pdf?
      nil
    end

    def image_url(width, height)
      upload.thumb("#{width}x#{height}##{upload_gravity}").url
    end

    def png_cover_image_url(width, height)
      upload.thumb("#{width}x#{height}##{upload_gravity}", :format => 'png',
                   :frame => 0).url
    end

    def ext
      upload.ext
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cambium-1.3.2 app/models/cambium/document.rb
cambium-1.3.1 app/models/cambium/document.rb