Module: Paperclip::Remote
- Included in:
- ClassMethods
- Defined in:
- lib/paperclip/remote.rb
Overview
Allows fetching files from remote locations. Example:
class User < ActiveRecord::Base
has_attached_file :photo, :remote => true
attr_accessible :photo, :photo_remote_url
end
In the form you can then have:
<%= f.label :photo, "Please update a photo" %>
<%= f.file_field :photo %>
<br/>
<%= f.label :photo_remote_url, "or specify a URL" %>
<%= f.text_field :photo_remote_url %>
Instance Method Summary collapse
Instance Method Details
#has_attached_file_with_remote(name, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/paperclip/remote.rb', line 21 def has_attached_file_with_remote(name, = {}) original = has_attached_file_without_remote(name, ) return original unless [name][:remote] attr_reader :"#{name}_remote_url" define_method :"#{name}_remote_url=" do |string| url = URI(string.presence) rescue nil url = nil unless URI::HTTP === url instance_variable_set(:"@#{name}_remote_url", url) end before_validation do |record| url = record.send(:"#{name}_remote_url") break unless url.present? begin if Paperclip::VERSION < "3.1.4" io = open(url.to_s) def io.original_filename File.basename(base_uri.path) end send :"#{name}=", io else record.send(:"#{name}_remote_url=", nil) # Reset! send :"#{name}=", url end rescue OpenURI::HTTPError end end original end |