<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Tag DRY</title>
    <link>http://www.robbyonrails.com/articles/tag/dry</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Using MacPorts Ruby and Rails after Upgrading to OS X Leopard</title>
      <description>&lt;p&gt;If you previously followed my article, &lt;a href="http://www.robbyonrails.com/articles/2007/06/19/installing-ruby-on-rails-and-postgresql-on-os-x-second-edition"&gt;Installing Ruby on Rails and PostgreSQL on &lt;span class="caps"&gt;OS X&lt;/span&gt;, second edition&lt;/a&gt; and are now upgrading to &lt;span class="caps"&gt;OS X&lt;/span&gt; Leopard, you&amp;#8217;ll want to make a few adjustments to your setup.&lt;/p&gt;


	&lt;p&gt;First of all, it&amp;#8217;s great that Apple has decided to provide Ruby on Rails out of the box.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
~ &amp;gt; gem list rails                                                                                                                                                                   
  *** LOCAL GEMS ***

  rails (1.2.3)
      Web-application framework with template engine, control-flow layer,
      and ORM.
&lt;/code&gt;&lt;/pre&gt;

How many gems does it come with?
&lt;pre&gt;&lt;code&gt;
~ &amp;gt; gem list|grep '^[a-z]'|wc -l                                                                                                                                                     
      29
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;It&amp;#8217;s really great that &lt;a href="http://apple.com"&gt;Apple&lt;/a&gt; shipped Leopard pre-installed with 29 gems, especially if you don&amp;#8217;t have your entire Rails stack setup already. In my case and for those that have followed my installation process, you don&amp;#8217;t need to switch over to this new development stack (yet). I have a lot of time invested in my fully-functionaly MacPorts installation process (PostgreSQL, MySQL, RMagick, Subversion, Git, etc. Since this all working fine on my machine, I&amp;#8217;m not ready to make the switch to Apple&amp;#8217;s installation.&lt;/p&gt;


	&lt;h2&gt;Don&amp;#8217;t Fix it&amp;#8230; if it&amp;#8217;s not broken!&lt;/h2&gt;


	&lt;p&gt;So, the the first thing that I did was modify my &lt;code&gt;PATH&lt;/code&gt; environment variable, which has &lt;code&gt;/usr/bin&lt;/code&gt; as the first path that it&amp;#8217;ll look at when you try to run commands like &lt;code&gt;ruby&lt;/code&gt;, &lt;code&gt;mongrel_rails&lt;/code&gt;, &lt;code&gt;gem&lt;/code&gt;, etc. You&amp;#8217;ll want to modify this and prepend &lt;code&gt;/opt/local/bin:&lt;/code&gt; to the front of &lt;code&gt;PATH&lt;/code&gt; in your shell configuration. If you&amp;#8217;re using bash, this would be&amp;#8230; &lt;code&gt;~/.bashrc&lt;/code&gt;. If you&amp;#8217;re using zshell like me, &lt;code&gt;~/.zshrc&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Now, when you start a new Terminal and run &lt;code&gt;gem list&lt;/code&gt;, you&amp;#8217;ll see all of the gems that you already have installed.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
~ &amp;gt; gem list rails                                                                                                                                   &amp;lt; new-host

*** LOCAL GEMS ***

rails (1.2.5, 1.2.4, 1.2.3, 1.1.6)
    Web-application framework with template engine, control-flow layer,
    and ORM.
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Back to my happy gems&amp;#8230;&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
~ &amp;gt; gem list|grep '^[a-z]'|wc -l                                                                                                                                                              &amp;lt; new-host
      72
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Great! Now I can get back to work and spend time playing with the new features in Finder, Mail.app, and iChat instead of installing all of the software dependencies that our development projects have. :-)&lt;/p&gt;
</description>
      <pubDate>Sat, 27 Oct 2007 05:43:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4c96e766-87e4-41f4-9d7d-54fe826ed4e9</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/10/27/using-macports-ruby-and-rails-after-upgrading-to-os-x-leopard</link>
      <category>RubyURL</category>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>rails</category>
      <category>ruby</category>
      <category>gems</category>
      <category>rubyonrails</category>
      <category>osx</category>
      <category>DRY</category>
      <category>macports</category>
      <category>leopard</category>
      <category>zsh</category>
      <category>bash</category>
      <category>rmagick</category>
    </item>
    <item>
      <title>Q&amp;amp;A: ActiveRecord Observers and You</title>
      <description>&lt;p&gt;Yesterday, I wrote a short post titled, &lt;a href="http://www.robbyonrails.com/articles/2007/04/27/observers-big-and-small"&gt;Observers Big and Small&lt;/a&gt;, about using Observers in your Rails applications.&lt;/p&gt;


	&lt;p&gt;The following questions were raised in the comments.&lt;/p&gt;


	&lt;h2&gt;When should I use an Observer?&lt;/h2&gt;


	&lt;p&gt;&lt;a href="http://www.last100meters.com/"&gt;Eric Allam&lt;/a&gt; asks&amp;#8230;&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;&amp;#8220;Why not just use ActiveRecord callback hooks instead of Observers? Are Observers more powerful or is it just a matter of preference?&amp;#8221;&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Eric, this is an excellent question. I&amp;#8217;d say that a majority of the time, using the &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html"&gt;ActiveRecord callbacks&lt;/a&gt; in your models is going to work for your situation. However, there are times that you want the same methods to be called through callbacks. For example, let&amp;#8217;s take a recent problem that we used an observer to solve.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://blog.imperialdune.com/"&gt;Graeme&lt;/a&gt; is working on implementing &lt;a href="http://rubyforge.org/projects/ferret"&gt;Ferret&lt;/a&gt; into a project that we&amp;#8217;re developing for a client. With the use of Ferret, we can index and later search through content over several objects into a format that makes sense for our implementation goals. Each time an object is created and updated, we have to update our Ferret indexes to reflect these changes. The most obvious location that we can call our indexing methods is in each models&amp;#8217; callbacks, but this violates the &lt;span class="caps"&gt;DRY&lt;/span&gt;[1] principle. So, we created an Observer, which &lt;em&gt;observes&lt;/em&gt; each of the models that need these methods to be called. In fact, as far as we&amp;#8217;re concerned, the fact that we&amp;#8217;re indexing some of its data, is none of its business. We only want our models to be concerned with that they&amp;#8217;re designed to be concerned about. We may opt to change our indexing solution in the future and we&amp;#8217;d just need to rethink that at the Observer level and not change anything about the business logic in our models.&lt;/p&gt;


	&lt;p&gt;This is the sort of scenario when using an Observer makes great sense in your application.&lt;/p&gt;


	&lt;h2&gt;Logging from an Observer&lt;/h2&gt;


	&lt;p&gt;Adam R. asks&amp;#8230;&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;&amp;#8220;I&amp;#8217;d also like the ability to use the logger from within an observer, but that’s another issue.&amp;#8221;&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I assume that you are referring to the &lt;code&gt;logger&lt;/code&gt; method? I always forget to even use that method. I do know that the following works just fine in an Observer.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class IndexObserver &amp;lt; ActiveRecord::Observer
  observer Article, Editorial, BlogPost, ClassifiedAd

  def after_save(model)
    RAILS_DEFAULT_LOGGER.warn("Every single day. Every word you say. Every game you play. Every night you stay. I'll be watching you.")
    # execute something fun
  end
end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This will output to your log file without any problem.&lt;/p&gt;


	&lt;p&gt;This reminded me of when I used to want to &lt;a href="http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests"&gt;log from Unit Tests&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;(few minutes later)&lt;/p&gt;


	&lt;p&gt;Okay, I just attempted to use &lt;code&gt;logger&lt;/code&gt; from an Observer and you&amp;#8217;re right&amp;#8230; it doesn&amp;#8217;t currently work. There is a simple fix though, just extend ActiveRecord::Observer to add a &lt;code&gt;logger&lt;/code&gt; method like so and require it in &lt;code&gt;config/environment.rb&lt;/code&gt; (much like I did in with unit tests).&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# lib/observer_extensions.rb
class ActiveRecord::Observer
  def logger
    RAILS_DEFAULT_LOGGER
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This will give you a solution to that problem.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class FooObserver &amp;lt; ActiveRecord::Observer
  observer Foo

  def after_save(model)
    logger.warn("I wonder if the #{address.class} knows that I've been watching it all along?")
  end
end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Observers Spy for Us&lt;/h2&gt;


	&lt;p&gt;Most often, I look at Observers as being the guys that I hire to spy on my models. I don&amp;#8217;t want my models to know that they&amp;#8217;re being spied on and I&amp;#8217;d like to keep it that way. They don&amp;#8217;t solve all of our problems and it&amp;#8217;s easy to overuse them. However, I have found several cases that they made a lot of sense and most of those cases have been where we&amp;#8217;ve had the same things occurring in our model&amp;#8217;s callbacks.&lt;/p&gt;


	&lt;p&gt;If you have other questions related to Observers, feel free to let me know. If you&amp;#8217;re already using Observers, perhaps you could post a comment and/or blog post response with an example of when and how you use Observers in your Rails applications.&lt;/p&gt;


	&lt;h3&gt;Related Posts&lt;/h3&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.robbyonrails.com/articles/2007/04/27/observers-big-and-small"&gt;Observers Big and Small&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go"&gt;Where did my Observer go?&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://tom-eric.info/archive/2006/10/30/observers-in-ruby-on-rails"&gt;Observers in Ruby on Rails&lt;/a&gt;, Tom Eric&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Don&amp;#8217;t Repeat Yourself&lt;/p&gt;
</description>
      <pubDate>Sat, 28 Apr 2007 17:28:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:587732a9-50fb-476d-b46a-1c10d29c3559</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/04/28/q-a-activerecord-observers-and-you</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PLANET ARGON</category>
      <category>activerecord</category>
      <category>observers</category>
      <category>patterns</category>
      <category>q</category>
      <category>a</category>
      <category>logging</category>
      <category>ferret</category>
      <category>graeme</category>
      <category>DRY</category>
    </item>
    <item>
      <title>Do Your Views in Ruby on Rails need a cleaning service?</title>
      <description>&lt;p&gt;I&amp;#8217;ve been working on a project with &lt;a href="http://blog.imperialdune.com/"&gt;Graeme&lt;/a&gt;, and we&amp;#8217;re spending some time cleaning up some &lt;span class="caps"&gt;RHTML&lt;/span&gt; views.&lt;/p&gt;


	&lt;p&gt;He posted an article earlier, titled, &lt;a href="http://blog.imperialdune.com/2007/3/27/dirty-views-clean-them-up"&gt;Dirty Views? Clean them up!&lt;/a&gt;, where he asks the following.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&amp;#8220;I am also looking for more information on best practices with views in Rails. There doesn’t seem to be much information on the subject.&amp;#8221;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;We&amp;#8217;re starting to re-evaluate how we approach our views and are curious what other teams are doing&amp;#8230; especially if you have a team thats approx 1/2 designers&amp;#8230; 1/2 developers per project. We&amp;#8217;ll be reviewing some of the other options for the View layer over the coming week(s) and welcome any suggestions/insight to this area of Rails&amp;#8230; head over to &lt;a href="http://blog.imperialdune.com/"&gt;Graeme&amp;#8217;s blog&lt;/a&gt; and share your thoughts. :-)&lt;/p&gt;
</description>
      <pubDate>Tue, 27 Mar 2007 21:55:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:81794704-13ac-465b-8862-82e0188e844e</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/03/27/do-your-views-in-ruby-on-rails-need-a-cleaning-service</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PLANET ARGON</category>
      <category>views</category>
      <category>DRY</category>
      <category>rails</category>
      <category>rubyonrails</category>
      <category>best</category>
      <category>practices</category>
      <category>graeme</category>
      <category>design</category>
      <category>interface</category>
      <category>development</category>
    </item>
  </channel>
</rss>
