<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Articles</title>
    <description>Articles</description>
    <link>http://thejls.com/articles</link>
<item>
  <title>Getting Started with RSpec</title>
  <description>&lt;p&gt;&lt;p&gt;I'll admit it, I don't test as much as I should.  I understood the importance of &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;Test Driven Development&lt;/a&gt; but never invested time into getting up to speed with RSpec, or any other testing tool for that matter.&lt;/p&gt;

&lt;p&gt;I decided to commit to using RSpec for testing and development of one of my latest projects and I will say it &lt;strong&gt;has been an amazing experience&lt;/strong&gt;.  There is a real feeling of comfort knowing that the change you just made is not only already covered in tests but also has not broken anything else.&lt;/p&gt;

&lt;p&gt;The docs are pretty good for RSpec so I'm going to just cover getting a Rails 3 app setup to use &lt;a href="http://rspec.info"&gt;RSpec 2&lt;/a&gt;, &lt;a href="https://github.com/thoughtbot/factory_girl"&gt;FactoryGirl&lt;/a&gt;, &lt;a href="https://github.com/floehopper/mocha"&gt;Mocha&lt;/a&gt; and &lt;a href="http://www.zenspider.com/ZSS/Products/ZenTest/"&gt;Autotest&lt;/a&gt;.  After those gems are installed and setup, there is plenty of documentation for you to bury yourself in!&lt;/p&gt;
&lt;/p&gt;&lt;p&gt;&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;We are going to write a book store app and keep it really simple so we can focus on getting the testing portion setup correctly.&lt;/p&gt;

&lt;div&gt;
&lt;coderay lang="ruby"&gt;
  rails new bookstore
  cd bookstore
  rm public/index.html
  rm -rf test
  rake db:migrate
&lt;/coderay&gt;
&lt;/div&gt;


&lt;p&gt;Open up your &lt;strong&gt;Gemfile&lt;/strong&gt; and add the testing libraries we will be using to the &lt;strong&gt;development&lt;/strong&gt; group.&lt;/p&gt;

&lt;div&gt;
&lt;p class="code-filename"&gt;Gemfile&lt;/p&gt;
&lt;coderay lang="ruby"&gt;
  source 'http://rubygems.org'

  gem 'rails', '3.0.2'
  gem 'sqlite3-ruby', :require =&gt; 'sqlite3'
  gem 'nifty-generators'
  
  group :development, :test do
    gem "factory_girl"
    gem 'factory_girl_rails'
    gem 'autotest'
    gem 'rspec'
    gem 'rspec-rails'
    gem "mocha"
  end
&lt;/coderay&gt;
&lt;/div&gt;


&lt;p&gt;Run bundle install to get the new gems installed and run the  rspec generator.&lt;/p&gt;

&lt;div&gt;
&lt;coderay lang="ruby"&gt;
  bundle install
  rails generate rspec:install
&lt;/coderay&gt;
&lt;/div&gt;


&lt;p&gt;The generator will create a directory called &lt;em&gt;/spec&lt;/em&gt; under the root app directory as well as a &lt;em&gt;spec/spec_helper.rb&lt;/em&gt; file.&lt;/p&gt;

&lt;h2&gt;RSpec Helper&lt;/h2&gt;

&lt;p&gt;Let's customize the &lt;em&gt;spec_helper.rb&lt;/em&gt; file since we are using Mocha for mocking and FactoryGirl instead of fixtures.&lt;/p&gt;

&lt;div&gt;
&lt;p class="code-filename"&gt;spec/spec_helper.rb&lt;/p&gt;
&lt;coderay lang="ruby"&gt;
  # Comment Out Line 18
  # config.mock_with :rspec
  # And add this in it's place
  config.mock_with :mocha
  
  # Comment out line 21 since we are 
  # using FactoryGirl and not fixtures
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"
&lt;/coderay&gt;
&lt;/div&gt;


&lt;p&gt;Now we are ready to generate our book resource.  I'm going to use &lt;a href="https://github.com/ryanb/nifty-generators"&gt;NiftyGenerators&lt;/a&gt; to generate the book model which is especially nice since it will generate the correct RSpec controller and model tests!&lt;/p&gt;

&lt;div&gt;
&lt;coderay lang="ruby"&gt;
  rails g nifty:layout
  rails g nifty:scaffold Book title:string author:string published:boolean
  rake db:migrate
&lt;/coderay&gt;
&lt;/div&gt;


&lt;p&gt;Let's modify our routes to make the book resource our root controller.&lt;/p&gt;

&lt;div&gt;
&lt;p class="code-filename"&gt;config/routes.rb&lt;/p&gt;
&lt;coderay lang="ruby"&gt;
  root :to =&gt; "books#index"
&lt;/coderay&gt;
&lt;/div&gt;


&lt;p&gt;If you open your &lt;code&gt;spec&lt;/code&gt; directory you will notice that NiftyGenerator has created a &lt;code&gt;controller&lt;/code&gt;, &lt;code&gt;models&lt;/code&gt; and &lt;code&gt;fixtures&lt;/code&gt; directory for us.  Then on top of that there are specs in those directories that provide a great base!&lt;/p&gt;

&lt;p&gt;If we run &lt;code&gt;rake spec&lt;/code&gt; now we will see that we have &lt;code&gt;10 examples, 0 failures&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Autotest&lt;/h2&gt;

&lt;p&gt;So far we have a good base and can already use &lt;a href="http://www.zenspider.com/ZSS/Products/ZenTest/"&gt;Autotest&lt;/a&gt; by running &lt;code&gt;autotest&lt;/code&gt; in terminal.&lt;/p&gt;

&lt;h2&gt;Coming Up&lt;/h2&gt;

&lt;p&gt;In the next article we will replace our fixtures with &lt;a href=""&gt;FactoryGirl&lt;/a&gt;.  There is also a Railscast about &lt;a href="http://railscasts.com/episodes/158-factories-not-fixtures"&gt;replacing your fixtures with factories&lt;/a&gt; which I highly recommend!&lt;/p&gt;
&lt;/p&gt;</description>
  <pubDate>Fri, 03 Dec 2010 00:29:46 +0000</pubDate>
  <link>http://thejls.com/articles/getting-started-with-rspec</link>
</item>
<item>
  <title>Less Rails More Migrate</title>
  <description>&lt;p&gt;&lt;p&gt;I&amp;#8217;ve recently needed a way to migrate multiple databases, all with different connections and migrations, with a single rake task.  Our first solution was to just create a new rails project for each database, setup our database.yml file as normal and drop the migrations into db/migrate.&lt;/p&gt; 
&lt;p&gt;We could have called it there and been done with it.  But I&amp;#8217;m lazy.  Having to go into each directory and run rake db:migrate was not going to let me reach the level of lazy I strive for.  Not to mention all of the bloat of having a full rails projects hanging around just for the migrations.&lt;/p&gt; 
&lt;p&gt;Let&amp;#8217;s make it lazy friendly &amp;#8482;.&lt;/p&gt; &lt;/p&gt;&lt;p&gt;&lt;h2&gt;The Setup&lt;/h2&gt; 
&lt;p&gt;I first created the directory structure that would hold each databases config file and migrations.&lt;/p&gt; 
&lt;pre&gt;migrator
 -&amp;gt; db
   -&amp;gt; configs
   -&amp;gt; migrations
 -&amp;gt; logs&lt;/pre&gt; 
&lt;p&gt;For each database that needed to be migrated I created the following files:&lt;/p&gt; 
&lt;p class='code-filename'&gt;migrator/configs/sample_database_1.yml&lt;/p&gt; 

&lt;coderay lang="ruby"&gt;
adapter: mysql
database: sample_database_name
username: supersecret
password: plaintext
host: localhost
&lt;/coderay&gt;

&lt;p&gt;Just the standard database.yml file without the development, production, etc top level nodes since this would only be run in one environment.&lt;/p&gt; 
&lt;p&gt;&lt;strong&gt;Folder in migrator/migrations named after the config yml&lt;/strong&gt;&lt;/p&gt; 
&lt;p&gt;Using the example above then the folder would be /migrator/migrations/sample_database_1.  In that folder we could drop all of our migration files (001_first_migration.rb, 002_second_migration, etc).&lt;/p&gt; 
&lt;h2&gt;The meat of our lazy sandwich&lt;/h2&gt; 
&lt;p&gt;A rake file to run all these migrations.&lt;/p&gt; 

&lt;p class="code-filename"&gt;migrator/Rakefile&lt;/p&gt;
&lt;div&gt;
&lt;coderay lang="ruby"&gt;
require 'active_record' 
require 'yaml' 

task :default =&gt; :migrate 
desc "Migrate the database(s) in db/migrations." 
task :migrate do 
   migration_dir = Dir.new(File.join(File.dirname(__FILE__),"db","migrations")) 
   config_dir    = Dir.new(File.join(File.dirname(__FILE__),"db","configs")).path 
   logs_dir      = Dir.new(File.join(File.dirname(__FILE__), "logs")).path 
   migration_dir.each do |d| 
     next if d.first == '.' 
     puts "Migrating '#{d}'..." 
     begin 
       ActiveRecord::Base.establish_connection(YAML::load(File.open(File.join(config_dir, "#{d}.yml")))) 
       ActiveRecord::Base.logger = Logger.new(File.open(File.join(logs_dir, "#{d}.log"), 'a')) 
       ActiveRecord::Migrator.migrate(File.join(migration_dir.path, d), ENV["VERSION"] ? ENV["VERSION"].to_i : nil) 
       puts "Done" 
     rescue Exception =&gt; e
       puts "\n\n#{e.to_s}\n" 
     end 
   end 
end
&lt;/coderay&gt;
&lt;/div&gt;
 
&lt;h2&gt;Run It&lt;/h2&gt; 
&lt;p&gt;Now to run our new migrator just go into the migrator directory and run&lt;/p&gt; 
&lt;pre&gt;&amp;gt; rake&lt;/pre&gt; 
&lt;p&gt;You can also pass it a version if you want to run migrate to a certain version.  Just keep in mind it will migrate all of the databases to the same version.  Great for the solution I needed maybe not great for yours.&lt;/p&gt; 
&lt;p&gt;I've put together a tar file that has the rake file and directory structure already setup.  &lt;a onclick="javascript:pageTracker._trackPageview('/downloads/migrator.tar');" href="https://s3.amazonaws.com/com.jls.media/migrator.tar"&gt;Grab it from here&lt;/a&gt;&lt;/p&gt;&lt;/p&gt;</description>
  <pubDate>Fri, 01 Oct 2010 18:50:19 +0000</pubDate>
  <link>http://thejls.com/articles/less-rails-more-migrate</link>
</item>
<item>
  <title>Using mod-rewrite to return default images</title>
  <description>&lt;p&gt;&lt;h2&gt;The Problem&lt;/h2&gt; 
&lt;p&gt;We have a web app running that displays pages of thumbnails.  Instead of having a rails action that would return the thumbnail or a default image if no thumbnail was available, I used Apache&amp;#8217;s mod_rewrite to do it for me.&lt;/p&gt; 
&lt;p&gt;The behavior I was looking for was if the image requested existed then pass the request through.  If the image requested did not exist on the file system then rewrite the request to &lt;em&gt;/images/image_not_available.jpg&lt;/em&gt;&lt;/p&gt; &lt;/p&gt;&lt;p&gt;&lt;h2&gt;The Rewrite Rules&lt;/h2&gt; 
&lt;p&gt;The rewrite rules I needed were simple and may not work for anyone elses need but all of our thumbnails are stored as &lt;em&gt;/thumbnails/image_filename.jpg&lt;/em&gt;&lt;/p&gt; 
&lt;p&gt;Using RewriteCond -f then I can check if the requested file name exists on the file system.&lt;/p&gt; 

&lt;coderay lang="ruby"&gt;
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^(/thumbnails/.+)\.jpg$  /images/image_not_available.jpg [L]&lt;/coderay&gt;

&lt;p&gt;Saved  me the overhead of having a Rails action that does the same thing.&lt;/p&gt;&lt;/p&gt;</description>
  <pubDate>Fri, 01 Oct 2010 15:26:00 +0000</pubDate>
  <link>http://thejls.com/articles/using-mod-rewrite-to-return-default-images</link>
</item>
  </channel>
</rss>
