diff --git a/config/locales/en.yml b/config/locales/en.yml index 42cf342..ff0f07d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/lib/muck-engine.rb b/lib/muck-engine.rb index 13c1820..66abae5 100644 --- a/lib/muck-engine.rb +++ b/lib/muck-engine.rb @@ -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' diff --git a/lib/muck-engine/remote_photo_methods.rb b/lib/muck-engine/remote_photo_methods.rb new file mode 100644 index 0000000..6920b58 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/Gemfile.lock b/test/Gemfile.lock index 7311623..ce95d98 100644 --- a/test/Gemfile.lock +++ b/test/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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