Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted by paperclip_by_url ( 15 years ago )
require 'open-uri'

module PaperclipUrl

  def paperclip_by_url
    #      #remote URL for paperclip
    attr_accessor :image_url
    before_validation :download_remote_image, :if => :image_url_provided?
    validates_presence_of :image_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'
    include PaperclipUrl::InstanceMethods
  end


  #  # This module contains instance methods
    module InstanceMethods
  
      def image_url_provided?
        !self.image_url.blank?
      end
  
      def download_remote_image
        self.image = do_download_remote_image
      end
  
      def do_download_remote_image
        io = open(URI.parse(image_url))
        def io.original_filename; base_uri.path.split('/').last; end
        io.original_filename.blank? ? nil : io
      rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
      end
  
    end
end

 

Revise this Paste

Your Name: Code Language: