about summary refs log tree commit diff
path: root/app/models/concerns/remotable.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/concerns/remotable.rb')
-rw-r--r--app/models/concerns/remotable.rb23
1 files changed, 19 insertions, 4 deletions
diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb
index 4a412ee3d..b4f169649 100644
--- a/app/models/concerns/remotable.rb
+++ b/app/models/concerns/remotable.rb
@@ -6,11 +6,16 @@ module Remotable
 
   included do
     attachment_definitions.each_key do |attachment_name|
-      attribute_name = "#{attachment_name}_remote_url".to_sym
-      method_name = "#{attribute_name}=".to_sym
+      attribute_name  = "#{attachment_name}_remote_url".to_sym
+      method_name     = "#{attribute_name}=".to_sym
+      alt_method_name = "reset_#{attachment_name}!".to_sym
 
       define_method method_name do |url|
-        parsed_url = Addressable::URI.parse(url).normalize
+        begin
+          parsed_url = Addressable::URI.parse(url).normalize
+        rescue Addressable::URI::InvalidURIError
+          return
+        end
 
         return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[attribute_name] == url
 
@@ -26,10 +31,20 @@ module Remotable
           send("#{attachment_name}_file_name=", filename)
 
           self[attribute_name] = url if has_attribute?(attribute_name)
-        rescue HTTP::TimeoutError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError => e
+        rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError => e
           Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}"
+          nil
         end
       end
+
+      define_method alt_method_name do
+        url = self[attribute_name]
+
+        return if url.blank?
+
+        self[attribute_name] = ''
+        send(method_name, url)
+      end
     end
   end
 end