<?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>Engine Yard Blog &#187; Andre Arko</title>
	<atom:link href="http://www.engineyard.com/blog/author/andrearko/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.engineyard.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 01:49:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Extending Rails 3 with Railties</title>
		<link>http://www.engineyard.com/blog/2010/extending-rails-3-with-railties/</link>
		<comments>http://www.engineyard.com/blog/2010/extending-rails-3-with-railties/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 11:49:55 +0000</pubDate>
		<dc:creator>Andre Arko</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Rails 3]]></category>
		<category><![CDATA[Railties]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=4752</guid>
		<description><![CDATA[<div class="note">This post comes from guest community contributor and Engine Yard alumni Andre Arko. Andre has been building web applications with Ruby and Rails for five years, and is a member of the Bundler core team. He works for <a href="http://plexapp.com/">Plex</a>, tweets as <a href="http://twitter.com/indirect">@indirect</a>, and blogs at <a href="http://andre.arko.net/">andre.arko.net</a>.</div>
<h2>Gem plugins in Rails 3.0</h2>
<p>Rails 3.0 is finally released, and with it comes a fantastic new way to extend Rails: Railties. Railties are the basis of the core components of Rails 3, and are the result of months of careful refactoring by Carlhuda. It is easier to extend and expand Rails than it has ever been before, without an <code>alias_method_chain</code> in sight.</p>
<p>Unfortunately, while the system for extending and expanding Rails has been completely overhauled, the documentation hasn't been updated yet. The <a href="http://guides.rubyonrails.org/plugins.html">Rails Plugins Guide</a> only covers writing plugins in the old Rails 2 style. Ilya Grigorik wrote a <a href="http://www.igvita.com/2010/08/04/rails-3-internals-railtie-creating-plugins/">Railtie &amp; Creating Plugins</a> blog post, but just scratched the surface of what is possible with a Railtie plugin. This post covers writing Railtie plugins, hooking into the Rails initialization process, packaging Railtie plugins as gems, and using gem plugins in a Rails 3 application.</p>
<h2>Creating Railtie plugins</h2>
<p>Creating a Railtie is easy. Just create a class that inherits from <a href="http://api.rubyonrails.org/classes/Rails/Railtie.html"><code>::Rails::Railtie</code></a>. Every subclass of Railtie is used to initialize your Rails application. Since ActionController, ActionView, and the other Rails components are also Railties, your plugin can function as a first-class member of the Rails application. It will have access to the same methods and context that are used by the official Rails components. Here is a sample minimal Railtie that will be loaded when your Rails application boots.</p>
<pre lang="ruby" escaped="true">require 'rails'
class MyCoolRailtie &lt; Rails::Railtie
  # railtie code goes here
end</pre>
<p>The <a href="http://api.rubyonrails.org/classes/Rails/Railtie.html">Railtie documentation</a> lists all of the methods that are available inside each Railtie class, but doesn't really go into depth about what you can use Railties to do. Here are some example Railties explaining how to use the Railtie methods (in alphabetical order) to customize and extend Rails.</p>
<h3>console</h3>
<p>The <code>console</code> method allows your Railtie to add code that will be run when a Rails console is started.</p>
<pre lang="ruby" escaped="true">console do
  Foo.console_mode!
end</pre>
<h3>generators</h3>
<p>Rails will require any generators defined in <code>lib/generators/*.rb</code> automatically. If you ship <a href="http://api.rubyonrails.org/classes/Rails/Generators.html">Rails::Generators</a> with your Railtie in some other directory, you can require them using this method.</p>
<pre lang="ruby" escaped="true">generators do
  require 'path/to/generator'
end</pre>
<h3>rake_tasks</h3>
<p>If you ship rake tasks for apps with your Railtie, load them using this method.</p>
<pre lang="ruby" escaped="true">rake_tasks do
  require 'path/to/railtie.tasks'
end</pre>
<h3>initializer</h3>
<p>The <code>initializer</code> method provides Railties with a lot of power. They create initializers that will be run during the Rails boot process, like the files put into <code>config/initializers</code> in the app directory. The initializer method takes two options, <code>:after</code> or <code>:before</code>, if there are specific initializers that you want to run before or after yours.</p>
<pre lang="ruby" escaped="true">initializer "my_cool_railtie.boot_foo" do
  Foo.boot(Bar)
end

initializer "my_cool_railtie.boot_bar",
  :before =&gt; "my_cool_railtie.boot_foo" do
    Bar.boot!
end</pre>
<h2>Rails configuration hooks</h2>
<p>The biggest extension hook that Railties provide is somewhat unassuming: the <code>config</code> method. That method returns the instance of <a href="http://api.rubyonrails.org/classes/Rails/Railtie/Configuration.html"><code>Railtie::Configuration</code></a> that belongs to the application being booted. This opens up all sorts of interesting possibilities, since the <code>config</code> object is the same one that is made available inside a Rails application's <code>environment.rb</code> file. Here are some annotated examples of using <code>config</code> to change how a Rails application is initialized and configured.</p>
<h3>after_initialize</h3>
<p>This method takes a block that will be run after Rails is is completely initialized, and all of the application's initializers have run.</p>
<h3>app_middleware</h3>
<p>This method exposes the <a href="http://api.rubyonrails.org/classes/ActionDispatch/MiddlewareStack.html">MiddlewareStack</a> that will be used to handle requests to your Rails application. You can use any of the methods defined on MiddlewareStack, including <code>use</code> and <code>swap</code>, to manage the Rails application's Rack middlewares. For example, if your Railtie included the Rack middleware <code>MyRailtie::Middleware</code>, you could add it to the Rails application middleware stack like this:</p>
<pre lang="ruby" escaped="true">config.middlewares.use MyRailtie::Middleware</pre>
<h3>before_configuration</h3>
<p>Code passed in a block to this method will be run immediately before the application configuration block inside <code>application.rb</code> is run. This is usually the best place to set default options that users of your plugin should be able to override themselves, as in the <code>jquery-rails</code> example below.</p>
<h3>before_eager_load</h3>
<p>The block passed to <code>before_eager_load</code> will be run before Rails requires the application's classes. Eager load is never run in development mode. However, if you need to run code after Rails loads, but before any application code loads, this is the place to put it.</p>
<pre lang="ruby" escaped="true">config.before_eager_load do
  SomeClass.set_important_value = "RoboCop"
end</pre>
<h3>before_initialize</h3>
<p>This method takes a block to be run before the Rails initialization process happens -- this is basically equivalent to creating an initializer, and setting it to run before the first initializer the app has.</p>
<h3>generators</h3>
<p>This object holds the configuration for the generators that are invoked when you run the <code>rails generate</code> command.</p>
<pre lang="ruby" escaped="true">config.generators do |g|
  g.orm             :datamapper, :migration =&gt; true
  g.template_engine :haml
  g.test_framework  :rspec
end</pre>
<p>You can also use it to disable colorized logging in the console.</p>
<pre lang="ruby" escaped="true">config.generators.colorize_logging = false</pre>
<h3>to_prepare</h3>
<p>Last, but quite importantly, <code>to_prepare</code> allows you to do one-time setup. The block you pass to this method will run for every request in development mode, but only once in production. Use it when you need to set something up once before the app starts serving requests.</p>
<h2>Examples</h2>
<p>At this point, you're probably thinking "why would I actually want to do any of that stuff?". So, here are a few select examples of Railtie plugins packaged as gems.</p>
<h3><a href="http://github.com/rspec/rspec-rails">rspec-rails</a></h3>
<p>The rspec-rails plugin ships with a set of rake tasks and generators that integrate the <a href="http://github.com/rspec/rspec">RSpec</a> gem with Rails.</p>
<pre lang="ruby" escaped="true">module RSpec
  module Rails
    class Railtie &lt; ::Rails::Railtie
      config.generators.integration_tool :rspec
      config.generators.test_framework   :rspec

      rake_tasks do
        load "rspec/rails/tasks/rspec.rake"
      end
    end
  end
end</pre>
<p>This Railtie just does three things: First, it sets the generators that will be used for integration tests via the <code>integration_tool</code> method. Next, it sets the generators that will be used to generate model, controller, and view tests (via the <code>test_framework</code> method). Last, it loads the RSpec rake tasks to run RSpec tests instead of test-unit tests.</p>
<h3><a href="http://github.com/indirect/jquery-rails">jquery-rails</a></h3>
<p>The jquery-rails plugin ships with a generator that downloads and installs jQuery, the jquery-ujs script that enables Rails helpers with jQuery, and optionally installs jQueryUI as well.</p>
<pre lang="ruby" escaped="true">module Jquery
  module Rails
    class Railtie &lt; ::Rails::Railtie
      config.before_configuration do
        if ::Rails.root.join('public/javascripts/jquery-ui.min.js').exist?
          config.action_view.javascript_expansions[:defaults] =
            %w(jquery.min jquery-ui.min rails)
        else
          config.action_view.javascript_expansions[:defaults] =
            %w(jquery.min rails)
        end
      end
    end
  end
end</pre>
<p>This Railtie only sets one setting, but checks for the jQueryUI library to determine the value to set. By using the <code>config.before_configuration</code> hook, it runs right before the <code>application.rb</code> config block runs. That means it has access to the Rails.root, which is needed to check for jQueryUI, and it means that users can still override <code>javascript_expansion[:defaults]</code> in their <code>application.rb</code> if they want something different than the new defaults that the plugin provides.</p>
<h3><a href="http://github.com/indirect/haml-rails">haml-rails</a></h3>
<p>The haml-rails gem provides generators for views written in Haml instead of the default generated views that are written in ERB.</p>
<pre lang="ruby" escaped="true">module Haml
  module Rails
    class Railtie &lt; ::Rails::Railtie
      config.generators.template_engine :haml

      config.before_initialize do
        Haml.init_rails(binding)
        Haml::Template.options[:format] = :html5
      end
    end
  end
end</pre>
<p>This Railtie simply changes the template engine that Rails invokes when you run <code>rails generate</code>, and then initializes Haml for Rails, and sets the Haml output format to HTML5.</p>
<h2>Packaging up gem plugins</h2>
<p>Railtie plugins are easy to turn into gem plugins for Rails. This makes them easy to distribute, manage, and upgrade. The first thing you need is a gem. If you don't have a gem yet, you can create a new gem easily using <a href="http://gembundler.com">Bundler</a>. Just run <code>bundle gem my_new_gem</code> and Bundler will generate a skeleton gem and gemspec that follow gem best practices. Once you have a gem, just make sure that your Railtie subclass is defined when <code>lib/my_new_gem.rb</code> is loaded. You can define the Railtie in a separate file and require that file, or define it directly. Last, add a dependency on the Rails gem (~&gt;3.0) to your gemspec.</p>
<p>If your gem is also a plain Ruby library, and you don't want to depend on the Rails gem, then you can put your Railtie in a separate file, and conditionally require that file inside your main library file.</p>
<pre lang="ruby" escaped="true"># lib/my_new_gem/my_cool_railtie.rb
module MyNewGem
  class MyCoolRailtie &lt; ::Rails::Railtie
    # Railtie code here
  end
end</pre>
<pre lang="ruby" escaped="true"># lib/my_new_gem.rb
require 'my_new_gem/my_cool_railtie.rb' if defined?(Rails)</pre>
<p>This ensures that your gem can be loaded (without the Railtie) if it is loaded outside the context of a Rails application.</p>
<p>Now that your gem has a Railtie, you can build it and release it to <a href="http://gemcutter.org">Gemcutter</a>. Once your gem is on Gemcutter, using it with Rails 3 applications is extremely easy -- just add the gem to your <code>Gemfile</code>. Bundler will download and install your gem when you run <code>bundle install</code>, Rails will load it, and the <code>Rails::Railtie</code> class takes care of the rest!
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2010/extending-rails-3-with-railties/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Homebrew: OS X&#8217;s Missing Package Manager</title>
		<link>http://www.engineyard.com/blog/2010/homebrew-os-xs-missing-package-manager/</link>
		<comments>http://www.engineyard.com/blog/2010/homebrew-os-xs-missing-package-manager/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 18:00:22 +0000</pubDate>
		<dc:creator>Andre Arko</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Homebrew]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=3262</guid>
		<description><![CDATA[<p>Managing software packages on Unix has always been, to put it politely, a giant pain, and most Linux distributions are built around the different ways we've all been trying to alleviate that pain. In this post, I'll walk you through <a href="http://github.com/mxcl/homebrew">Homebrew</a>, a fantastic new option for package management made simple.</p>
<p>Pre-Homebrew, there were various attempts to create effective package managers for OS X. The two most popular efforts were <a href="http://finkproject.org">Fink</a> and <a href="http://macports.org">MacPorts</a>, but they each had their frustrations. In both cases, creating packages or portfiles was <em>still </em>complex and difficult.</p>
<p><a href="http://www.methylblue.com/">Max Howell</a>'s done a great job with Homebrew; it's easy to edit, and creating new packages is a breeze. Let's dig in!</p>
<h2>What Does It Do?</h2>
<p>The pitch is simple: Homebrew alleviates the drudgery and repetition of downloading and installing Unix software packages on OS X. If you're sick of <code>./configure &amp;&amp; make &amp;&amp; make install</code>, Homebrew can help.</p>
<h2>Why Homebrew?</h2>
<p>As previously mentioned, OS X already has two package managers: <a href="http://finkproject.org">Fink</a> and <a href="http://macports.org">MacPorts</a>. If one of those is working for you, great. But if you've been frustrated by them in the past, I strongly suggest you give Homebrew a try. It's easy to create and edit formulae, and even to edit Homebrew <em>itself</em>, since the core is just a few hundred lines of Ruby code.</p>
<p>It doesn't impose external structure on you: the default is to install it to <code>/usr/local</code>, but you can install it anywhere. Inside your Homebrew directory, software is installed in subdirectories inside Homebrew's <em>cellar</em>, like <code>Cellar/git/1.6.5.4/</code>. After installation, Homebrew symlinks the software into the regular Unix directories. If you want to hand-install a package or version that isn't officially part of Homebrew yet, it can happily coexist in the same location.</p>
<p>That's usually not necessary, though, since formulae can install directly from version control. If a package has a public git, svn, cvs, or mercurial repository, you can install the latest development version as often as you'd like with a simple <code>brew install</code>.</p>
<p>Installing packages is faster, too, because Homebrew also works hard to avoid package duplication. No more installing <em>yet another</em> version of Perl as a package dependency when you already have a <em>working</em> install of Perl built into OS X. Best of all, Homebrew has a basic philosophy that you shouldn't have to use sudo to install or manage software on your computer.</p>
<h2>Sounds Pretty Great... How Do I Get It?</h2>
<p>The first (and only) dependency that Homebrew has is the <a href="http://developer.apple.com/tools/">OS X Developer Tools</a>, which are on the OS X installer disc, and available from Apple as a free download.</p>
<p>Unless you have a reason not to, the easiest place to install Homebrew is in <code>/usr/local</code>. You can do that in just a few steps on the command line:</p>
<pre escaped="true"># Take ownership of /usr/local so you don't have to sudo
sudo chown -R `whoami` /usr/local
# Fix the permissions on your mysql installation, if you have one
sudo chown -R mysql:mysql /usr/local/mysql*
# Download and install Homebrew from github
curl -L http://github.com/mxcl/homebrew/tarball/master | tar xz --strip 1 -C /usr/local</pre>
<p>Once you've done that, you're good to go! Assuming <code>/usr/local/bin</code> is in your PATH, feel free to try it out:</p>
<pre escaped="true">brew install wget
brew info git</pre>
<p>The Homebrew wiki also has more about <a href="http://github.com/mxcl/homebrew/wiki/Gems,-Eggs-and-Perl-Modules">integrating with RubyGems, CPAN, and Python's EasyInstall</a>.</p>
<p>Keeping your copy of Homebrew up to date is easy, too:</p>
<pre escaped="true">brew install git
brew update</pre>
<p>Once you have git installed, you can just run <code>brew update</code> any time you want to pull down the latest formulae.</p>
<h2>Contributing</h2>
<p>Creating a new formula is almost that easy. If Homebrew didn't have a formula for wget, you could create one like this:</p>
<pre escaped="true">brew create http://ftp.gnu.org/gnu/wget/wget-1.12.tar.bz2</pre>
<p>After you save your formula, you can test it out with <code>brew install -vd wget</code>, to enable verbose logging and debug mode. If you need help getting your formula working, there's more documentation on the <a href="http://wiki.github.com/mxcl/homebrew/contributing">Homebrew wiki</a>. You can also learn by example from already existing formula, like <a href="http://github.com/mxcl/homebrew/tree/master/Library/Formula/git.rb">git</a> or <a href="http://github.com/mxcl/homebrew/tree/master/Library/Formula/flac.rb">flac</a>.</p>
<p>You can check out lots of example formulae, as well as the internals of Homebrew, by running <code>brew edit</code>. The code is pretty straightforward. If you have questions, or are interested in future plans, the contributors to Homebrew tend to hang out in the #machomebrew channel on Freenode.</p>
<p>Once you have a working new formula, it's easy to create your own fork of Homebrew on GitHub to push your new formula to, by using the github gem:</p>
<pre escaped="true">git add .
git commit -m "Added a formula for wget"
gem install json github
github fork
git push &lt;your github username&gt; mastergitx</pre>
<p>After pushing your change to GitHub, go to the <a href="http://github.com/mxcl/homebrew/issues">Homebrew issue tracker</a> and create a ticket with the subject "New formula: ". Assuming everything checks out, your formula will be added to the main Homebrew repository and available for everyone else to use.</p>
<h2>Wrapping Up</h2>
<p>Homebrew is a compelling alternative to MacPorts and Fink. The Homebrew core and all the formulae are written in Ruby, so it's easy to add new packages or even new features. If you're looking for more control over the Unix software you have installed on your Mac, or you've been frustrated by other package managers in the past, check it out. I think you'll be happily surprised.
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2010/homebrew-os-xs-missing-package-manager/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
	</channel>
</rss>

