Sha256: 9d6afa12e8dc8e57399ec60533f543a6f862b983b2ec9518d7cbbbe304624f6a

Contents?: true

Size: 2 KB

Versions: 21

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module Spree
  class Image < Asset
    validate :no_attachment_errors

    has_attached_file :attachment,
                      styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' },
                      default_style: :product,
                      default_url: 'noimage/:style.png',
                      url: '/spree/products/:id/:style/:basename.:extension',
                      path: ':rails_root/public/spree/products/:id/:style/:basename.:extension',
                      convert_options: { all: '-strip -auto-orient -colorspace sRGB' }
    validates_attachment :attachment,
      presence: true,
      content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) }

    # save the w,h of the original image (from which others can be calculated)
    # we need to look at the write-queue for images which have not been saved yet
    after_post_process :find_dimensions, if: :valid?

    def mini_url
      Spree::Deprecation.warn(
        'Spree::Image#mini_url is DEPRECATED. Use Spree::Image#url(:mini) instead.'
      )
      attachment.url(:mini, false)
    end

    def url(size)
      attachment.url(size)
    end

    def filename
      attachment_file_name
    end

    def find_dimensions
      temporary = attachment.queued_for_write[:original]
      filename = temporary.path unless temporary.nil?
      filename = attachment.path if filename.blank?
      geometry = Paperclip::Geometry.from_file(filename)
      self.attachment_width  = geometry.width
      self.attachment_height = geometry.height
    end

    # if there are errors from the plugin, then add a more meaningful message
    def no_attachment_errors
      unless attachment.errors.empty?
        # uncomment this to get rid of the less-than-useful interim messages
        # errors.clear
        errors.add :attachment, "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file."
        false
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
solidus_core-2.8.6 app/models/spree/image.rb
solidus_core-2.8.5 app/models/spree/image.rb
solidus_core-2.8.4 app/models/spree/image.rb
solidus_core-2.8.3 app/models/spree/image.rb
solidus_core-2.8.2 app/models/spree/image.rb
solidus_core-2.8.1 app/models/spree/image.rb
solidus_core-2.8.0 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.3 app/models/spree/image.rb
solidus_core_devise_token_auth-2.8.0.alpha.1 app/models/spree/image.rb