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 %>
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
20 21 22 23 |
# File 'lib/paperclip/remote.rb', line 20 def self.included(base) locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}") I18n.load_path += locale_path unless I18n.load_path.include?(locale_path) end |
Instance Method Details
#has_attached_file_with_remote(name, options = {}) ⇒ Object
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 53 54 55 56 57 |
# File 'lib/paperclip/remote.rb', line 25 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 record.errors.add(:"#{name}_remote_url", :unreachable) end end original end |