Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
41 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Adapted from http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/ | ||
module MuckEngine | ||
module RemotePhotoMethods | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_validation :download_remote_photo, :if => :photo_url_provided? | ||
validates_presence_of :photo_remote_url, :if => :photo_url_provided?, :message => I18n.translate('muck.engine.invalid_photo_url') | ||
end | ||
|
||
attr_accessor :photo_url | ||
|
||
private | ||
|
||
def photo_url_provided? | ||
!self.photo_url.blank? | ||
end | ||
|
||
def download_remote_photo | ||
self.photo = do_download_remote_photo | ||
self.photo_remote_url = photo_url | ||
end | ||
|
||
def do_download_remote_photo | ||
io = open(URI.parse(photo_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters