Sha256: 028f3c88c993eeedfe297e231effff862ff1a2bb50024cff113f652fa2f59d9a

Contents?: true

Size: 1.53 KB

Versions: 18

Compression:

Stored size: 1.53 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

    # 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

18 entries across 18 versions & 1 rubygems

Version Path
tenon-1.1.4 app/models/tenon/asset.rb
tenon-1.1.3 app/models/tenon/asset.rb
tenon-1.1.2 app/models/tenon/asset.rb
tenon-1.1.1 app/models/tenon/asset.rb
tenon-1.0.76 app/models/tenon/asset.rb
tenon-1.0.75 app/models/tenon/asset.rb
tenon-1.0.74 app/models/tenon/asset.rb
tenon-1.0.73 app/models/tenon/asset.rb
tenon-1.0.72 app/models/tenon/asset.rb
tenon-1.0.71 app/models/tenon/asset.rb
tenon-1.0.70 app/models/tenon/asset.rb
tenon-1.0.69 app/models/tenon/asset.rb
tenon-1.0.68 app/models/tenon/asset.rb
tenon-1.0.67 app/models/tenon/asset.rb
tenon-1.0.66 app/models/tenon/asset.rb
tenon-1.0.65 app/models/tenon/asset.rb
tenon-1.0.64 app/models/tenon/asset.rb
tenon-1.0.63 app/models/tenon/asset.rb