Sha256: 8cf9ce4f6ec9d52e53879b982e9461dbb80471838e6817227bd57556f7f5faa6

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

module Tenon
  class Asset < ActiveRecord::Base
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :duplicate

    # Scopes
    default_scope -> { order('created_at DESC').includes(:item_assets) }

    # Associations
    has_many :item_assets

    # Attachment
    has_attached_file :attachment, styles: Proc.new { |clip|
      Tenon::AssetStyleGenerator.generate(clip.instance)
    }

    validates_attachment_presence :attachment
    do_not_validate_attachment_file_type :attachment
    before_attachment_post_process :prevent_pdf_thumbnail

    # Tags
    acts_as_taggable

    # Filters
    before_destroy :check_attached_items

    def self.with_type(type)
      if %w(images).include?(type)
        where('attachment_content_type LIKE ?', "%#{type.singularize}%")
      else
        documents
      end
    end

    def self.documents
      where('attachment_content_type NOT LIKE ?', '%image%')
    end

    def is_image?
      attachment_content_type =~ /image/
    end

    def dimensions(style = :original)
      attach = style.to_sym == :original ? attachment : attachment.styles[style]
      file = Paperclip.io_adapters.for(attach)
      Paperclip::Geometry.from_file(file).to_s.split('x')
    end

    def cropping?
      crop_x.present?
    end

    private

    def check_attached_items
      unless item_assets.count == 0
        errors.add(:base, "This asset has been attached to items in your site and can't be deleted.")
        return false
      end
    end

    def prevent_pdf_thumbnail
      return false unless attachment_content_type.index('image')
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tenon-1.0.62 app/models/tenon/asset.rb
tenon-1.0.61 app/models/tenon/asset.rb
tenon-1.0.60 app/models/tenon/asset.rb
tenon-1.0.59 app/models/tenon/asset.rb
tenon-1.0.57 app/models/tenon/asset.rb