<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: 21croissants's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 14:57:39 GMT</pubDate>
    <description>DZone Snippets: 21croissants's Code Snippets</description>
    <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>
  </channel>
</rss>
