@awead

Technical notes and explications of code

Filters, Not Overrides

When refactoring some controllers, I realized it’s much nicer to filter actions on controllers instead of overriding them.

Take for example a controller that exists somewhere in your application or in a gem:

1
2
3
4
5
6
7
8
class BaseController < ApplicationController

  def index
    @terms = Terms.all
    render_all_terms
  end

end

I need to use that controller in my application, but have to add some additional stuff to it to use in my views:

1
2
3
4
5
6
7
8
9
10
class MyController < ApplicationController

  include BaseController

  def  index
    @my_terms = MyTerms.all
    super
  end

end

This will work and I’ll have both @terms and @my_terms in my views. However, I find it’s nicer, and a little bit less invasive, if I can work around BaseController without having override it:

1
2
3
4
5
6
7
8
9
10
11
class MyController < ApplicationController

  include BaseController

  before_filter :get_my_terms, only: :index

  def get_my_terms
    @my_terms = MyTerms.all
  end

end

The end result is the same, but I’ve accomplished it without having to change BaseController at all, thereby leaving its public interface untouched.