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.
{width=”240”
height=”181”
style=”border: 2px solid #333;”}
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.
```ruby
# 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`.
```css
```ruby
# 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`
```bash
```text
# Include your application configuration below
require 'giraffe_actioncontroller_ext'
```text
That’s all there is to it. Now you can do fun things like…
```ruby
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.
```css
```ruby
# 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.
{width=”240”
height=”194”
style=”border: 2px solid #333;”}
Happy coding (and flying)!