Skip to content

Commit

Permalink
added remote photos method
Browse files Browse the repository at this point in the history
  • Loading branch information
jbasdf committed Jul 13, 2011
1 parent a52b7de commit 841c34a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/locales/en.yml
Expand Up @@ -21,6 +21,7 @@ en:
admin_welcome: Welcome back %{user}
admin_home_title: Admin Home
select_language_prompt: Please select a language
invalid_photo_url: is invalid or inaccessible
general:
deactivate: Deactivate
no_text: "No"
Expand Down
1 change: 1 addition & 0 deletions lib/muck-engine.rb
Expand Up @@ -10,6 +10,7 @@
require 'muck-engine/models/language'
require 'muck-engine/models/state'
require 'muck-engine/models/general'
require 'muck-engine/remote_photo_methods'
require 'muck-engine/mailers/general'
require 'muck-engine/controllers/application'
require 'muck-engine/controllers/ssl_requirement'
Expand Down
33 changes: 33 additions & 0 deletions lib/muck-engine/remote_photo_methods.rb
@@ -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
12 changes: 6 additions & 6 deletions test/Gemfile.lock
Expand Up @@ -9,7 +9,7 @@ GIT
PATH
remote: ../
specs:
muck-engine (3.3.10)
muck-engine (3.3.11)
overlord
validation_reflection
will_paginate (~> 3.0.pre2)
Expand Down Expand Up @@ -70,13 +70,13 @@ GEM
xpath (~> 0.1.4)
childprocess (0.1.9)
ffi (~> 1.0.6)
columnize (0.3.3)
columnize (0.3.4)
configuration (1.3.1)
crack (0.1.8)
cucumber (1.0.0)
cucumber (1.0.1)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.4.1)
gherkin (~> 2.4.5)
json (>= 1.4.6)
term-ansicolor (>= 1.0.5)
cucumber-rails (1.0.2)
Expand All @@ -92,7 +92,7 @@ GEM
ffi (1.0.9)
friendly_id (3.2.1.1)
babosa (~> 0.3.0)
gherkin (2.4.1)
gherkin (2.4.5)
json (>= 1.4.6)
httparty (0.7.8)
crack (= 0.1.8)
Expand All @@ -113,7 +113,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.16)
mysql (2.8.1)
nokogiri (1.4.6)
nokogiri (1.5.0)
open4 (1.1.0)
overlord (3.0.2)
httparty
Expand Down

0 comments on commit 841c34a

Please sign in to comment.