Sha256: 3c71075ddddae95d59bef419e6a850e1bf744bbea3a7f2b146c3ef7f2d9d8ad5

Contents?: true

Size: 1.63 KB

Versions: 48

Compression:

Stored size: 1.63 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 videos).include?(type)
        where('attachment_content_type LIKE ?', "%#{type.singularize}%")
      else
        documents
      end
    end

    def self.documents
      where('attachment_content_type NOT LIKE ?', '%image%')
      .where('attachment_content_type NOT LIKE ?', '%video%')
    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

48 entries across 48 versions & 1 rubygems

Version Path
tenon-1.0.48 app/models/tenon/asset.rb
tenon-1.0.47 app/models/tenon/asset.rb
tenon-1.0.46 app/models/tenon/asset.rb
tenon-1.0.45 app/models/tenon/asset.rb
tenon-1.0.44 app/models/tenon/asset.rb
tenon-1.0.43 app/models/tenon/asset.rb
tenon-1.0.42 app/models/tenon/asset.rb
tenon-1.0.41 app/models/tenon/asset.rb
tenon-1.0.40 app/models/tenon/asset.rb
tenon-1.0.39 app/models/tenon/asset.rb
tenon-1.0.38 app/models/tenon/asset.rb
tenon-1.0.37 app/models/tenon/asset.rb
tenon-1.0.36 app/models/tenon/asset.rb
tenon-1.0.35 app/models/tenon/asset.rb
tenon-1.0.33 app/models/tenon/asset.rb
tenon-1.0.32 app/models/tenon/asset.rb
tenon-1.0.31 app/models/tenon/asset.rb
tenon-1.0.30 app/models/tenon/asset.rb
tenon-1.0.29 app/models/tenon/asset.rb
tenon-1.0.28 app/models/tenon/asset.rb