<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coffee Powered &#187; attachment_fu</title>
	<atom:link href="http://www.coffeepowered.net/tag/attachment_fu/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coffeepowered.net</link>
	<description>code and content</description>
	<lastBuildDate>Mon, 09 Jan 2012 18:32:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Stupid attachment_fu tricks, part 1</title>
		<link>http://www.coffeepowered.net/2008/09/25/stupid-attachment_fu-tricks-part-1/</link>
		<comments>http://www.coffeepowered.net/2008/09/25/stupid-attachment_fu-tricks-part-1/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 23:16:23 +0000</pubDate>
		<dc:creator>Chris Heald</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[imagemagick]]></category>

		<guid isPermaLink="false">http://www.coffeepowered.net/?p=27</guid>
		<description><![CDATA[attachment_fu is fantastic, but it&#8217;s a bit limited for some purposes. Ever wanted to upload data from a URL instead of making people upload files? It&#8217;s a common problem! Presume that we have a model named Image, which is our target for attachment_fu. Adding URL upload capability is surprisingly simple: There you go. All you [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://svn.techno-weenie.net/projects/plugins/attachment_fu/">attachment_fu</a> is fantastic, but it&#8217;s a bit limited for some purposes. Ever wanted to upload data from a URL instead of making people upload files? It&#8217;s a common problem!</p>
<p>Presume that we have a model named Image, which is our target for attachment_fu. Adding URL upload capability is surprisingly simple:</p>
<pre class="brush: ruby; title: ; notranslate">
class Image &lt; ActiveRecord::Base

	# Standard attachment_fu inclusion here
	has_attachment :storage =&gt; :file_system,
		:content_type =&gt; :image,
		:resize_to =&gt; &quot;1024x1024&gt;&quot;,
		:path_prefix =&gt; &quot;public/images/cache/attached&quot;,
		:format =&gt; &quot;jpg&quot;

	# Allows the direct assignment of a URL to this image, which is the source image to save from
	def url=(v)
		self.uploaded_data = UrlUpload.new(v)
	end

	# Or, we can just pass a URL to Image#uploaded_data
	def uploaded_data=(filedata_or_url)
		if filedata_or_url.is_a? String and filedata_or_url.match /^http(s)?:\/\// then
			file = open(filedata_or_url)
			file.extend(UrlUpload)
			super(file)
		else
			super(filedata_or_url)
		end
	end
end

module UrlUpload
	def filename
		base_uri.to_s.split(&quot;/&quot;).last
	end

	def original_filename
		base_uri.to_s.split(&quot;/&quot;).last
	end
end
</pre>
<p>There you go. All you need now is <code>Image.create(:url => "http://some.url/to/an/image.png")</code> and when the model is saved, the image will be sucked down and saved. Easy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coffeepowered.net/2008/09/25/stupid-attachment_fu-tricks-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

