suryart/spree_lookbook

View on GitHub
app/models/spree/lookbook_collection_image.rb

Summary

Maintainability
A
2 hrs
Test Coverage
module Spree
  class LookbookCollectionImage < Asset
    validates_attachment_presence :attachment
    validate :no_attachment_errors

    attr_accessible :alt, :attachment, :position, :viewable_type, :viewable_id

    has_attached_file :attachment,
                      :styles => { 
                                    :mini => '48x48>', 
                                    :small => '100x100>', 
                                    :lookbook => '240x240>', 
                                    :large => '600x600>' 
                                  },
                      :default_style => :lookbook,
                      :url => '/spree/lookbookss/:id/:style/:basename.:extension',
                      :path => ':rails_root/public/spree/lookbookss/:id/:style/:basename.:extension',
                      :convert_options => { :all => '-strip' }
    # 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

    default_scope :order => "#{self.table_name}.position"

    include Spree::Core::S3Support
    supports_s3 :attachment

    #used by admin lookbookss autocomplete
    def mini_url
      attachment.url(:mini, false)
    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 interrim 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