<?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>Michael Schuerig &#187; Rails</title>
	<atom:link href="http://www.schuerig.de/michael/blog/index.php/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.schuerig.de/michael/blog</link>
	<description>Sentenced to making sense</description>
	<lastBuildDate>Tue, 06 Sep 2011 07:21:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Creative Associations</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2009/05/29/creative-associations/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2009/05/29/creative-associations/#comments</comments>
		<pubDate>Fri, 29 May 2009 14:01:58 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.schuerig.de/michael/blog/?p=62</guid>
		<description><![CDATA[A presentation I held on May 28 at Düsseldorf on Rails about interesting and useful things you can do with ActiveRecord, SQL, and a smart database. german english .]]></description>
			<content:encoded><![CDATA[<p>A presentation I held on May 28 at <a href="http://groups.google.de/group/duesseldorf-on-rails">Düsseldorf on Rails</a> about interesting and useful things you can do with <tt>ActiveRecord</tt>, <tt>SQL</tt>, and a smart database.</p>
<ul>
<li><a href="http://www.schuerig.de/michael/pres/kreative-assoziationen/index.html">german</a></li>
<li><a href="http://www.schuerig.de/michael/pres/kreative-assoziationen/index.html.en">english</a></li>
</ul>
<p>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2009/05/29/creative-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No database to rule them all. Branch databases for Rails and git</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2009/04/09/branch-db/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2009/04/09/branch-db/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 23:04:37 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://www.schuerig.de/michael/blog/?p=59</guid>
		<description><![CDATA[You&#8217;re using git to manage your Ruby on Rails projects? Then you&#8217;ve probably come to appreciate topic branches as a kind of transaction facility for programmers. If you need to implement a non-trivial or just try something, the way to go is onto a new &#8220;topic&#8221; branch. There you can mangle the code all you [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re using <em><code>git</code></em> to manage your <em>Ruby on Rails</em> projects? Then you&#8217;ve probably come to appreciate topic branches as a kind of transaction facility for programmers. If you need to implement a non-trivial or just try something, the way to go is onto a new &#8220;topic&#8221; branch. There you can mangle the code all you like, without fearing to cause any lasting harm. In case everything works out fine, you merge the branch into the master branch and discard it. If you come to a dead end &#8212; well, you <em>just</em> discard the misbegotten branch.</p>
<p>But what about the database? If moving along your branch involves changes to the database, structural changes in particular, you can&#8217;t easily switch to another branch without these changes. In other words, your development database is really only suitable for a single branch.</p>
<p>Up to now, that is. Install the <code>branch_db</code> gem and what you get is a private database for your branch that you can mutilate without interfering with work on the other branches.</p>
<p>For details and installation instructions see the <a href="http://github.com/mschuerig/branch_db/blob/master/README.rdoc">README</a>. Here&#8217;s just an appetizer. Say you&#8217;re on branch &#8220;feature&#8221; and want a private copy of the &#8220;master&#8221; database. Here&#8217;s how you do it:</p>
<p><code>
<pre>
$ rake db:branches:copy ORIG_BRANCH=master
</pre>
<p></code></p>
<h2>Getting it</h2>
<ul>
<li><a href="http://github.com/mschuerig/branch_db">github</a></li>
<li>$ sudo gem install mschuerig-branch_db</li>
</ul>
<h2>Let your Rails app know about it</h2>
<p>In the appropriate place in <code>config/environment.rb</code> add</p>
<p><code>
<pre>
config.gem "mschuerig-branch_db", :lib =&gt; 'branch_db'
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2009/04/09/branch-db/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby as a language for Rails views</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2009/04/02/ruby-views/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2009/04/02/ruby-views/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 22:55:31 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.schuerig.de/michael/blog/?p=57</guid>
		<description><![CDATA[Didn&#8217;t you always want to write your Rails views as plain Ruby objects? &#8212; &#8220;What?&#8221;, I hear you say. No, I haven&#8217;t lost my mind and the idea is quite sensible (or so I hope), once you add the restriction that it is JSON-formatted data that you want to return. Say you need to set [...]]]></description>
			<content:encoded><![CDATA[<p>Didn&#8217;t you always want to write your Rails views as plain Ruby objects? &#8212; <em>&#8220;What?&#8221;</em>, I hear you say. No, I haven&#8217;t lost my mind and the idea is quite sensible (or so I hope), once you add the restriction that it is JSON-formatted data that you want to return.</p>
<p>Say you need to set up some hashes or arrays for rendering to JSON. This is best done in Ruby and it is clearly a view concern. So let&#8217;s do it in the views. Like this:</p>
<p><code>
<pre>
  # app/controllers/movies_controller.rb
  def index
    respond_to do |format|
      format.json do
        @movies = Movie.all
        @count = Movie.count
        render :template =&gt; 'movies/index.json.rb'
      end
    end
  end
</pre>
<p></code></p>
<p><code>
<pre>
  # app/views/movies/index.json.rb
  {
    :identifier =&gt; Movie.primary_key,
    :totalCount =&gt; @count,
    # render @movies does not work as it insists on returning a string
    :items =&gt; @movies.map { |m| render(m) }
  }
</pre>
<p></code></p>
<p><code>
<pre>
  # app/views/movies/_movie.json.rb
  {
    :id =&gt; movie.to_param,
    :title =&gt; movie.title,
    :releaseDate =&gt; movie.release_date
  }
</pre>
<p></code></p>
<h2>Getting it</h2>
<ul>
<li><a href="http://github.com/mschuerig/ruby_template_handler">github</a></li>
<li>$ sudo gem install mschuerig-ruby_template_handler</li>
</ul>
<h2>Let your Rails app know about it</h2>
<p>In the appropriate place in <code>config/environment.rb</code> add</p>
<p><code>
<pre>
config.gem "mschuerig-ruby_template_handler", :lib =&gt; 'ruby_template_handler'
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2009/04/02/ruby-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simplistic Enumerations for ActiveRecord</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2009/04/02/simplistic-enums/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2009/04/02/simplistic-enums/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 19:49:03 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.schuerig.de/michael/blog/?p=55</guid>
		<description><![CDATA[Invariably, in almost every application there happen to be lists of data items that are immutable, just for reference. It could be colors, the four seasons, continents, the states of your country, kinds or types of this and that. These items are almost like constants. As accessible as they are through the ordinary ActiveRecord API, [...]]]></description>
			<content:encoded><![CDATA[<p>Invariably, in almost every application there happen to be lists of data items that are immutable, just for reference. It could be colors, the four seasons, continents, the states of your country, kinds or types of this and that. These items are <em>almost</em> like constants. As accessible as they are through the ordinary ActiveRecord API, it seems an utter waste to hit the database again and again, for data that won&#8217;t change however often you request it.</p>
<p>Of course, the Rails community recognized early on that some data are more constant than others and over the years several plugins have been published that add cached and easily accessible enumerations to ActiveRecord. Some of these additions are quite complicated or befuddle ActiveRecord by not backing the enumeration values with real database objects. My experience has been that these attempted optimizations result in bizarre behavior when I did interesting things with ActiveRecord such as multiply nested named scopes plus custom SQL.</p>
<p>So, I thought a basic, no, simplistic, version of enumerations is called for. Here&#8217;s how it looks:</p>
<p><code>
<pre>
class Color &lt; ActiveRecord::Base
  enumerates do |e|
    e.value :name =&gt; 'red'
    e.value :name =&gt; 'green'
    e.value :name =&gt; 'blue'
  end
end

Color[:green]
Color.find_by_name('red')
Color.find_by_name!(:red)
Color.all
Color.count
Color.reload
</pre>
<p></code></p>
<p><code>
<pre>
e.value :name =&gt; 'red'
</pre>
<p></code></p>
<p>ensures that a Color object with name &#8216;red&#8217; exists, if it does not, one is created.</p>
<h2>Caveats</h2>
<p>Although there is a <code>#reload</code> method defined on enumeration models, i.e. <code>Color.reload</code>, it is very unwise to use it. The point is that this method only affects a single server process and you most likely have many of them.</p>
<p>So, if you need to change enumeration values, the only way to do it is to treat it like an update to your application code.</p>
<h2>Getting it</h2>
<ul>
<li><a href="http://github.com/mschuerig/easy_enums">github</a></li>
<li>$ sudo gem install mschuerig-easy_enums</li>
</ul>
<h2>Let your Rails app know about it</h2>
<p>In the appropriate place in <code>config/environment.rb</code> add</p>
<p><code>
<pre>
config.gem "mschuerig-easy_enums", :lib => 'easy_enums'
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2009/04/02/simplistic-enums/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lifting indexes with ActiveRecord</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2009/04/02/lifting-indexes/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2009/04/02/lifting-indexes/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 16:40:22 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.schuerig.de/michael/blog/?p=49</guid>
		<description><![CDATA[On a Rails project I&#8217;m currently working on I need to fill the database with test data to have something to play with. Apart from large imports, that&#8217;s the time when indexes may slow down operation severely instead of speeding things up. Consider: The indexes are not used, but have to be updated again and [...]]]></description>
			<content:encoded><![CDATA[<p>On a Rails project I&#8217;m currently working on I need to fill the database with test data to have something to play with. Apart from large imports, that&#8217;s the time when indexes may slow down operation severely instead of speeding things up. Consider: The indexes are not used, but have to be updated again and again for each new record that is inserted into the database. It is <em>much</em> cheaper, to <em>lift</em> &#8212; well, really <em>drop</em> &#8212; the indexes during mass operation and recreate them afterwards.</p>
<p>Here&#8217;s an example:</p>
<p><code>
<pre>
namespace :db do
  desc "Populate the database with sample data"
  task :populate =&gt; :environment do

    retained_indexes = [
      'index_people_on_lastname_and_firstname',
      { :table =&gt; :movies, :columns =&gt; :title }
      { :table =&gt; 'people', :columns =&gt; ['lastname', :firstname] }
    ]

    ActiveRecord::Base.transaction do
      IndexLifter.without_indexes(
        # Only consider indexes on these tables;
        # all tables by default.
        :movies,
        :people,
        # Don't lift these indexes
        :except =&gt; retained_indexes,
        # Don't lift unique indexes; default: false.
        :except_unique =&gt; true
      ) do
        ActiveRecord::Base.silence do

          # import or generate large amounts of data here

        end
      end
    end
  end
end
</pre>
<p></code></p>
<p>Please bear in mind that dropping and creating of indexes is a rather intrusive operation on the structure of your database. You should only perform it while no other users (or processes) are accessing it.</p>
<p>Also, consider that some indexes may be important for the proper function of your database. If you have <em>unique indexes</em>, i.e. indexes that enforce that particular columns or combinations of columns are unique, and if you are handling violations of this constraint in your application code, then you might need to retain these indexes even during data generation.</p>
<h2>Getting it</h2>
<ul>
<li><a href="http://github.com/mschuerig/index_lifter">github</a></li>
<li>$ sudo gem install mschuerig-index_lifter</li>
</ul>
<h2>Let your Rails app know about it</h2>
<p>In the appropriate place in <code>config/environment.rb</code> add</p>
<p><code>
<pre>
config.gem "mschuerig-index_lifter", :lib => 'index_lifter'
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2009/04/02/lifting-indexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash completion for script/generate</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2007/11/18/script-generate-bash-completion/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2007/11/18/script-generate-bash-completion/#comments</comments>
		<pubDate>Sun, 18 Nov 2007 00:45:50 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://schuerig.de/michael/blog/index.php/2007/11/18/script-generate-bash-completion/</guid>
		<description><![CDATA[The Rails generator script/generate knows pretty well what things it can generate. In fact, it knows much better than I do. So, I think it could really give me some help when I&#8217;m typing along on the command line. If you save the snippet below as /etc/bash_completion.d/generate you can enjoy this help, too. _generate() { [...]]]></description>
			<content:encoded><![CDATA[<p>The Rails generator <tt>script/generate</tt> knows pretty well what things it can generate. In fact, it knows much better than I do. So, I think it could really give me some help when I&#8217;m typing along on the command line.</p>
<p>If you save the snippet below as <tt>/etc/bash_completion.d/generate</tt> you can enjoy this help, too.</p>
<p><code>
<pre>
_generate()
{
  local cur

  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}

  if [ ! -d "$PWD/script" ]; then
    return 0
  fi

  if [ $COMP_CWORD == 1 ] &#038;&#038; [[ "$cur" == -* ]]; then
    COMPREPLY=( $( compgen -W '-h -v\\
      --help --version'\\
      -- $cur ))
    return 0
  fi

  if [ $COMP_CWORD == 2 ] &#038;&#038; [[ "$cur" == -* ]]; then
    COMPREPLY=( $( compgen -W '-p -f -s -q -t -c\\
      --pretend --force --skip --quiet --backtrace --svn'\\
      -- $cur ))
    return 0
  fi

  COMPREPLY=( $(script/generate --help | \\
    awk -F ': ' '/^  (Plugins|Rubygems|Builtin|User):/ { gsub(/, */, "\n", $2); print $2 }' | \\
    command grep "^$cur" \\
  ))
}

complete -F _generate $default generate
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2007/11/18/script-generate-bash-completion/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Testing with foreign key constraints</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2007/11/08/testing-with-foreign-key-constraints/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2007/11/08/testing-with-foreign-key-constraints/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 13:07:24 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://schuerig.de/michael/blog/index.php/2007/11/08/testing-with-foreign-key-constraints/</guid>
		<description><![CDATA[I&#8217;m not yet so enlightened that all of my Rails unit and functional tests run without accessing the database. Indeed, I&#8217;m still using YAML fixtures to populate the database for testing. I also insist on having foreign key constraints in the database, a thing that&#8217;s not exactly encouraged by Rails, but which is quite possible [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not yet so enlightened that all of my Rails unit and functional tests run without accessing the database. Indeed, I&#8217;m still using YAML fixtures to populate the database for testing.</p>
<p>I also insist on having foreign key constraints in the database, a thing that&#8217;s not exactly encouraged by Rails, but which is quite possible nonetheless. The various plugins from <a href="http://www.redhillonrails.org/">RedHill Consulting</a> are a big help.</p>
<p>But then, when you feel all warm and cosy due to the additional safety at the database-level, you&#8217;re suddenly trapped by a snag: Sooner or later you find that your fixtures contain dependencies among objects that preclude any attempt at clever ordering by violating one foreign key constraint or another. Fixture files are loaded one after another in their entirety and when an object in an earlier fixture refers to an object in a later fixture, the database aptly notices as an inconsistency.</p>
<p>Well, you may think, it <em>is</em> an inconsistency, but only a temporal one. After all the fixture files are loaded, everything is consistent again. That&#8217;s the clue. We need to tell the database that, yes, indeed, things may be inconsistent for a time, but we&#8217;ll be cleaning up, promise. The good thing is that there is even an SQL standard-compliant way to express this promise.</p>
<p><code>
<pre>
  START TRANSACTION
  SET CONSTRAINTS ALL DEFERRED
  COMMIT
</pre>
<p></code></p>
<p>If you use transactional fixtures, the transaction bracket is already provided by Rails, but there&#8217;s no pretty way to sneak in the <tt>"SET CONSTRAINTS ..."</tt> line. There are two ways of slightly different brutality. First, you can edit <tt>activerecord/lib/fixtures.rb</tt> and just insert the required line.</p>
<p><code>
<pre>
  def self.create_fixtures(fixtures_directory, table_names, class_names = {})
    ...
    connection.transaction(Thread.current['open_transactions'].to_i == 0) do
      # insert the following line
      connection.execute("SET CONSTRAINTS ALL DEFERRED")
      ...
    end
    ...
  end
</pre>
<p></code></p>
<p>Alternatively, you can overwrite the entire method in, say, <tt>&lt;railsapp&gt;/lib/transactional_fixture_loading_hack.rb</tt> like this</p>
<p><code>
<pre>
require 'active_record/fixtures'

Fixtures.class_eval do
  def self.create_fixtures(fixtures_directory, table_names, class_names = {})
    ...
    connection.transaction(Thread.current['open_transactions'].to_i == 0) do
      # inserted line
      connection.execute("SET CONSTRAINTS ALL DEFERRED")
      ...
    end
    ...
  end
</pre>
<p></code></p>
<p>Whatever you do, you&#8217;ll have to inspect your code whenever you update your Rails version.</p>
<p>We&#8217;re still not done, unfortunately. The database defers only those constraints that are <tt><em>deferrable</em></tt>. Have a look at the <a href="http://www.redhillonrails.org/#foreign_key_migrations">Foreign Key Migrations Plugin</a> for how to achieve this.</p>
<p>As a matter of convenience, I suggest that in your <tt>test_helper.rb</tt> you add a method that loads all your fixtures</p>
<p><code>
<pre>
class Test::Unit::TestCase
  self.use_transactional_fixtures = true

  def self.load_all_fixtures
    fixtures :users, :thingamajigs, :gadgets, :widgets
  end
end
</pre>
<p></code></p>
<p>Then, in a testcase class you can use it like this</p>
<p><code>
<pre>
require File.dirname(__FILE__) + '/../test_helper'

class ThingamajigTest < Test::Unit::TestCase
  load_all_fixtures

  ...
end
</pre>
<p></code></p>
<p>Note that with transactional fixtures this results in each fixture file loaded only once for all the tests.</p>
<p>So, there we are a last. Or those with a reasonable DBMS, I might say. For, of course, this technique is no use, if your database does not support deferrable constraints. PostgreSQL for one does support them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2007/11/08/testing-with-foreign-key-constraints/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>XML round-trip testing for Rails resources</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2007/02/27/xml-round-tripping/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2007/02/27/xml-round-tripping/#comments</comments>
		<pubDate>Tue, 27 Feb 2007 02:00:05 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://schuerig.de/michael/blog/index.php/2007/02/27/xml-round-tripping/</guid>
		<description><![CDATA[I&#8217;ve recently started to add XML support to a Rails application, meaning that the application provides data in XML format, if the request asks for it, and it understands XML data on create or update. To keep the application as well as myself sane, I&#8217;ve written a test that ensures the round trip of getting [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started to add XML support to a Rails application, meaning that the application provides data in XML format, if the request asks for it, and it understands XML data on create or update.</p>
<p>To keep the application as well as myself sane, I&#8217;ve written a test that ensures the round trip of getting XML and updating an object by sending XML works. This is only a very basic test and there surely is more that can and should be tested.</p>
<p><code>
<pre>
class XmlRoundtripTest &lt; ActionController::IntegrationTest

  RESOURCES = [
    :people, :things
  ]
  fixtures :stuff, *RESOURCES

  def self.assert_roundtrippability_for(*resources)
    resources.each do |resource|
      define_method("test_xml_roundtrip_for_#{resource}") do
        @user = user
        @user.logs_in

        xml = @user.gets_xml(resource)
        @user.changes_object_name!(xml)

        old_version = @user.extracts_lock_version(xml)
        @user.sends_xml(xml, resource)

        # Make sure that the resource was really updated
        new_version = @user.extracts_lock_version(@user.gets_xml(resource))
        assert_equal(old_version + 1, new_version)
      end
    end
  end

  assert_roundtrippability_for *RESOURCES

  def user
    open_session do |user|
      def user.gets_xml(resources, id = 1)
        get "/#{resources}/#{id}.xml"
        assert_response :success
        @response.body
      end

      def user.sends_xml(xml, resources, id = 1)
        put "/#{resources}/#{id}.xml", xml, :content_type =&gt; 'application/xml'
        assert_response :success
        @response.body
      end

      def user.changes_object_name!(xml)
        # arbitrarily change an attribute we know is there
        xml.gsub!(%r{&lt;name&gt;(.*?)&lt;/name&gt;}, '&lt;name&gt;X\1Y&lt;/name&gt;')
      end

      def user.extracts_lock_version(xml)
        xml =~ %r{&lt;lock-version&gt;(\d+)&lt;/lock-version&gt;}
        $1.to_i
      end
    end
  end
end
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2007/02/27/xml-round-tripping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return of the Tag Cloud &#8212; ajaxing the tagged</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2007/02/22/tag-cloud/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2007/02/22/tag-cloud/#comments</comments>
		<pubDate>Thu, 22 Feb 2007 22:10:28 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://schuerig.de/michael/blog/index.php/2007/02/22/tag-cloud/</guid>
		<description><![CDATA[On my continuing quest to become more Web-2.0-ish, I&#8217;ve tried my luck tilting at&#8230; well, tag clouds. To get an idea what I&#8217;m talking about, please have a look. These are the rudimentary basics to create a tag cloud like that. In the controller def tags @popular_tags = Tag.find_most_popular(:limit =&#62; 30) end In the view [...]]]></description>
			<content:encoded><![CDATA[<p>On my continuing quest to become more Web-2.0-ish, I&#8217;ve tried my luck tilting at&#8230; well, tag clouds. To get an idea what I&#8217;m talking about, please <a href="http://www.schuerig.de/michael/webdesign/demo/tagcloud.html">have a look</a>.</p>
<p>These are the rudimentary basics to create a tag cloud like that.</p>
<p>In the controller</p>
<p><code>
<pre>
def tags
  @popular_tags = Tag.find_most_popular(:limit =&gt; 30)
end
</pre>
<p></code></p>
<p>In the view</p>
<p><code>
<pre>
&lt;ul id="tags" class="tagcloud"&gt;
&lt;% @popular_tags.sort_by(&#038;:name).each do |t| -%&gt;
  &lt;%= weighted_tag t, tag_options %&gt;
&lt;% end -%&gt;
&lt;/ul&gt;
</pre>
<p></code></p>
<p>or, to get a somewhat nicer look, and to demonstrate some of the options</p>
<p><code>
<pre>
&lt;%
  tag_options = {
    :wrap =&gt; ['li', '&lt;span class="dlm"&gt;[&lt;/span&gt;', '&lt;span class="dlm"&gt;]&lt;/span&gt;'],
    :min_size =&gt; 100, :max_size =&gt; 500,
    :link_options =&gt; { :class =&gt; 'tag' })
  }
-%&gt;
&lt;h2&gt;Tags&lt;/h2&gt;
&lt;ul id="tags" class="tagcloud"&gt;
&lt;% @popular_tags.sort_by(&#038;:name).each do |t| -%&gt;
  &lt;%= weighted_tag t, tag_options %&gt;
&lt;% end -%&gt;
&lt;/ul&gt;
</pre>
<p></code></p>
<p><a href="http://schuerig.de/michael/blog/wp-content/uploads/2007/06/tag_cloud-0.0.2.tar.gz">Here you can download a do-it-yourself kit of all the pieces.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2007/02/22/tag-cloud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Popular Requests</title>
		<link>http://www.schuerig.de/michael/blog/index.php/2007/02/14/popular-requests/</link>
		<comments>http://www.schuerig.de/michael/blog/index.php/2007/02/14/popular-requests/#comments</comments>
		<pubDate>Tue, 13 Feb 2007 23:29:40 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://schuerig.de/michael/blog/index.php/2007/02/14/popular-requests/</guid>
		<description><![CDATA[It was late last night. And the night before, I admit it. I also admit that I don&#8217;t know much about what Web 2.0 is all about, but there are two things that I do know: rounded corners are involved and, of course tags and tag clouds. Tagging is work, therefore ipso facto to be [...]]]></description>
			<content:encoded><![CDATA[<p>It was late last night. And the night before, I admit it. I also admit that I don&#8217;t know much about what <em>Web 2.0</em> is all about, but there are two things that I do know: rounded corners are involved and, of course tags and tag clouds.</p>
<p>Tagging is work, therefore <a href="http://en.wikipedia.org/wiki/Ipso_facto"><em>ipso facto</em></a> to be avoided, better still: automated.</p>
<p>Let&#8217;s say you want to show people which part of your site are popular. How about a tag cloud of the most requested locations? Well, the first thing to do would be to collect which requests are happening at all. Also, you&#8217;d need to attach a sensible, displayable title to the raw request. For the sake of this little demonstration, I&#8217;m going to assume that in your template &#8212; <code>list.rhtml</code>, <code>edit.rhtml</code>, and brethren &#8212; you have an instance variable <code>@title</code>. It could be either defined in the template itself or transplanted there from its controller, it doesn&#8217;t matter.</p>
<p>Given all this, you could hypothetically write code like this.</p>
<p><code>
<pre>
class ApplicationController < ActionController::Base
  collect_popular_requests :max_age =&gt; 7.days, :max_count =&gt; 10000,
    :title =&gt; proc { |controller| controller.response.template.instance_variable_get(:@title) }
</pre>
<p></code></p>
<p>Therein further assuming that at most the 10000 most recent requests are kept if they are no older than 7 days. Now that we have hypothetically collected the data to base our tag cloud on, we have to display it somehow. Again, let's assume it could be done with a route like this</p>
<p><code>
<pre>
map.popular '/', :controller => 'most_popular', :action => 'index'
</pre>
<p></code></p>
<p>a controller like that</p>
<p><code>
<pre>
class MostPopularController < ApplicationController
  helper BoilerPlate::PopularRequestsHelper
  collect_popular_requests :\off # Ignore this place

  def index
    @popular_requests = popular_requests_list.most_popular(30)
  end
end
</pre>
<p></code></p>
<p>and for good measure a view (<code>index.rhtml</code>)</p>
<p><code>
<pre>
&lt;ul class="tagcloud"&gt;
&lt;% alpha_reqs = @popular_requests.sort_by(&#038;:title) -%&gt;
&lt;% alpha_reqs.each do |req| -%&gt;
  &lt;%= popular_request_tag(req, :wrap =&gt; 'li', :min_size =&gt; 30, :max_size =&gt; 400) %&gt;
&lt;% end -%&gt;
&lt;/ul&gt;
</pre>
<p></code></p>
<p>and some CSS for good style</p>
<p><code>
<pre>
.tagcloud {
  text-align: center;
  width: 70%;
}
.tagcloud li {
  display: inline-block;
  display: -moz-inline-box;
  white-space: nowrap;
  vertical-align: middle;
  line-height: 1.2em;
  padding: 0 0.2em;
}
</pre>
<p></code></p>
<p>Well, if you've made it this far, you might even consider doing all of this for real. For a good start, I suggest you download <a href="http://schuerig.de/michael/blog/wp-content/uploads/2007/02/popular_requests.tar.gz">this Rails plugin</a>, install it, create a database table with</p>
<p><code>
<pre>
$ script/generate popular_requests AddPopularRequests
$ rake db:migrate
</pre>
<p></code></p>
<p>and start playing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.schuerig.de/michael/blog/index.php/2007/02/14/popular-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

