Read my latest article: Announcing RailsDeveloper (posted Wed, 01 Sep 2010 17:01:00 GMT)

Extending ActionController, part two

Posted by Robby Russell Fri, 09 Feb 2007 14:57:00 GMT

27 comments Latest by zhang Sun, 29 Aug 2010 05:21:23 GMT

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?

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Justin Fri, 09 Feb 2007 16:38:43 GMT

    It’s good to know that people are looking out for the giraffes.

  2. Avatar
    Robby Russell Fri, 09 Feb 2007 16:56:21 GMT Recommend me on Working with Rails

    Justin,

    It’s good to know that people are looking out for the giraffes.

    Yeah, we felt that there was a void in the community. We’re trying to get them out of my thumbs all the time.

  3. Avatar
    Luke Redpath Fri, 09 Feb 2007 17:13:27 GMT

    Robby, is there any reason why you use class_eval instead of just using send?

    ActionController::Base.send(:include, YourModule)
  4. Avatar
    Robby Russell Fri, 09 Feb 2007 18:04:32 GMT Recommend me on Working with Rails

    Luke,

    I’m not sure that the giraffes would appreciate being sent there. Rumors suggest that they’d prefer to just get picked up when necessary. I’ll discuss with my comrades of thumbs and get back to you on that.

  5. Avatar
    Niklas Sun, 11 Feb 2007 16:08:45 GMT

    I think there is a little typo. You mentionied lib/giraffe_controller.rb only once, I think there is a _ext missing?

    Why do giraffes wear boots?

  6. Avatar
    Ronjrc Tue, 14 Aug 2007 13:31:32 GMT

    http://adbrgbbffrsba.host.com desk3 [url=http://adbsgbbffrsba.host.com]desk4[/url] [link=http://adbagbbffrsba.host.com]desk6[/link]

  7. Avatar
    wholesale ed hardy Wed, 17 Mar 2010 07:16:52 GMT
  8. Avatar
    Bailey Button uggs Mon, 24 May 2010 08:36:06 GMT

    fdg

  9. Avatar
    AdiSTAR Ride Sat, 29 May 2010 09:26:24 GMT

    trytr

  10. Avatar
    air jordan 2 Mon, 31 May 2010 06:09:53 GMT

    ret

  11. Avatar
    air jordan I Wed, 02 Jun 2010 03:54:47 GMT

    TYU TY U

  12. Avatar
    air jordan retro Wed, 02 Jun 2010 03:54:59 GMT

    TYU TYU

  13. Avatar
    air jordan retro Wed, 02 Jun 2010 03:54:59 GMT

    TYU TYU

  14. Avatar
    2010 germany 16 lahm Sat, 05 Jun 2010 02:18:50 GMT

    etert

  15. Avatar
    mbt chapa ebony red Sat, 05 Jun 2010 02:19:00 GMT

    rt yr

  16. Avatar
    designer handbags reviews Fri, 18 Jun 2010 01:23:52 GMT

    I’ll discuss with my comrades of thumbs and get back to you on that.

  17. Avatar
    hermes handbags Mon, 28 Jun 2010 00:36:15 GMT

    For those that wish to acquirement a hermes , it can be rather difficult. If you aren’t a affiliate of the rich, the famous,a appropriate hermes belt . One nice allotment is that they agreement the actuality of the hermes wallet . It’s a good idea to sketch out the structure of the DEAN MARTIN CELEBRITY ROASTS with paper and pencil. Proprietary DEAN MARTIN CELEBRITY ROASTS DVD tools offer GUI systems for creating this type of structure, but no such tools are available yet for DEAN MARTIN CELEBRITY ROASTS DVD COLLECTION production on Linux.

  18. Avatar
    VVVVVVV Mon, 28 Jun 2010 03:33:23 GMT
  19. Avatar
    air jordan 11 Wed, 30 Jun 2010 09:13:29 GMT

    Demonstrate a unique new conceptjordan shoesAwA356

  20. Avatar
    asics running shoesa Thu, 22 Jul 2010 10:39:39 GMT

    This was a useful post and I think it is rather easy to see from the other comments as well that this post is well written and useful. Keep up the good work asics gel kinsei 2 review asics gel kinsei 2 mens asics gel kinsei sale asics gel kinsei 2 men’s shoes Onitsuka Tiger Mexico 66 Beige Navy Onitsuka Tiger Mexico 66 Black Gold

  21. Avatar
    ugg boots Mon, 26 Jul 2010 09:57:22 GMT
  22. Avatar
    ugg boots Mon, 26 Jul 2010 09:57:40 GMT
  23. Avatar
    replica watches Mon, 02 Aug 2010 07:27:59 GMT

    NICE!

  24. Avatar
    Christian Louboutin Pumps Tue, 03 Aug 2010 01:10:56 GMT

    Perfect!

  25. Avatar
    Adidas Shoes Tue, 03 Aug 2010 01:11:35 GMT

    good article,nice post

  26. Avatar
    http://www.bestretroshoes.com Fri, 27 Aug 2010 02:48:33 GMT
    air jordan retro 6,air jordan retro 6
    air jordan retro 16,air jordan retro 16
    Air Jordan Jumpman Team Pro,Air Jordan Jumpman Team Pro
    Air Jordan Fusion 4,Air Jordan Fusion 4
    nike ajf 13,nike ajf 13
    Air Jordan 12,Air Jordan 12
    Air Jordan 13,Air Jordan 13
    Air Jordan 14,Air Jordan 14
    Air Jordan 15,Air Jordan 15
    Air Jordan 22,Air Jordan 22
  27. Avatar
    zhang Sun, 29 Aug 2010 05:21:23 GMT

Share your thoughts... (really...I want to hear them)

Comments