<?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>Pennysmalls &#187; rails</title>
	<atom:link href="http://pennysmalls.com/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://pennysmalls.com</link>
	<description>A blog about Ruby, Rails and other tech.  Mostly.</description>
	<lastBuildDate>Thu, 04 Mar 2010 07:24:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Much faster Rails tests if you use MyISAM</title>
		<link>http://pennysmalls.com/2009/11/05/much-faster-rails-tests-if-you-use-myisam/</link>
		<comments>http://pennysmalls.com/2009/11/05/much-faster-rails-tests-if-you-use-myisam/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 10:14:33 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=125</guid>
		<description><![CDATA[Here&#8217;s a little insight I had the other day.
If you use MyISAM tables, and there are various performance related reasons you might, then you&#8217;ve been stuck having to turn transactional fixtures off in your tests.
But unless you are using some special feature of MyISAM that is not present in InnoDB, then why not use InnoDB [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little insight I had the other day.</p>
<p>If you use MyISAM tables, and there are various performance related reasons you might, then you&#8217;ve been stuck having to turn transactional fixtures off in your tests.</p>
<p>But unless you are using some special feature of MyISAM that is not present in InnoDB, then why not use InnoDB tables in your test database?</p>
<p>I wrote a simple plugin that has a rake task that clones the development database to the test while changing the ENGINE to InnoDB.</p>
<p>You just run the task, turn transational fixtures on in test/test_helper.rb, and you&#8217;re off.</p>
<p>Result: our tests used to take 8.5 minutes to run.  Now they take just 3 minutes.  That&#8217;s about <strong>65% less time</strong> than it used to take.</p>
<p>The plugin is here: <a href="http://github.com/sdsykes/fast_fixture">http://github.com/sdsykes/fast_fixture</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/11/05/much-faster-rails-tests-if-you-use-myisam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FastImageInline &#8211; inline images in your html code</title>
		<link>http://pennysmalls.com/2009/09/23/fastimageinline-inline-images-in-your-html-code/</link>
		<comments>http://pennysmalls.com/2009/09/23/fastimageinline-inline-images-in-your-html-code/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 21:19:16 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=102</guid>
		<description><![CDATA[You may not know this, and I didn&#8217;t until recently, but you can place images directly in your img tags.  Not just the address of them, but the whole binary data &#8211; base 64 encoded.
The technique is based on the Data URI scheme.
Take a look at the source of Google News.  Scroll down [...]]]></description>
			<content:encoded><![CDATA[<p>You may not know this, and I didn&#8217;t until recently, but you can place images directly in your img tags.  Not just the address of them, but the whole binary data &#8211; base 64 encoded.</p>
<p>The technique is based on the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">Data URI scheme</a>.</p>
<p>Take a look at the source of <a href="http://news.google.com/">Google News</a>.  Scroll down a bit and you should start to see image tags that contain base64 encoded data (well you will if you are on IE8 or another make of browser &#8211; not IE7).  That&#8217;s what I&#8217;m talking about.</p>
<p>The technique is particularly suitable if you have have small images that change often and you do not need the browser to cache them.  In this case, the saved http connection and fetch that inlining the images affords you is a performance win.</p>
<p>In case you find it useful I have extended my <a href="http://github.com/sdsykes/fastimage">FastImage</a> <a href="http://github.com/sdsykes/fastimage_resize">series</a> with <a href="http://github.com/sdsykes/fastimage_inline">FastImage Inline</a>, which is a <strong>gem</strong> and <strong>rails plugin</strong> that will take care of inlining your images.</p>
<p>It&#8217;s simple to use &#8211; just like image_tag there is now a helper method inline_image_tag (and inline_image_path for image_path).</p>
<p>&nbsp;<br />
<strong>Example</strong></p>
<pre>
  inline_image_tag("bullet.gif")
</pre>
<p>Result for request from a data-uri capable browser:</p>
<pre>
  &lt;img alt="Bullet" src="data:image/gif;base64,R0lGODlhCAANAJECAAAAAP///////wA
  AACH5BAEAAAIALAAAAAAIAA0AAAITlI+pyxgPI5gAUvruzJpfi0ViAQA7" />
</pre>
<p>Result for a non-capable browser (eg IE7 or below):</p>
<pre>
  &lt;img alt="Bullet" src="/images/bullet.gif?1206090639" />
</pre>
<p>&nbsp;<br />
<strong>Installation</strong></p>
<p>Note that the FastImage gem must be installed first, check the requirements section below.</p>
<p>&nbsp;<br />
<strong>As a Rails plugin</strong></p>
<pre>
  ./script/plugin install git://github.com/sdsykes/fastimage_inline.git
</pre>
<p>&nbsp;<br />
<strong>As a Gem</strong></p>
<pre>
  sudo gem install sdsykes-fastimage_inline -s http://gems.github.com
</pre>
<p>Install the gem as above, and configure it in your environment.rb file as below:</p>
<pre>
  ...
  Rails::Initializer.run do |config|
    ...
    config.gem "sdsykes-fastimage_inline", :lib=>"fastimage_inline"
    ...
  end
  ...
</pre>
<p>&nbsp;<br />
<strong>Requirements</strong></p>
<p>* FastImage <a href="http://github.com/sdsykes/fastimage">http://github.com/sdsykes/fastimage</a></p>
<p>&nbsp;<br />
<strong>Browser support</strong></p>
<p>All modern browsers support this technique except for IE versions 7 and below.  This is still a major segment of the market of course, but as IE users migrate to IE 8 this will become less of a problem.  </p>
<p>FastImage Inline uses a simple browser detection mechanism by looking at the user agent string.  If the browser is known to not have support, or if we do not recognise it at all, we serve a normal image tag which includes the path to the image file in the src attribute.  But if we know the browser can handle it, we send the image inline, and the browser won&#8217;t need to fetch it separately.</p>
<p>&nbsp;<br />
<strong>Limits</strong></p>
<p>Reportedly IE8 will not handle data strings longer than 32k bytes. But it is probably unwise to inline images this big anyway.  Google news serves images that are up to about 3.5k in length, and this seems a reasonable approach.  However, FastImage Inline does not enforce any particular constraints, it is for you to decide.</p>
<p>FastImage Inline does not cache the images it has read &#8211; so every time an image is sent it will be read from disk.  This feature may be added in a later release.</p>
<p>&nbsp;<br />
<strong>Conclusion</strong></p>
<p>Inlining images is not for everyone, but it&#8217;s a useful technique in your toolbox for optimising delivery of certain kinds of pages or content.  For more information check the comprehensive list of <a href="http://en.wikipedia.org/wiki/Data_URI_scheme#Advantages">advantages and disadvantages</a> on the Data URI scheme wikipedia page.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/09/23/fastimageinline-inline-images-in-your-html-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recent code &#8211; FastImage resize, Scrooge and Read From Slave</title>
		<link>http://pennysmalls.com/2009/07/14/recent-code-fastimage-resize-scrooge-and-read-from-slave/</link>
		<comments>http://pennysmalls.com/2009/07/14/recent-code-fastimage-resize-scrooge-and-read-from-slave/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 14:57:09 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=93</guid>
		<description><![CDATA[A roundup of some of my projects that may be of interest:
1. FastImage Resize
This builds on my work on FastImage to provide an image resize facility.  The resize code calls libgd to do the work of resampling and resizing the image &#8211; this is a library that is very likely to be already installed on [...]]]></description>
			<content:encoded><![CDATA[<p>A roundup of some of my projects that may be of interest:</p>
<p>1. <a href="http://github.com/sdsykes/fastimage_resize/tree/master">FastImage Resize</a></p>
<p>This builds on my work on <a href="http://pennysmalls.com/2009/06/11/fastimage-finds-image-dimensions-fast-using-minimal-resources/">FastImage</a> to provide an image resize facility.  The resize code calls <a href="http://libgd.org/">libgd </a>to do the work of resampling and resizing the image &#8211; this is a library that is very likely to be already installed on your system if it is some flavour of unix / linux or even OSX.  And if not, it is very easy to install.  This is a light and simple option if you don&#8217;t wish to install heavier libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).</p>
<p>2. <a href="http://github.com/methodmissing/scrooge/tree/master">Scrooge</a></p>
<p>This is a plugin and gem to optimise queries to the database based on a learning algorithm that looks at how the results of each query are used.  I worked on this with Lourens Naudé earlier this year, and I will shortly make a minor release with a few further optimisations and tests.  Try this if your database is slowing you down, but also see <a href="http://slim-attributes.rubyforge.org/">slim-attributes</a> if you are using MySQL.</p>
<p>3. <a href="http://github.com/sdsykes/read_from_slave/tree/master">Read From Slave</a></p>
<p>A gem to force your database reads to a slave database while your writes go to the master.  It&#8217;s fast and simple, it works a treat, and we have it in production use.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/07/14/recent-code-fastimage-resize-scrooge-and-read-from-slave/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FastImage finds image dimensions fast using minimal resources</title>
		<link>http://pennysmalls.com/2009/06/11/fastimage-finds-image-dimensions-fast-using-minimal-resources/</link>
		<comments>http://pennysmalls.com/2009/06/11/fastimage-finds-image-dimensions-fast-using-minimal-resources/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 11:37:36 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=86</guid>
		<description><![CDATA[I just released a gem to find image dimensions and type information fast.  I have previously done some work in this area, but this is a much more comprehensive solution, and fixes problems with certain jpegs.
FastImage finds the size or type of an image given its uri by fetching as little as needed
The problem
Your [...]]]></description>
			<content:encoded><![CDATA[<p>I just released a gem to find image dimensions and type information fast.  I have previously done some <a href="http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/">work in this area</a>, but this is a much more comprehensive solution, and fixes problems with certain jpegs.</p>
<p><a href="http://github.com/sdsykes/fastimage/tree/master">FastImage</a> finds the size or type of an image given its uri by fetching as little as needed</p>
<p><strong>The problem</strong></p>
<p>Your app needs to find the size or type of an image. This could be for adding width and height attributes to an image tag, for adjusting layouts or overlays to fit an image or any other of dozens of reasons.</p>
<p>But the image is not locally stored – it’s on another asset server, or in the cloud – at Amazon S3 for example.</p>
<p>You don’t want to download the entire image to your app server – it could be many tens of kilobytes, or even megabytes just to get this information. For most image types, the size of the image is simply stored at the start of the file. For JPEG files it’s a little bit more complex, but even so you do not need to fetch most of the image to find the size.</p>
<p><a href="http://github.com/sdsykes/fastimage/tree/master">FastImage</a> does this minimal fetch for image types GIF, JPEG, PNG and BMP. And it doesn’t rely on installing external libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).</p>
<p>You only need supply the uri, and FastImage will do the rest.</p>
<p><strong>Examples</strong></p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><span class="kw3">require</span> <span class="st0">&#8216;fastimage&#8217;</span></p>
<p>FastImage.<span class="me1">size</span><span class="br0">&#40;</span><span class="st0">&quot;http://stephensykes.com/images/ss.com_x.gif&quot;</span><span class="br0">&#41;</span><br />
<span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="nu0">266</span>, <span class="nu0">56</span><span class="br0">&#93;</span> &nbsp;<span class="co1"># width, height</span><br />
FastImage.<span class="me1">type</span><span class="br0">&#40;</span><span class="st0">&quot;http://stephensykes.com/images/pngimage&quot;</span><span class="br0">&#41;</span><br />
<span class="sy0">=&gt;</span> <span class="re3">:png</span></div>
<p><strong>Installation</strong></p>
<p><strong>Gem</strong></p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;">
sudo gem install sdsykes-fastimage -s http://gems.github.com
</div>
<p><strong>Rails</strong></p>
<p>Install the gem as above, and configure it in your environment.rb file as below:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;">&#8230;<br />
<span class="re2">Rails::Initializer</span>.<span class="me1">run</span> <span class="kw1">do</span> <span class="sy0">|</span>config<span class="sy0">|</span><br />
&#8230;<br />
<span class="me1">config</span>.<span class="me1">gem</span> <span class="st0">&quot;sdsykes-fastimage&quot;</span>, <span class="re3">:lib</span><span class="sy0">=&gt;</span><span class="st0">&quot;fastimage&quot;</span><br />
&#8230;<br />
<span class="kw1">end</span><br />
&#8230;</div>
<p>Then you’re off – just use FastImage.size() and FastImage.type() in your code as in the examples.</p>
<p><strong>Documentation</strong></p>
<p><a href="http://rdoc.info/projects/sdsykes/fastimage">http://rdoc.info/projects/sdsykes/fastimage</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/06/11/fastimage-finds-image-dimensions-fast-using-minimal-resources/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Ferret on Ruby 1.9.1</title>
		<link>http://pennysmalls.com/2009/03/24/ferret-on-ruby-191/</link>
		<comments>http://pennysmalls.com/2009/03/24/ferret-on-ruby-191/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 09:47:39 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=62</guid>
		<description><![CDATA[I took the trouble to port ferret to ruby 1.9.1 yesterday evening.  I have it working on my mac.
Here&#8217;s a gem for you to try &#8211; I have labelled it 0.11.6.19.  If you use it let me know how it runs, but it&#8217;s at your own risk, I haven&#8217;t extensively tested it.
[UPDATE: this gem has [...]]]></description>
			<content:encoded><![CDATA[<p>I took the trouble to port <a href="http://ferret.davebalmain.com/">ferret</a> to ruby 1.9.1 yesterday evening.  I have it working on my mac.</p>
<p><a href="http://pennysmalls.com/gems/ferret-0.11.6.19.gem">Here&#8217;s a gem</a> for you to try &#8211; I have labelled it 0.11.6.19.  If you use it let me know how it runs, but it&#8217;s at your own risk, I haven&#8217;t extensively tested it.</p>
<p>[UPDATE: this gem has been updated 5th April 2009 - please test.  There is also a <a href="http://github.com/sdsykes/ferret/tree/master">fork at github</a>]</p>
<p>I&#8217;ve made mostly simple changes in the code:</p>
<ul>
<li>Changed all struct RString -&gt; ptr to use the RSTRING_PTR macro, except for cases where it was being used to add items to an array where rb_ary_store was used.</li>
<li>Changed all struct RString -&gt; len to use the RSTRING_LEN macro</li>
<li>Changed all struct RArray -&gt; ptr to use the RARRAY_PTR macro</li>
<li>Changed all struct RArray -&gt; len to use the RARRAY_LEN macro</li>
<li>Removed manual adjustment of the len member of RArray.  In fact ruby 1.9 stores small arrays of 3 items or less differently from larger ones, and this adds complexity.  It is better to use the rb_ary_store method which will use the correct pointer and will keep the length in sync with the number of items in the array.</li>
<li>Changed all struct RHash -&gt; tbl to ntbl</li>
<li>Removed references to rb_thread_critical</li>
<li>Removed 4th argument from calls to rb_cvar_set</li>
<li>Included ruby/re.h and not regex.h, and altered tokenizer code to correctly use the new regexp library</li>
<li>Included ruby/st.h and not st.h</li>
<li>Some other minor changes to error messages formats causing compiler warnings</li>
</ul>
<p>By the way, acts_as_ferret also runs with some very minor surgery, Thomas von Deyen <a href="http://github.com/tvdeyen/acts_as_ferret/tree/master">has a fork here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/03/24/ferret-on-ruby-191/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Rails 2.3 breakage and fixage</title>
		<link>http://pennysmalls.com/2009/03/04/rails-23-breakage-and-fixage/</link>
		<comments>http://pennysmalls.com/2009/03/04/rails-23-breakage-and-fixage/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 14:49:53 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=45</guid>
		<description><![CDATA[Rails 2.3 will be with us soon, so I took the time to update our app to be compatible.  It&#8217;s a reasonably large app (26,000 LOC), so there&#8217;s bound to be some issues.
The first thing to notice is that the PStore store for sessions has completely gone away.  This means that saying something [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.3 will be with us soon, so I took the time to update our app to be compatible.  It&#8217;s a reasonably large app (26,000 LOC), so there&#8217;s bound to be some issues.</p>
<p>The first thing to notice is that the PStore store for sessions has completely gone away.  This means that saying something like <strong>config.action_controller.session_store = :p_store</strong> in your environment file will no longer work.</p>
<p>We don&#8217;t use the cookie store because our sessions can get bigger than the 4k limit in certain circumstances.  So we use the memcache store on the production machines, and pstore on the dev and test machines.  And we can&#8217;t do that any more, which is a shame as it worked well &#8211; particularly with <a href="http://izumi.plan99.net/blog/index.php/2008/03/26/making-pstore-reaaaally-fast/">Hongli Lai&#8217;s improvements</a>.</p>
<p>The remaining options are DRb, Memcached, or SQL.  We didn&#8217;t want to add complexity to our environments, so none of those looked attractive.  So we ended up rewriting some of our code so that the cookie store would be usable in most cases.  We&#8217;ll keep memcache as the store on the production systems though.</p>
<p>Talking of memcache, it seems we now need to include <strong>require &#8216;memcache&#8217;</strong> in our production.rb file.  It&#8217;s not automatically loaded before we want to configure it.</p>
<p>The rest of the problems weren&#8217;t with the app itself, but with the incredible amount of failed and erroring tests due to changes in the Rails testing system.</p>
<p>Firstly all the unit tests were not even running because they all inherited from <strong>Test::Unit::TestCase</strong>.  Nowerdays they need to inherit from <strong>ActiveSupport::TestCase</strong>, and this is necessary in Rails 2.3.</p>
<p>Also make sure your test_helper.rb opens the right class:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;">ENV<span class="br0">&#91;</span><span class="st0">&quot;RAILS_ENV&quot;</span><span class="br0">&#93;</span> = <span class="st0">&quot;test&quot;</span><br />
<span class="kw3">require</span> <span class="kw4">File</span>.<span class="me1">expand_path</span><span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/../config/environment&quot;</span><span class="br0">&#41;</span><br />
<span class="kw3">require</span> <span class="st0">&#8216;test_help&#8217;</span></p>
<p><span class="kw1">class</span> <span class="re2">ActiveSupport::TestCase</span><br />
&#8230;</div>
<p>Next, if you were using <strong>assert_valid model_item</strong> you must change this to <strong>assert model_item.valid?</strong>, <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1470-assert_valid-undefined-in-edge-activesupporttestcase">see here</a>.  No deprecation warning in 2.2 that it would be removed, but never mind, the fix is quite easy.</p>
<p>We have tests for our routing.  They live in the unit tests &#8211; it&#8217;s handy to test all the routes in one place.  But in 2.3 the assert_routing method has disappeared.  In fact it&#8217;s just not automatically available in unit tests any more, you can retrieve it by doing this in your test class:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><span class="kw1">include</span> <span class="re2">ActionController::Assertions::RoutingAssertions</span></div>
<p>But the routing assertion also needs clean_backtrace which seems to be part of the ActionController::TestCase.  We opted to just define it in test_helper.rb (for a quick fix, just add this code):</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;">&nbsp; <span class="kw1">def</span> clean_backtrace<span class="br0">&#40;</span><span class="sy0">&amp;</span>block<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">yield</span><br />
&nbsp; <span class="kw1">rescue</span> <span class="re2">ActiveSupport::TestCase::Assertion</span> <span class="sy0">=&gt;</span> error<br />
&nbsp; &nbsp; framework_path = <span class="kw4">Regexp</span>.<span class="me1">new</span><span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">expand_path</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;#{File.dirname(__FILE__)}/assertions&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; error.<span class="me1">backtrace</span>.<span class="me1">reject</span>! <span class="br0">&#123;</span><span class="sy0">|</span>line<span class="sy0">|</span> <span class="kw4">File</span>.<span class="me1">expand_path</span><span class="br0">&#40;</span>line<span class="br0">&#41;</span> =~ framework_path <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw3">raise</span><br />
&nbsp; <span class="kw1">end</span></div>
<p>We also test cookies in functional tests, and the <a href="http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Example/FunctionalExampleGroup.html">usage has changed in 2.3</a>.  So you&#8217;ll need to check through those.</p>
<p>If you send multipart emails and have file fixtures (of the expected email contents) to test them, we noticed that instead of just saying <strong>Content-Type: text/plain</strong> in the header before the mime encoded parts, we now get <strong>Content-Type: text/plain; charset=iso-8859-1</strong>.  Those need to be edited.</p>
<p>Finally, if you are using <strong>assert_select_email</strong> in your tests for your mailer classes, you will find it is also no longer available.  The fast solution is to put <strong>include ActionController::Assertions::SelectorAssertions</strong> in your mailer test class.</p>
<p>We have worked around some of the issues presented to us with minimul changes to our code.  It seems like Rails is encouraging us to organise our tests differently, particularly where functionality in ActionController::Assertions is no longer automatically available to unit tests.  Working around this feels somewhat unclean, so we&#8217;ll take a look again whether tests should be moved or rewritten once the dust has settled on 2.3.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/03/04/rails-23-breakage-and-fixage/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using acts as ferret with phusion passenger / mod_rails</title>
		<link>http://pennysmalls.com/2009/03/02/using-acts-as-ferret-with-phusion-passenger-mod_rails/</link>
		<comments>http://pennysmalls.com/2009/03/02/using-acts-as-ferret-with-phusion-passenger-mod_rails/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 20:59:31 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=42</guid>
		<description><![CDATA[The passenger manual makes it clear that you need to close and reestablish your connections to things like memcached after it forks to avoid inadventently sharing file handles.  The reason is well and clearly explained there.
The api to do this is simple &#8211; just place this kind of code in your environment.rb file:
if defined?&#40;PhusionPassenger&#41;
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.modrails.com/documentation/Users%20guide%20latest.html#_example_1_memcached_connection_sharing_harmful">passenger manual</a> makes it clear that you need to close and reestablish your connections to things like memcached after it forks to avoid inadventently sharing file handles.  The reason is well and clearly explained there.</p>
<p>The api to do this is simple &#8211; just place this kind of code in your environment.rb file:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><span class="kw1">if</span> <span class="kw1">defined</span>?<span class="br0">&#40;</span>PhusionPassenger<span class="br0">&#41;</span><br />
&nbsp; PhusionPassenger.<span class="me1">on_event</span><span class="br0">&#40;</span><span class="re3">:starting_worker_process</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>forked<span class="sy0">|</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> forked<br />
&nbsp; &nbsp; &nbsp; <span class="co1"># We&#8217;re in smart spawning mode.</span><br />
&nbsp; &nbsp; &nbsp; <span class="sy0">&#8211;</span> reestablish connections <span class="sy0">&#8211;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1"># We&#8217;re in conservative spawning mode. We don&#8217;t need to do anything.</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; <span class="kw1">end</span><br />
<span class="kw1">end</span></div>
<p>All well and good, but how exactly do you reestablish those connections?</p>
<p>In our case we have to deal with memcached and ferret (with ferret running in a DRb server via the <a href="http://rm.jkraemer.net/projects/show/aaf">acts_as_ferret</a> plugin).</p>
<p>Memcached is dead easy:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;">CACHE.<span class="me1">reset</span></div>
<p>Ferret not so easy.  It turns out that DRb has no in-built way to close its pool of connections.  So a monkey patch is the only thing to do.  I was inspired by <a href="http://rucila.s43.xrea.com/memo/?date=20060803">some code you can find here</a>.  But since we want to blindly close all the connections, our case is simpler:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;">&nbsp; <span class="kw1">class</span> <span class="re2">DRb::DRbConn</span><br />
&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">close_all</span><br />
&nbsp; &nbsp; &nbsp; <span class="re1">@mutex</span>.<span class="me1">synchronize</span> <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@pool</span>.<span class="me1">each</span> <span class="br0">&#123;</span><span class="sy0">|</span>c<span class="sy0">|</span> c.<span class="me1">close</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@pool</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; <span class="kw1">end</span></p>
<p>&nbsp; <span class="re2">DRb::DRbConn</span>.<span class="me1">close_all</span></div>
<p>DRb will happily reconnect by itself when needed after its connection pool has been emptied.</p>
<p>Putting it all together, it looks like this:</p>
<div class="ruby" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><span class="kw1">if</span> <span class="kw1">defined</span>?<span class="br0">&#40;</span>PhusionPassenger<span class="br0">&#41;</span><br />
&nbsp; <span class="co1"># monkey patch drb so we can close its connections</span><br />
&nbsp; <span class="kw1">class</span> <span class="re2">DRb::DRbConn</span><br />
&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">close_all</span><br />
&nbsp; &nbsp; &nbsp; <span class="re1">@mutex</span>.<span class="me1">synchronize</span> <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@pool</span>.<span class="me1">each</span> <span class="br0">&#123;</span><span class="sy0">|</span>c<span class="sy0">|</span> c.<span class="me1">close</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@pool</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; <span class="kw1">end</span></p>
<p>&nbsp; PhusionPassenger.<span class="me1">on_event</span><span class="br0">&#40;</span><span class="re3">:starting_worker_process</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>forked<span class="sy0">|</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> forked<br />
&nbsp; &nbsp; &nbsp; <span class="co1"># We&#8217;re in smart spawning mode.</span><br />
&nbsp; &nbsp; &nbsp; CACHE.<span class="me1">reset</span> &nbsp;<span class="co1"># memcached</span><br />
&nbsp; &nbsp; &nbsp; <span class="re2">DRb::DRbConn</span>.<span class="me1">close_all</span> &nbsp;<span class="co1"># ferret</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1"># We&#8217;re in conservative spawning mode. We don&#8217;t need to do anything.</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; <span class="kw1">end</span><br />
<span class="kw1">end</span></div>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2009/03/02/using-acts-as-ferret-with-phusion-passenger-mod_rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Breakage and fixage in Rails 2.2</title>
		<link>http://pennysmalls.com/2008/11/29/breakage-and-fixage-in-rails-22/</link>
		<comments>http://pennysmalls.com/2008/11/29/breakage-and-fixage-in-rails-22/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 16:54:28 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=35</guid>
		<description><![CDATA[Finally our app is completely Rails 2.2 ready.
Some quick notes on some issues and things that needed to be fixed:
1. Default error messages
This call is no good any more

ActiveRecord::Errors.default_error_messages

Use this instead:

I18n.translate('activerecord.errors.messages')

2. Use ActiveSupport::Inflector rather than Inflector
The warning tells you all you need to know:

DEPRECATION WARNING: Inflector is deprecated!
Use ActiveSupport::Inflector instead.

3. Integration tests are broken if [...]]]></description>
			<content:encoded><![CDATA[<p>Finally our app is completely Rails 2.2 ready.</p>
<p>Some quick notes on some issues and things that needed to be fixed:</p>
<p><strong>1. Default error messages</strong></p>
<p>This call is no good any more</p>
<pre>
ActiveRecord::Errors.default_error_messages
</pre>
<p>Use this instead:</p>
<pre>
I18n.translate('activerecord.errors.messages')
</pre>
<p><strong>2. Use ActiveSupport::Inflector rather than Inflector</strong></p>
<p>The warning tells you all you need to know:</p>
<pre>
DEPRECATION WARNING: Inflector is deprecated!
Use ActiveSupport::Inflector instead.
</pre>
<p><strong>3. Integration tests are broken if you are not using the cookie store</strong></p>
<p>See <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1453-gets-in-integration-test-unless-you-are-using-cookie-sessions">here for details</a>.  If you are seeing &#8220;NoMethodError: You have a nil object when you didn&#8217;t expect it!&#8221; inexplicably from your integration tests, then this could be the issue.</p>
<p>I ended up placing this in environments/test.rb, even though it should not be needed:</p>
<pre>
config.action_controller.session = { :session_key =&gt; "_myapp_session",
  :secret =&gt; "some secret phrase of at least 30 characters" }
</pre>
<p><strong>4. Use of string keys in assert_redirected_to in tests no longer works</strong></p>
<p>Consider this code:</p>
<pre>
assert_redirected_to "host"=&gt;"foobar.com",
  "action"=&gt;"something", "controller"=&gt;"hw"
</pre>
<p>It used to work, but in rails 2.2 it does not.  You need to use symbols for the keys, like this:</p>
<pre>
assert_redirected_to :host=&gt;"foobar.com",
  :action=&gt;"something", :controller=&gt;"hw"
</pre>
<p>I think the change was made in <a href="http://github.com/rails/rails/commit/c3aaba0180f0710094d974b4ba4659bce81446df">this commit</a>.</p>
<p><strong>5. Render_partial is gone</strong></p>
<p>If you are still using render_partial in places, you should replace it with render :partial=&gt;&#8221;partial_name&#8221;</p>
<p><strong>6. HAML is not yet compatible with Rails 2.2</strong></p>
<p>There is a problem in HAML that causes output from calls to content_tag (and other tag helpers) to be lost.  See <a href="http://groups.google.com/group/haml/browse_thread/thread/72f8ac7868a37029">this thread for details</a>.</p>
<p>If you use HAML I do not recommend upgrading to rails 2.2 until this issue has been sorted out.  However, it&#8217;ll probably be fixed in a day or two, and the thread I linked to contains details of the patch I used if you need to fix it before that.</p>
<p><strong>7. Components are deprecated</strong></p>
<p>We had to rewrite some old code that was using components.  It actually wasn&#8217;t too much effort in the end, and the resulting refactoring was an improvement anyway.</p>
<p>&#8211;</p>
<p>As ever our tests were extremely valuable during this process.  Once HAML is sorted out we will be upgrading our production server to Rails 2.2, so hopefully that will be in a day or so.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2008/11/29/breakage-and-fixage-in-rails-22/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Slim-Attributes v0.5.0 released</title>
		<link>http://pennysmalls.com/2008/10/14/slim-attributes-v050-released/</link>
		<comments>http://pennysmalls.com/2008/10/14/slim-attributes-v050-released/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 18:57:17 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=31</guid>
		<description><![CDATA[I just released a new version of slim-attributes.  There are some small speed gains and some other minor changes from 0.4.1, but there are no big changes.
Read more about slim-attributes at the slim-attributes homepage, or read on below.
Introduction
Slim-attributes is a small patch to the ActiveRecord Mysql adaptor that stops rails from immediately making ruby strings [...]]]></description>
			<content:encoded><![CDATA[<p>I just released a new version of slim-attributes.  There are some small speed gains and some other minor changes from 0.4.1, but there are no big changes.</p>
<p>Read more about slim-attributes at the <a href="http://slim-attributes.rubyforge.org/">slim-attributes homepage</a>, or read on below.</p>
<p><strong>Introduction</strong></p>
<p>Slim-attributes is a small patch to the ActiveRecord Mysql adaptor that stops rails from immediately making ruby strings from the column names and data from your database queries.  Because you probably don&#8217;t need them all!</p>
<p>So ruby strings are lazily created on demand &#8211; it&#8217;s faster and uses less memory.  And it drops directly in, requiring only the installation of a gem and adding 1 line to environment.rb.</p>
<p>Measuring with just ActiveRecord code &#8211; fetching stuff from the database &#8211; we see anything up to a 50% (or more) speed increase, but it really depends on your system and environment, and what you are doing with the results from the database.  The more columns your tables have, the better the improvement will likely be.  Measure your own system and send me the results!</p>
<p><strong>Installation</strong></p>
<p>Try:</p>
<pre>
gem install slim-attributes -- --with-mysql-config
</pre>
<p>or:</p>
<pre>
gem install slim-attributes
</pre>
<p>then add this to environment.rb:</p>
<pre>
require 'slim_attributes'
</pre>
<p><strong>Description</strong></p>
<p>Normally the mysql adaptor in Rails returns a hash of the data returned from the database, one hash per active record object returned by the query.  The routine that generates these hashes is called all_hashes, and this is what we replace.  The reason for overriding all_hashes is threefold:</p>
<ul>
<li>making a hash of each and every row returned from the database is slow</li>
<li>rails makes frozen copies of each column name string (for the keys) which results in a great many strings which are not really needed</li>
<li>we observe that it&#8217;s not often that all the fields of rows fetched from the database are actually used</li>
</ul>
<p>So this is an alternative implementation of all_hashes that returns a &#8216;fake hash&#8217; which contains a hash of the column names (the same hash of names is used for every row), and also contains the row data in an area memcpy&#8217;d directly from the mysql API (which is much faster than creating ruby strings).</p>
<p>The field contents are then instantiated into Ruby strings on demand &#8211; ruby strings are only made if you need them &#8211; when you ask for a particular attribute from the model object.</p>
<p>Note that if you always look at all the columns when you fetch data from the database then this won&#8217;t necessarily be faster that the unpatched mysql adapter.  But it won&#8217;t be much slower either, and we do expect that most times not all the columns from a result set are accessed.</p>
<p><strong>Future development</strong></p>
<p>I speculate that further speed gains might be had through keeping the mysql result objects from mysql-ruby around, and not copying the data from them at all until it is needed.  However, mysql-ruby limits the non freed result sets to just 20 before calling GC.start, so surgery inside mysql-ruby would be required to achieve this.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2008/10/14/slim-attributes-v050-released/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Splitting models into several smaller files in Rails</title>
		<link>http://pennysmalls.com/2008/10/06/splitting-models-into-several-smaller-files-in-rails/</link>
		<comments>http://pennysmalls.com/2008/10/06/splitting-models-into-several-smaller-files-in-rails/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 20:52:38 +0000</pubDate>
		<dc:creator>Stephen Sykes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://pennysmalls.com/?p=30</guid>
		<description><![CDATA[I was reading what Paul Barry had to say about splitting models into smaller files.  It resonated with me a little &#8211; some of our models are approaching 1000 lines.
But I felt the name &#8216;concerned_with&#8217; did not fully / appropriately describe what is being done, and that there should be an easier way than [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading what Paul Barry had to say about <a href="http://paulbarry.com/articles/2008/08/30/concerned-with-skinny-controller-skinny-model">splitting models into smaller files</a>.  It resonated with me a little &#8211; some of our models are approaching 1000 lines.</p>
<p>But I felt the name &#8216;concerned_with&#8217; did not fully / appropriately describe what is being done, and that there should be an easier way than having to specify every file to be required.</p>
<p>So I ended up modifying the code to be a little easier to use.  If you place it in an initializer (i.e. in a file in your initializers directory), then you can specify in your model that you wish to require all the files from a subdirectory of the same name as the model.</p>
<p>So if you model is called Customer, then the model file is customer.rb.  Now you can also have a subdirectory called customer that contains further files containing model code.</p>
<p>In the original model class file you should add require_class_subdirectory to it, like this:</p>
<pre>
class Customer
  require_class_subdirectory
...
end
</pre>
<p>This will cause all the files in the subdirectory to be required.<br />
In each file in the subdirectory you should open the model class like so:</p>
<pre>class Customer
  def something  # you can cut/paste code in from the main model file
  end
...
end
</pre>
<p>The filenames you use don&#8217;t matter &#8211; in the above case it could be &#8217;something.rb&#8217; for instance.</p>
<p>So, to recap, your main class file customer.rb has &#8216;require_class_subdirectory&#8217; added to it.  You create a folder called &#8216;customer&#8217; in your models directory, and place some .rb files in there.  In each of those files you re-open the class (&#8216;class Customer&#8217;) and place code there just as if you were writing into the main class file.</p>
<p>This allows you to separate code according to function within a model, and to keep file sizes manageable.</p>
<p>Here is the code to put in the initializer:</p>
<pre>class &lt;&lt; ActiveRecord::Base
  def require_class_subdirectory
    ActiveSupport::Dependencies.load_paths.select{|lp| lp =~ /app\/models/}.each do |path|
      Dir["#{path}/#{name.underscore}/*.rb"].each do |filename|
        require_dependency "#{name.underscore}/#{File.basename(filename)}"
      end
    end
  end
end
</pre>
<p>Other approaches to this problem are possible.  In particular it may be feasible to patch or hook the constant missing mechanism in rails to automatically load the files in the subdirectory, which would remove the need for the require_class_subdirectory line in your main model file.</p>
<p>Finally, not even everyone thinks this is a problem that needs to be solved.  My colleague who uses Aptana says it has a good outline mode that means it&#8217;s easier to work with one large file for a model than lots of smaller ones.  In Textmate I find the smaller files easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://pennysmalls.com/2008/10/06/splitting-models-into-several-smaller-files-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
