The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Extending ActionController, part two

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Robby Russell

Posts: 981
Nickname: matchboy
Registered: Apr, 2005

Robby Russell is the Founder & Executive Director PLANET ARGON, a Ruby on Rails development firm
Extending ActionController, part two Posted: Feb 9, 2007 9:13 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Robby Russell.
Original Post: Extending ActionController, part two
Feed Title: Robby on Rails
Feed URL: http://feeds.feedburner.com/RobbyOnRails
Feed Description: Ruby on Rails development, consulting, and hosting from the trenches...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Robby Russell
Latest Posts From Robby on Rails

Advertisement

One of our consulting clients consists of a team of .NET developers that are rewriting a rather large product in Ruby on Rails. Every once in a while they have a problem that needs a second set of eyes to look over in order to find a solution with Rails. One of their developers recently asked how they could extend ActionController to provide all of their controllers with an action that would interact with a custom extension they built for ActiveRecord.

One of the few examples that he found to help them do this was a short blog post that I wrote nearly two years ago, titled, Extending ActionController. Given that I wouldn’t do it that way anymore, I felt that I’d quickly post an updated way of doing something similar.

Create Your Extension

This is when you get to take advantage of that lonely lib/ directory in your Rails application. We’ll go ahead and save our custom extension as lib/giraffe_actioncontroller_ext.rb. Now let’s put some code in there.

5

Looking at the following example, you’ll notice that we’re creating a basic Ruby module, which contains a method named, hot_air_balloon. Within that method, we can do just about anything that we’d normally do in an controller action.


# lib/giraffe_actioncontroller_ext.rb
module PlanetArgon
  module Giraffe
    # add your custom methods here
    def hot_air_balloon
      #
      # if some_condition_in_request?
          render :text => 'the giraffe left in a hot air balloon'
      #end
    end
  end
end

Great, however it’s not going to do anything yet. We need to wire our custom module into ActionController. To do this, let’t go ahead and place the following code at the bottom of lib/giraffe_controller.rb.


# include our custom module in ActionController::Base
ActionController::Base.class_eval do
  include PlanetArgon::Giraffe
end

Now that this file exists, we need to tell Rails about it.

Require Your Extension

You’ll want to update your environment configuration by adding the following to config/environment.rb


# Include your application configuration below

require 'giraffe_actioncontroller_ext' 

That’s all there is to it. Now you can do fun things like…


class ApplicationController < ActionController::Base
    before_filter :hot_air_balloon
    #...
end

Unhiding Actions

As I mentioned, our consulting client needed a handful of methods available to all controllers for use within actions, but they also wanted one method to be accessible via external requests. It turns out that all methods are, by default, hidden from the action processor. Basically, their names are stored in an array, named, hidden_actions. So, to remedy this, they were able to delete their action from the array.

A quick way to do this, is to update lib/giraffe_actioncontroller_ext.rb... like so.


# include our custom module in ActionController::Base
ActionController::Base.class_eval do
  include PlanetArgon::Giraffe
  hidden_actions.delete 'hot_air_balloon'
end

Now every controller in your application has an awesome hot_air_ballon action, which your giraffe friends can use to cruise the night skies in harmony.

8

Happy coding (and flying)!

1 Artwork and stories were created by several members of the PLANET ARGON team. I’m not sure why they killed me off in the first issue… or why they removed my thumbs in the story… only to discover hidden giraffes?

Read: Extending ActionController, part two

Topic: Is BDD kinkier than TDD? Previous Topic   Next Topic Topic: Rails Initialisation and you

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use