class SomeController < ApplicationController
before_action :set_adwords_client
private
def set_adwords_client
@hoge = Hoge.find(params[:id])
add_account_access_history(@hoge.id)
end
end
class ApplicationController < ActionController::Base
ACCESS_HISTORY_MAX_LENGTH = 5
def add_account_access_history(id)
hoge = Hoge.find(id)
return unless hoge
# 履歴先頭に追加
account_access_histories.unshift(hoge.id)
# 重複排除
account_access_histories.uniq!
# 設定値以上の履歴が残っていたら削除
if account_access_histories.size > ACCESS_HISTORY_MAX_LENGTH
account_access_histories.slice!(0..ACCESS_HISTORY_MAX_LENGTH - 1)
end
end
private
def account_access_histories
session[:account_access_history] ||= []
end
end