Commit 0919884f by jyurek

Tweaks to URI handling

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@196 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 439879e3
...@@ -164,8 +164,8 @@ module Thoughtbot #:nodoc: ...@@ -164,8 +164,8 @@ module Thoughtbot #:nodoc:
attachments[attr] = (attachments[attr] || {:name => attr}).merge(options) attachments[attr] = (attachments[attr] || {:name => attr}).merge(options)
define_method "#{attr}=" do |uploaded_file| define_method "#{attr}=" do |uploaded_file|
return send("destroy_#{attr}") if uploaded_file.nil?
uploaded_file = fetch_uri(uploaded_file) if uploaded_file.is_a? URI uploaded_file = fetch_uri(uploaded_file) if uploaded_file.is_a? URI
return send("destroy_#{attr}") if uploaded_file.nil?
return unless is_a_file? uploaded_file return unless is_a_file? uploaded_file
attachments[attr].merge!({ attachments[attr].merge!({
...@@ -247,8 +247,6 @@ module Thoughtbot #:nodoc: ...@@ -247,8 +247,6 @@ module Thoughtbot #:nodoc:
write_attachment attachments[attr] if attachments[attr][:files] write_attachment attachments[attr] if attachments[attr][:files]
delete_attachment attachments[attr], attachments[attr][:complain_on_delete] if attachments[attr][:delete_on_save] delete_attachment attachments[attr], attachments[attr][:complain_on_delete] if attachments[attr][:delete_on_save]
attachments[attr][:delete_on_save] = false attachments[attr][:delete_on_save] = false
attachments[attr][:dirty] = false
attachments[attr][:files] = nil
end end
end end
private :"#{attr}_before_save" private :"#{attr}_before_save"
...@@ -285,7 +283,11 @@ module Thoughtbot #:nodoc: ...@@ -285,7 +283,11 @@ module Thoughtbot #:nodoc:
s.gsub!(/:class/, self.class.to_s.underscore.pluralize) s.gsub!(/:class/, self.class.to_s.underscore.pluralize)
s.gsub!(/:style/, style.to_s) s.gsub!(/:style/, style.to_s)
s.gsub!(/:attachment/, attachment[:name].to_s.pluralize) s.gsub!(/:attachment/, attachment[:name].to_s.pluralize)
s.gsub!(/:name/, file_name) if file_name if file_name
s.gsub!(/:name/, file_name)
s.gsub!(/:base/, file_name.split(".")[0..-2].join(".") )
s.gsub!(/:ext/, file_name.split(".").last )
end
end end
end end
...@@ -322,6 +324,8 @@ module Thoughtbot #:nodoc: ...@@ -322,6 +324,8 @@ module Thoughtbot #:nodoc:
file.write(atch.read) file.write(atch.read)
end end
end end
attachment[:files] = nil
attachment[:dirty] = false
end end
def delete_attachment attachment, complain = false def delete_attachment attachment, complain = false
...@@ -393,12 +397,12 @@ module Thoughtbot #:nodoc: ...@@ -393,12 +397,12 @@ module Thoughtbot #:nodoc:
end end
def fetch_uri uri def fetch_uri uri
require 'open-uri'
# I hate the fact that URI and open-uri can't handle file:// urls. # I hate the fact that URI and open-uri can't handle file:// urls.
if uri.scheme == 'file' if uri.scheme == 'file'
path = url.gsub(%r{^file://}, '/') path = url.gsub(%r{^file://}, '/')
image_data = open(path) image_data = open(path)
else else
require 'open-uri'
image_data = open(uri.to_s) image_data = open(uri.to_s)
end end
...@@ -410,6 +414,9 @@ module Thoughtbot #:nodoc: ...@@ -410,6 +414,9 @@ module Thoughtbot #:nodoc:
image.original_filename = File.basename(uri.path) image.original_filename = File.basename(uri.path)
image image
rescue Exception => e
self.errors.add_to_base("Could not save #{uri.to_s}: #{e}")
nil
end end
def is_a_file? data def is_a_file? data
......
# desc "Explaining what the task does"
# task :paperclip do
# # Task goes here
# end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment