Railsのコントローラの処理を共通化してみる

RailsでHello, world!を表示してみるの続き

Railsのコントローラの処理をいろいろ共通化してみる。


とりあえず、前回作ったSampleControllerにログを出力するよう修正してみる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    logger.debug("SampleController#index開始")
    logger.debug("SampleController#index終了")
  end
end

アクセスすると、ログには下記のように出力される。

Processing by SampleController#index as HTML
SampleController#index 開始
SampleController#index 終了
  Rendered sample/index.html.erb within layouts/application (1.5ms)

before_action/after_action

before_actionとafter_actionを定義してみる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController

  before_action :before_action_test
  after_action :after_action_test

  def index
    logger.debug("SampleController#index 開始")
    logger.debug("SampleController#index 終了")
  end

  private

    def before_action_test
      logger.debug("SampleController#before_action_test 開始")
      logger.debug("SampleController#before_action_test 終了")
    end

    def after_action_test
      logger.debug("SampleController#after_action_test 開始")
      logger.debug("SampleController#after_action_test 終了")
    end

end

アクセスすると、ログには下記のように出力される。

Processing by SampleController#index as HTML
SampleController#before_action_test 開始
SampleController#before_action_test 終了
SampleController#index 開始
SampleController#index 終了
  Rendered sample/index.html.erb within layouts/application (1.5ms)
SampleController#after_action_test 開始
SampleController#after_action_test 終了

ApplicationController

ApplicationControllerにbefore_actionとafter_actionを定義してみる。

$ vi app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :before_action_test
  after_action :after_action_test

  private

    def before_action_test
      logger.debug("ApplicationController#before_action_test 開始")
      logger.debug("ApplicationController#before_action_test 終了")
    end

    def after_action_test
      logger.debug("ApplicationController#after_action_test 開始")
      logger.debug("ApplicationController#after_action_test 終了")
    end

end

SampleControllerからはsuperで親クラスのメソッドを呼ぶようにする。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController

  def index
    logger.debug("SampleController#index 開始")
    logger.debug("SampleController#index 終了")
  end

  private

    def before_action_test
      logger.debug("SampleController#before_action_test 開始")
      super
      logger.debug("SampleController#before_action_test 終了")
    end

    def after_action_test
      logger.debug("SampleController#after_action_test 開始")
      super
      logger.debug("SampleController#after_action_test 終了")
    end

end

アクセスすると、ログには下記のように出力される。

Processing by SampleController#index as HTML
SampleController#before_action_test 開始
ApplicationController#before_action_test 開始
ApplicationController#before_action_test 終了
SampleController#before_action_test 終了
SampleController#index 開始
SampleController#index 終了
  Rendered sample/index.html.erb within layouts/application (1.5ms)
SampleController#after_action_test 開始
ApplicationController#after_action_test 開始
ApplicationController#after_action_test 終了
SampleController#after_action_test 終了

モジュール

モジュールを作ってみる。

$ vi app/controllers/concerns/module_test.rb
module ModuleTest
  def hoge
      logger.debug("ModuleTest#hoge 開始")
      logger.debug("ModuleTest#hoge 終了")
  end
end

モジュールをSampleControllerのindexメソッドの中で呼ぶようにする。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController

  include ModuleTest

  def index
    logger.debug("SampleController#index 開始")
    hoge
    logger.debug("SampleController#index 終了")
  end

  private

    def before_action_test
      logger.debug("SampleController#before_action_test 開始")
      super
      logger.debug("SampleController#before_action_test 終了")
    end

    def after_action_test
      logger.debug("SampleController#after_action_test 開始")
      super
      logger.debug("SampleController#after_action_test 終了")
    end

end

アクセスすると、ログには下記のように出力される。

Processing by SampleController#index as HTML
SampleController#before_action_test 開始
ApplicationController#before_action_test 開始
ApplicationController#before_action_test 終了
SampleController#before_action_test 終了
SampleController#index 開始
ModuleTest#hoge 開始
ModuleTest#hoge 終了
SampleController#index 終了
  Rendered sample/index.html.erb within layouts/application (0.1ms)
SampleController#after_action_test 開始
ApplicationController#after_action_test 開始
ApplicationController#after_action_test 終了
SampleController#after_action_test 終了