Sha256: ce0908dba16fa0f52ed316abb7b46a9bbcd9b29d9ec3141e833366a2ebceca81

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

module Tenon
  class Asset < Tenon::ApplicationRecord
    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

    # 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

10 entries across 10 versions & 1 rubygems

Version Path
tenon-2.1.0 app/models/tenon/asset.rb
tenon-2.0.8 app/models/tenon/asset.rb
tenon-2.0.7 app/models/tenon/asset.rb
tenon-2.0.6 app/models/tenon/asset.rb
tenon-2.0.5 app/models/tenon/asset.rb
tenon-2.0.4 app/models/tenon/asset.rb
tenon-2.0.3 app/models/tenon/asset.rb
tenon-2.0.2 app/models/tenon/asset.rb
tenon-2.0.1 app/models/tenon/asset.rb
tenon-2.0.0 app/models/tenon/asset.rb