<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: fixtures code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 07 Sep 2008 09:21:19 GMT</pubDate>
    <description>DZone Snippets: fixtures code</description>
    <item>
      <title>Synchronizing Rails DB Contents via Fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3393</link>
      <description>The following rake task will dump the contents of the current environment's database to YAML fixtures. Stick the following in lib/tasks/fixtures.rake:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  namespace :fixtures do&lt;br /&gt;    &lt;br /&gt;    desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;    Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;    task :dump =&gt; :environment do&lt;br /&gt;      sql  = "SELECT * FROM %s"&lt;br /&gt;      skip_tables = ["schema_info"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After making changes to the database that you'd like to dump to fixtures:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:fixtures:dump&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After checking out updated fixtures from SVN:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:migrate&lt;br /&gt;rake db:fixtures:load&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 01:04:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3393</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>All Fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3155</link>
      <description>&lt;code&gt;&lt;br /&gt;def self.all_fixtures(options={})&lt;br /&gt;  fixtures Dir.entries(RAILS_ROOT + '/test/fixtures').select {|f| f =~ /^\w+/ }.collect {|f| f.sub!(/.yml$/,'').to_sym }.reject {|i| [*options[:except]].include? i }&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Dec 2006 03:06:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3155</guid>
      <author>packagethf (Jeffrey Hardy)</author>
    </item>
    <item>
      <title>DRYing up YAML fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3114</link>
      <description>// Rails Recipes explained how to DRY up the database configuration code. I applied the same idea to user fixtures, which worked while we used MySQL. Once on Postgres, "defaults" started throwing an error. The easiest solution was to make quentin's values the 'defaults'&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;quentin: &amp;defaults&lt;br /&gt;  id: 1&lt;br /&gt;  login: quentin&lt;br /&gt;  email: quentin@example.com&lt;br /&gt;  site_id: 1&lt;br /&gt;  salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd&lt;br /&gt;  crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test&lt;br /&gt;  created_at: &lt;%= 5.days.ago.to_s :db %&gt;&lt;br /&gt;  activated_at: &lt;%= 5.days.ago.to_s :db %&gt; # only if you're activating new signups&lt;br /&gt;aaron:&lt;br /&gt;  id: 2&lt;br /&gt;  login: aaron&lt;br /&gt;  email: aaron@example.com&lt;br /&gt;  activation_code: aaronscode&lt;br /&gt;  site_id: 1&lt;br /&gt;  &lt;&lt;: *defaults&lt;br /&gt;#etc...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Dec 2006 03:12:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3114</guid>
      <author>chebuctonian (Daniel Haran)</author>
    </item>
    <item>
      <title>Create YAML test fixtures from database in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2525</link>
      <description>As found at &lt;a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake"&gt;http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;&lt;br /&gt;task :extract_fixtures =&gt; :environment do&lt;br /&gt;  sql  = "SELECT * FROM %s"&lt;br /&gt;  skip_tables = ["schema_info"]&lt;br /&gt;  ActiveRecord::Base.establish_connection&lt;br /&gt;  (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;    i = "000"&lt;br /&gt;    File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;      data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;      file.write data.inject({}) { |hash, record|&lt;br /&gt;        hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;        hash&lt;br /&gt;      }.to_yaml&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Sep 2006 01:48:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2525</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Generate Rails fixture skeleton using ActiveRecord</title>
      <link>http://snippets.dzone.com/posts/show/2389</link>
      <description>In Rails 1.1.5, the basic generator generates the following code for the fixture used in database unit tests:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html&lt;br /&gt;first:&lt;br /&gt;  id: 1&lt;br /&gt;another:&lt;br /&gt;  id: 2&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;As ActiveRecord provides database reflexion features, we can generate a fixture file with all the columns' name prepopulated for number and text types, such as:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html&lt;br /&gt;first:&lt;br /&gt;  id: 1&lt;br /&gt;  short_title: short_title_first&lt;br /&gt;  title: title_first&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This will be done by the following class:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require_gem 'activerecord'&lt;br /&gt;&lt;br /&gt;class RailsFixturesGenerator&lt;br /&gt;&lt;br /&gt;  def generate(class_name)&lt;br /&gt;    &lt;br /&gt;    # Get the "Class" object from the class name        &lt;br /&gt;    model_class = Object.const_get(class_name)&lt;br /&gt;    &lt;br /&gt;    yaml_content =  "# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html\n"&lt;br /&gt;    yaml_content += "first:\n"&lt;br /&gt;    &lt;br /&gt;    # get if first!   &lt;br /&gt;    model_class.columns.each { |column|&lt;br /&gt;      &lt;br /&gt;      yaml_content += "  " + column.name + ": "&lt;br /&gt;      &lt;br /&gt;      if column.number?&lt;br /&gt;        yaml_content +=  "1"&lt;br /&gt;      end&lt;br /&gt;      if column.text?&lt;br /&gt;        # @todo /!\ max length&lt;br /&gt;        yaml_content +=  column.name + "_first"&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      yaml_content += "\n"      &lt;br /&gt;    }  &lt;br /&gt;    &lt;br /&gt;    write_fixture_file(model_class, yaml_content)&lt;br /&gt;    &lt;br /&gt;    yaml_content            &lt;br /&gt;  end  &lt;br /&gt;  &lt;br /&gt;  # Write the &lt;fixture&gt; yaml file  in the test/fixtures folder&lt;br /&gt;  def write_fixture_file(model_class, yaml_content)&lt;br /&gt;    &lt;br /&gt;    path = ENV['DEST'] || "#{RAILS_ROOT}/test/fixtures"&lt;br /&gt;    db   = ENV['DB']   || 'test'&lt;br /&gt;    &lt;br /&gt;    File.open("#{path}/#{model_class.table_name}.yml", 'wb') do |file|    &lt;br /&gt;      file.write yaml_content &lt;br /&gt;      file.close    &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Of course, I have an unit test that I wrote before the code ;-)&lt;br /&gt;This was my first "complex" method I wrote in Ruby so please bear with me. Any feedback is welcome. I want to write a Rails plugin in order to share the generators I will write. </description>
      <pubDate>Thu, 10 Aug 2006 20:34:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2389</guid>
      <author>21croissants (Jean-Michel G)</author>
    </item>
    <item>
      <title>Fix for nil object error in Rails test fixtures</title>
      <link>http://snippets.dzone.com/posts/show/1135</link>
      <description>If you're seeing errors like this when you run Rails tests:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# NoMethodError: You have a nil object when you didn't expect it!&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You might need to edit test/test_helper.rb to make sure use_instantiated_fixtures is true:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;self.use_instantiated_fixtures = true&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Prior to 1.0, Rails automatically created instance variables out of fixtures. So if you had a fixture record named "foo", you could access it in your test as "@foo". As of 1.0, the default is to disable that feature, which breaks a lot of existing code. Mike Clark &lt;a href="http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting" title="Mike Clark's Weblog: Faster Testing with Rails 1.0"&gt;explains the change&lt;/a&gt;.</description>
      <pubDate>Wed, 11 Jan 2006 06:43:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1135</guid>
      <author>brainpipe ()</author>
    </item>
    <item>
      <title>loading fixtures to development database</title>
      <link>http://snippets.dzone.com/posts/show/446</link>
      <description>Fixed the previous example so it would load environment first:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Load fixtures data into the development database"&lt;br /&gt;task :load_fixtures_to_development =&gt; :environment do&lt;br /&gt;  ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;  require 'active_record/fixtures'&lt;br /&gt;  Fixtures.create_fixtures("test/fixtures", ActiveRecord::Base.configurations[:fixtures_load_order])&lt;br /&gt;  puts "Loaded these fixtures: " + ActiveRecord::Base.configurations[:fixtures_load_order].collect { |f| f.to_s }.join(', ')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This requires a fixture list in database.yml (or you can specify in environment.rb if you wish:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;:fixtures_load_order:&lt;br /&gt;  - :fixture_1&lt;br /&gt;  - :fixture_2&lt;br /&gt;  - :fixture_3&lt;br /&gt;  - :fixture_4&lt;br /&gt;  - :fixture_5&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Jul 2005 03:03:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/446</guid>
      <author>technoweenie (Rick Olson)</author>
    </item>
    <item>
      <title>Loading data from fixtures in development mode</title>
      <link>http://snippets.dzone.com/posts/show/367</link>
      <description>By &lt;a href="http://one.textdrive.com/pipermail/rails/2005-March/003844.html"&gt;Tim Bates.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;desc "Load fixtures data into the development database"&lt;br /&gt;task :load_fixtures_data_to_development do&lt;br /&gt;   require 'active_record/fixtures'&lt;br /&gt;   ActiveRecord::Base.establish_connection(&lt;br /&gt;       ActiveRecord::Base.configurations["development"])&lt;br /&gt;   Fixtures.create_fixtures("test/fixtures",&lt;br /&gt;       ActiveRecord::Base.configurations[:fixtures_load_order])&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Sun, 05 Jun 2005 07:11:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/367</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
