<?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>Matthew Bass &#187; Tutorials</title>
	<atom:link href="http://matthewbass.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewbass.com</link>
	<description>Musings on software and life...</description>
	<lastBuildDate>Fri, 24 Feb 2012 16:48:54 +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>How to safely transpose Ruby arrays</title>
		<link>http://matthewbass.com/2009/05/02/how-to-safely-transpose-ruby-arrays/</link>
		<comments>http://matthewbass.com/2009/05/02/how-to-safely-transpose-ruby-arrays/#comments</comments>
		<pubDate>Sat, 02 May 2009 04:09:53 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/?p=309</guid>
		<description><![CDATA[Ruby arrays have this handy method called transpose which takes an existing 2-dimensional array (i.e. a matrix) and flips it on its side:

>> a = [[1,2], [3,4], [5,6]]
>> puts a.transpose.inspect
[[1, 3, 5], [2, 4, 6]]

Each row becomes a column, essentially. This is fine and dandy for polite arrays. If one of the rows in the [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby arrays have this handy method called <code>transpose</code> which takes an existing 2-dimensional array (i.e. a matrix) and flips it on its side:</p>
<pre>
>> a = [[1,2], [3,4], [5,6]]
>> puts a.transpose.inspect
[[1, 3, 5], [2, 4, 6]]
</pre>
<p>Each row becomes a column, essentially. This is fine and dandy for polite arrays. If one of the rows in the original array is not as long as the others, though, Ruby <a href="http://dictionary.reference.com/dic?q=chunder">chunders</a> thusly:</p>
<pre>
>> a = [[1,2], [3,4], [5]]
>> a.transpose
IndexError: element size differs (1 should be 2)
	from (irb):3:in `transpose'
	from (irb):3
</pre>
<p>That ain&#8217;t pretty, especially if the intent behind using <class>transpose</class> is to render data in a nice columnar fashion. For example, what if we wanted to render a list of high school courses in columns, one column per semester? Grouping the courses by semester and then transposing would do the trick, <em>but only if there were exactly the same number of courses taken each semester</em>. If even <em>one</em> semester differs, Ruby will blow up. What we really want is for Ruby to just ignore the fact that each grouping may have differently sized arrays and transpose anyway, filling in the empty spaces with nils.</p>
<p>Here&#8217;s how to do just that:</p>
<pre class="brush: ruby;">
class Array
  def safe_transpose
    result = []
    max_size = self.max { |a,b| a.size &lt;=&gt; b.size }.size
    max_size.times do |i|
      result[i] = Array.new(self.first.size)
      self.each_with_index { |r,j| result[i][j] = r[i] }
    end
    result
  end
end
</pre>
<p>Now we call <code>safe_transpose</code> on our matrix of courses and Ruby does the right thing. It calculates the length of the longest row and uses that as the baseline to perform the transposition. So our original example becomes:</p>
<pre>
>> a = [[1,2], [3,4], [5]]
>> puts a.transpose.inspect
[[1, 3, 5], [2, 4, nil]]
</pre>
<p>Nice and neat. <strong>Caveats</strong>: the code above hasn&#8217;t been refactored or tested. Your mileage may vary. If you see a better way to do this, <a href="mailto:pelargir@gmail.com">let me know</a> and I&#8217;ll post an update.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2009/05/02/how-to-safely-transpose-ruby-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sending mail from Rails through Google SMTP</title>
		<link>http://matthewbass.com/2008/06/11/sending-mail-from-rails-through-google-smtp/</link>
		<comments>http://matthewbass.com/2008/06/11/sending-mail-from-rails-through-google-smtp/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 14:54:34 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/06/11/sending-mail-from-rails-through-google-smtp/</guid>
		<description><![CDATA[I just ran into a problem configuring a Rails app to deliver email through Google&#8217;s SMTP servers. ActionMailer doesn&#8217;t support TLS/SSL which is required by Google. Fortunately, the action_mailer_optional_tls plugin provides this functionality.
I wanted to host my SMTP settings in an external YAML file so I wouldn&#8217;t end up checking my username and password into [...]]]></description>
			<content:encoded><![CDATA[<p>I just ran into a problem configuring a Rails app to deliver email through Google&#8217;s SMTP servers. ActionMailer doesn&#8217;t support TLS/SSL which is required by Google. Fortunately, the <a href="http://github.com/collectiveidea/action_mailer_optional_tls/tree/master">action_mailer_optional_tls</a> plugin provides this functionality.</p>
<p>I wanted to host my SMTP settings in an external YAML file so I wouldn&#8217;t end up checking my username and password into the repository. (The YAML file is placed on the production server and a softlink is created during each deploy.) For some reason, I kept getting &#8220;connection refused&#8221; messages whenever I tried to send email with this configuration:</p>
<pre>
# smtp.yml
production:
  address: smtp.gmail.com
  port: 587
  tls: true
  domain: foo.com
  authentication: :plain
  user_name: someone@foo.com
  password: secret

# production.rb
smtp = YAML::load(File.open("#{RAILS_ROOT}/config/smtp.yml"))
ActionMailer::Base.smtp_settings = smtp[Rails.env]
</pre>
<p>The problem was the hash returned by YAML. The keys were strings, whereas ActionMailer was expecting the keys to be symbols. The fix was to make the hash use indifferent access:</p>
<pre>
...
ActionMailer::Base.smtp_settings = smtp[Rails.env].with_indifferent_access
</pre>
<p>That cleared up the &#8220;connection refused&#8221; problem. Now my app is sending email like a champ.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/06/11/sending-mail-from-rails-through-google-smtp/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to block ads on Facebook</title>
		<link>http://matthewbass.com/2008/05/28/block-ads-on-facebook/</link>
		<comments>http://matthewbass.com/2008/05/28/block-ads-on-facebook/#comments</comments>
		<pubDate>Thu, 29 May 2008 02:23:14 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/05/28/block-ads-on-facebook/</guid>
		<description><![CDATA[The ads they run on Facebook are getting downright annoying. I&#8217;m confident I&#8217;m not alone in this feeling. Here&#8217;s a beginner&#8217;s tutorial describing how you can prevent Facebook ads from being displayed. You also pick up a few other nice features in the process.
1. Download and install Firefox
If you&#8217;re already using Firefox, you can skip [...]]]></description>
			<content:encoded><![CDATA[<p>The ads they run on <a href="http://facebook.com">Facebook</a> are getting downright annoying. I&#8217;m confident I&#8217;m not alone in this feeling. Here&#8217;s a beginner&#8217;s tutorial describing how you can prevent Facebook ads from being displayed. You also pick up a few other nice features in the process.</p>
<p><strong>1. Download and install Firefox</strong></p>
<p>If you&#8217;re already using Firefox, you can skip this step. If you&#8217;re not, consider this your wake-up call. Internet Explorer just doesn&#8217;t cut it anymore. Aside from being completely <a href="http://en.wikipedia.org/wiki/Open_source">open source</a>, Firefox allows installation of scripts that enhance your browsing experience. You won&#8217;t be able to block Facebook ads without using Firefox.</p>
<p><a href="http://www.mozilla.com/en-US/firefox/">Visit this page</a>, click on the big green download link, save the installer to your hard drive, and run it. Proceed through the installation&#8230;</p>
<p><strong>2. Install Greasemonkey</strong></p>
<p>Greasemonkey is an add-on for Firefox that allows for customization of the way a web page is displayed. It relies on <a href="http://en.wikipedia.org/wiki/JavaScript">JavaScript</a>, but that&#8217;s not important to know for what we&#8217;re doing (unless you&#8217;re a geek).</p>
<p>First, <strong>bookmark this post</strong>. You&#8217;re going to restart Firefox after this step. You&#8217;ll want to get back to this post so you can pick up where you left off.</p>
<p>Next, <a href="https://addons.mozilla.org/en-US/firefox/addon/748">visit this page</a> and click the green &#8220;Add to Firefox&#8221; button. A dialog will pop up. Wait for the countdown to finish, then click the &#8220;Install&#8221; button. A new dialog will ask if you want to restart Firefox. Yes, you want to, so do it.</p>
<p><strong>3. Install Facebook Companion</strong></p>
<p>This is a Greasemonkey script that does three nice things to Facebook:</p>
<ul>
<li>Removes ads</li>
<li>Adds an &#8220;Ignore All Requests&#8221; button (useful if you don&#8217;t want to see new app requests)</li>
<li>Adds a plus over all thumbnails that, when clicked, pops up a large version of the image</li>
</ul>
<p>To install, <a href="http://userscripts.org/scripts/show/8475">visit this page</a> and click on the small, gray button on the right side titled &#8220;Install this script&#8221; (it&#8217;s just below the search box). Again, you will be presented with a dialog and an &#8220;Install&#8221; button. Click it&#8230;</p>
<p><strong>4. Visit Facebook and enjoy</strong></p>
<p>Now it&#8217;s time to <a href="http://facebook.com">visit Facebook</a> and do some ad-free social networking. Enjoy!</p>
<p><em>Extra credit: if you&#8217;re interested in browsing for Greasemonkey scripts that do other cool things, check out <a href="http://userscripts.org">Userscripts.org</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/05/28/block-ads-on-facebook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting speed dial numbers on a Sprint RAZR V3m</title>
		<link>http://matthewbass.com/2008/05/22/setting-speed-dial-numbers-on-a-sprint-razr-v3m/</link>
		<comments>http://matthewbass.com/2008/05/22/setting-speed-dial-numbers-on-a-sprint-razr-v3m/#comments</comments>
		<pubDate>Thu, 22 May 2008 19:35:32 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Products]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/05/22/setting-speed-dial-numbers-on-a-sprint-razr-v3m/</guid>
		<description><![CDATA[I&#8217;ve been generally displeased with my Motorola RAZR. Sprint gave it to me over a year ago and aside from terribly poor battery life, it has one of the worst user interfaces I&#8217;ve ever seen on a phone. Despite that, it&#8217;s very compact and since my Sprint plan doesn&#8217;t expire until October of this year [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been generally displeased with my Motorola RAZR. Sprint gave it to me over a year ago and aside from terribly poor battery life, it has one of the worst user interfaces I&#8217;ve ever seen on a phone. Despite that, it&#8217;s very compact and since my Sprint plan doesn&#8217;t expire until October of this year I&#8217;ve stuck with it.</p>
<p>Something I couldn&#8217;t figure out was how to set the speed dial numbers. Turns out that there isn&#8217;t a way to do this through the main &#8220;Contacts&#8221; list (seems like that would be the best place for it). After Google failed to turn up anything, I began randomly clicking through my settings menu in frustration, attempting to locate the speed dial settings. I finally found them. <em>Finally</em>.</p>
<p>Go to the main settings pane, then select the &#8220;Contacts&#8221; button (orange book with a phone icon on the front). There will be an entry on this menu titled &#8220;Speed Dial #s&#8221; which will let you configure everything you need. Why this wasn&#8217;t included on the main contact list I&#8217;ll never know, but there you have it.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/05/22/setting-speed-dial-numbers-on-a-sprint-razr-v3m/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Download an image file using wget</title>
		<link>http://matthewbass.com/2008/05/21/download-an-image-file-using-wget/</link>
		<comments>http://matthewbass.com/2008/05/21/download-an-image-file-using-wget/#comments</comments>
		<pubDate>Thu, 22 May 2008 01:22:57 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/05/21/download-an-image-file-using-wget/</guid>
		<description><![CDATA[
wget -O filename.png "http://some.url/folder/some_image.png"

And to upload the same file using Capistrano:

task :something do
  put File.read("filename.png"), "#{deploy_to}/remote_filename.png"
end

]]></description>
			<content:encoded><![CDATA[<pre>
wget -O filename.png "http://some.url/folder/some_image.png"
</pre>
<p>And to upload the same file using <a href="http://capify.org">Capistrano</a>:</p>
<pre>
task :something do
  put File.read("filename.png"), "#{deploy_to}/remote_filename.png"
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/05/21/download-an-image-file-using-wget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to filter model data before rendering in Streamlined</title>
		<link>http://matthewbass.com/2008/03/15/how-to-filter-model-data-before-rendering-in-streamlined/</link>
		<comments>http://matthewbass.com/2008/03/15/how-to-filter-model-data-before-rendering-in-streamlined/#comments</comments>
		<pubDate>Sat, 15 Mar 2008 16:08:59 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/03/15/how-to-filter-model-data-before-rendering-in-streamlined/</guid>
		<description><![CDATA[One question that seems to come up a lot on the Streamlined mailing list is how best to filter the records that are displayed in the list view using a certain set of conditions. There are several ways of doing this, but the simplest way is to use Rails&#8217; built-in scoping methods in conjunction with [...]]]></description>
			<content:encoded><![CDATA[<p>One question that seems to come up a lot on the <a href="http://streamlinedframework.org">Streamlined</a> mailing list is how best to filter the records that are displayed in the list view using a certain set of conditions. There are several ways of doing this, but the simplest way is to use Rails&#8217; built-in scoping methods in conjunction with an around filter.</p>
<p>For example, say we have a Task model and a TaskController that is Streamlined-enabled. The TasksController will display a list of all Tasks in the system by default. What if we only want to display Tasks for the currently logged-in user? Something like this would work:</p>
<pre>
around_filter :scope_by_user, :only => [:list, :index]

def scope_by_user
  Task.find_with_user_scope(current_user) { yield }
end
</pre>
<p>Once this code is added to the TaskController, any calls to Task#find within the associated actions, views, and helpers will be scoped. Only tasks belonging to the current user will be listed.</p>
<p>What if we still want to perform an unscoped Task#find within an action? We can eliminate the scoping for a specific block of code using the #with_exclusive_scope method. It looks like this:</p>
<pre>
Task.with_exclusive_scope { code_that_should_not_be_scoped }
</pre>
<p>And that, folks, is how easy it is to filter lists in Streamlined.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/03/15/how-to-filter-model-data-before-rendering-in-streamlined/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using named routes in Streamlined addition files</title>
		<link>http://matthewbass.com/2008/02/04/using-named-routes-in-streamlined-addition-files/</link>
		<comments>http://matthewbass.com/2008/02/04/using-named-routes-in-streamlined-addition-files/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 20:33:46 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/02/04/using-named-routes-in-streamlined-addition-files/</guid>
		<description><![CDATA[For you Streamlined users out there, here&#8217;s an easy way to include named routes in your Streamlined addition modules. Previously, you had to hard code the URLs like so:

module OrderAdditions
  def name_link
    link_to user.name, "/users/show/#{user.id}"
  end
end

Order.class_eval do
  include OrderAdditions
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::TagHelper
end

Streamlined.ui_for(Order) do
  list_columns :name_link
end

By [...]]]></description>
			<content:encoded><![CDATA[<p>For you Streamlined users out there, here&#8217;s an easy way to include named routes in your Streamlined addition modules. Previously, you had to hard code the URLs like so:</p>
<pre>
module OrderAdditions
  def name_link
    link_to user.name, "/users/show/#{user.id}"
  end
end

Order.class_eval do
  include OrderAdditions
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::TagHelper
end

Streamlined.ui_for(Order) do
  list_columns :name_link
end
</pre>
<p>By including Rails 2&#8217;s new <code>ActionController::UrlWriter</code> module, you can access any named or RESTful routes you&#8217;ve defined in routes.rb:</p>
<pre>
module OrderAdditions
  def name_link
    link_to user.name, user_url(user.id)
  end
end

Order.class_eval do
  ...
  include ActionController::UrlWriter
  default_url_options[:host] = APP_HOST
end
</pre>
<p>It would be nice for this to get baked into Streamlined somehow so UrlWriter automatically gets included, but until that happens this is a good way to get rid of those pesky hard-coded URLs.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/02/04/using-named-routes-in-streamlined-addition-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Layout assertion added to test_spec_on_rails</title>
		<link>http://matthewbass.com/2008/01/10/layout-assertion-added-to-test_spec_rails/</link>
		<comments>http://matthewbass.com/2008/01/10/layout-assertion-added-to-test_spec_rails/#comments</comments>
		<pubDate>Thu, 10 Jan 2008 19:59:53 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/01/10/layout-assertion-added-to-test_spec_rails/</guid>
		<description><![CDATA[After contacting Rick with my patch, he offered to give me commit access to the project. I took him up on the offer, so the layout assertion is now in there.
test_spec_on_rails is now on Git so unless you&#8217;re running Edge Rails you can’t use script/install to grab it. My suggestion is to clone from Git [...]]]></description>
			<content:encoded><![CDATA[<p>After contacting Rick <a href="/2008/01/09/add-layout-checking-to-test_spec_rails-plugin/">with my patch</a>, he offered to give me commit access to the project. I took him up on the offer, so the layout assertion is now in there.</p>
<p>test_spec_on_rails is now on Git so unless you&#8217;re running Edge Rails you can’t use script/install to grab it. My suggestion is to clone from Git into vendor/plugins and then svn:ignore the .git file:</p>
<pre>
git clone git://github.com/pelargir/test_spec_on_rails.git vendor/plugins/test_spec_on_rails
svn propset svn:ignore .git vendor/plugins/test_spec_on_rails
</pre>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/01/10/layout-assertion-added-to-test_spec_rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Add layout checking to test_spec_on_rails</title>
		<link>http://matthewbass.com/2008/01/09/add-layout-checking-to-test_spec_rails-plugin/</link>
		<comments>http://matthewbass.com/2008/01/09/add-layout-checking-to-test_spec_rails-plugin/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 15:21:40 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/01/09/add-layout-checking-to-test_spec_rails-plugin/</guid>
		<description><![CDATA[test_spec_on_rails is a plugin that adds Rails-specific assertions to test_spec, allowing you to do nifty things like this:

it "should render with foo template" do
  get :some_action
  template.should.be "foo"
  # equivalent to assert_equal "foo", @response.template
end

it "should display foo on page" do
  get :some_action
  page.should.select "p", "foo"
  # equivalent to assert_select [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://agilewebdevelopment.com/plugins/test_spec_on_rails">test_spec_on_rails</a> is a plugin that adds Rails-specific assertions to <a href="https://rubyforge.org/projects/test-spec">test_spec</a>, allowing you to do nifty things like this:</p>
<pre>
it "should render with foo template" do
  get :some_action
  template.should.be "foo"
  # equivalent to assert_equal "foo", @response.template
end

it "should display foo on page" do
  get :some_action
  page.should.select "p", "foo"
  # equivalent to assert_select "p", "foo"
end
</pre>
<p>One thing test_spec_on_rails is missing, though, is the ability to check the layout that a template renders with. For example, it would be nice to do this:</p>
<pre>
it "should render with foo layout" do
  get :some_action
  layout.should.be "foo"
  # equivalent to assert_equal "foo", @response.layout
end
</pre>
<p>I&#8217;ve submitted a patch to Rick Olson that adds this capability. I&#8217;m not sure if or when it will get incorporated into the plugin though, so I&#8217;m <a href="/files/add_layout.diff">posting the patch here</a> for anyone who might need it.</p>
<p>To apply, right-click on the link and download the file into your vendor/plugins/test_spec_on_rails folder, then run this command from inside the same folder:</p>
<pre>
patch -p0 < add_layout.diff
</pre>
<p>Assuming your plugin isn't checked out as an external, you'll be able to commit the changes to your own repository. You can then start using the new hotness as described above. Enjoy!</p>
<p><strong>Update:</strong> Rick made me a committer so I was able to <a href="http://matthewbass.com/2008/01/10/layout-assertion-added-to-test_spec_rails/">add this myself</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/01/09/add-layout-checking-to-test_spec_rails-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fix for fixture_replacement2 when using default methods</title>
		<link>http://matthewbass.com/2008/01/07/tweak-fixture_replacement2-when-using-default-methods/</link>
		<comments>http://matthewbass.com/2008/01/07/tweak-fixture_replacement2-when-using-default-methods/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 02:21:25 +0000</pubDate>
		<dc:creator>Matthew Bass</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthewbass.com/2008/01/07/tweak-fixture_replacement2-when-using-default-methods/</guid>
		<description><![CDATA[I&#8217;ve been using the excellent fixture_replacement plugin for several months now and greatly prefer it over traditional fixtures (yes, even foxy fixtures). fixture_replacement2 adds even more goodness to the party. However, I ran into a problem today when trying to use default_xxx methods in my example_data.rb file:

module FixtureReplacement
  attributes_for :course do &#124;c&#124;
   [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using the excellent <a href="http://replacefixtures.rubyforge.org/">fixture_replacement</a> plugin for several months now and greatly prefer it over traditional fixtures (yes, even <a href="http://m.onkey.org/2007/10/26/fixtures-go-foxy">foxy fixtures</a>). fixture_replacement2 adds even more goodness to the party. However, I ran into a problem today when trying to use <code>default_xxx</code> methods in my example_data.rb file:</p>
<pre>
module FixtureReplacement
  attributes_for :course do |c|
    c.name = "Geometry"
    c.transcript = default_transcript
  end

  attributes_for :transcript do |e|
    e.name = "Joe's Transcript"
  end
end
</pre>
<p>In &#8220;fixture_replacement language&#8221; this says that new courses should receive a default name of &#8220;Geometry&#8221; and should also receive a new transcript with a default name of &#8220;Joe&#8217;s Transcript.&#8221; The example above follows the format given in the fixture_replacement <a href="http://replacefixtures.rubyforge.org/files/README.html">README</a>. When I attempt to use this config file in my Rails 2.0.2 project, though, I get this error:</p>
<pre>
NameError: undefined local variable or method `default_transcript' for FixtureReplacement:Module
at top level in example_data.rb at line 7
...
</pre>
<p>Why can&#8217;t it find <code>default_transcript</code>? I have no idea. Digging into the source code reveals that <code>default_transcript</code> <em>is</em> added to the FixtureReplacement module at runtime, but for some reason it doesn&#8217;t ever show up as being available for calling. I don&#8217;t have the time or inclination to dig further, but I did find a workaround by changing:</p>
<pre>
c.transcript = default_transcript
</pre>
<p>to&#8230;</p>
<pre>
c.transcript = c.default_transcript
</pre>
<p>This makes fixture_replacement happy and all my tests pass. I submitted a patch so hopefully this wrinkle will be ironed out soon. But this should get you by until then.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewbass.com/2008/01/07/tweak-fixture_replacement2-when-using-default-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

