<?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>Transcending Frontiers &#187; Testing File Uploads with Webrat and Paperclip</title>
	<atom:link href="http://blog.thefrontiergroup.com.au/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thefrontiergroup.com.au</link>
	<description>Your peek inside the collective mind of The Frontier Group</description>
	<lastBuildDate>Tue, 03 Jan 2012 06:53:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Testing File Uploads with Webrat and Paperclip</title>
		<link>http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/</link>
		<comments>http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 03:13:23 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Paperclip]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Webrat]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=389</guid>
		<description><![CDATA[&#8211; Check out some of our more recent Ruby on Rails blog posts. If you&#8217;d like to hire our team, get in touch &#8211; I wanted to integrate some branding functionality into an application we&#8217;re developing and so I needed test file upload functionality. We&#8217;re using Webrat for integration tests, though this will likely change as [...]]]></description>
			<content:encoded><![CDATA[<p><em style="font-style: italic;">&#8211; Check out some of our more recent </em><a title="Ruby on Rails posts" href="http://thefrontiergroup.com.au/blog/category/ruby-on-rails/"><em style="font-style: italic;">Ruby on Rails blog posts</em></a><em style="font-style: italic;">. If you&#8217;d like to hire our team, <a title="Get in touch" href="http://thefrontiergroup.com.au/pages/contact-us">get in touch</a></em><em style="font-style: italic;"> &#8211;</em></p>
<p>I wanted to integrate some branding functionality into an application we&#8217;re developing and so I needed test file upload functionality. We&#8217;re using Webrat for integration tests, though this will likely change as we increase the amount of Javascript in the app. I added Paperclip to handle the file attachments for logos, and everything was working.</p>
<p>When I added validation to the model, making sure that the file being attached was an image, this broke the tests. It didn&#8217;t seem to matter what type the file was, it would fail no matter what on the file type validation.</p>
<p>I used ruby-debug to debug my test and it seems by default Webrat sends file uploads as plain text. It does have the option to specify the file type when attaching the file, so the easiest way around this is just to specify the MIME type for the file. Now my Cucumber step looks something like this :</p>
<pre class="ruby">When /^I attach "([^\"]*)" image to the "([^\"]*)" file field$/ do |filename, field|
  type = filename.split(".")[1]

  if type == "jpg"
    type = "image/jpeg"
  end

  attach_file field, File.join(RAILS_ROOT, test_asset_path, filename), type
end</pre>
<p>Obviously this will need some work as I progress, but it works. At this stage I have an assets folder in my features folder to store any files that I need for my tests.</p>
<p>On the confirmation end of the test I just have a simple tag test to check that the image tag is displaying, and it contains the correct src attribute :</p>
<pre class="ruby">Then /^I should see tag "(.+)"$/ do |selector|
  (Hpricot(response.body)/selector).should_not be_empty
end</pre>
<p>So in my feature test I have :</p>
<pre class="ruby">Then I should see tag "img[@src*='']"</pre>
<p>This just confirms that there is an image tag that contains the file name of the file that I uploaded in the test.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery: Check For Non-Empty Input Fields</title>
		<link>http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 08:46:07 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=176</guid>
		<description><![CDATA[&#8211; Check out some of our more recent Ruby on Rails blog posts. If you&#8217;d like to hire our team, get in touch &#8211; I was writing some code today and I wanted to prompt the user to check if they wanted to delete a row of data only if there was some data they [...]]]></description>
			<content:encoded><![CDATA[<p><em>&#8211; Check out some of our more recent </em><a title="Ruby on Rails posts" href="http://thefrontiergroup.com.au/blog/category/ruby-on-rails/"><em>Ruby on Rails blog posts</em></a><em>. If you&#8217;d like to hire our team, <a title="Get in touch" href="http://thefrontiergroup.com.au/pages/contact-us">get in touch</a></em><em> &#8211;</em></p>
<p>I was writing some code today and I wanted to prompt the user to check if they wanted to delete a row of data only if there was some data they might not want deleted. Typically to do this I would loop through all the input fields and if any of them weren&#8217;t blank I would run the check. I figured that there must be a better way with callbacks or selectors, and after a little thinking I wrote this jQuery snippet :</p>
<pre class="javascript">row.find('input[value!=""]').length &gt; 0</pre>
<p>This basically says count the number of inputs with a non-blank value you find, inside the given row.</p>
<p>Just like <a href="http://thefrontiergroup.com.au/blog/2008/11/dynamically-reducing-object-sizes-in-php/">my last post</a>, it&#8217;s nothing amazing but it sure is a better way of doing things. If you look into the <code>andSelf()</code> method then you could also easily chain selects and other input types.</p>
<p>I&#8217;m also interested if someone has a better way to approach this problem?</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

