あすなろとダブルポストです。+最後の感想ちょこっと変更しました。
###まとめ
CtonrollerやActionを指定して処理を実行できます
after_contoroller_action
before_contoroller_action
ex. after_blog_save
→ BlogController#save終了後処理をする。
###流れをわかるためのもがき
- ぐぐってヒントを見つける。
[Rails][cache] Sweeper
http://d.hatena.ne.jp/meritdemerit/20070607/p2
なんとなくはわかるけど、やっぱりよくわからないのでソースを見ることにする。
- Rails Referenceで検索する。右上にファイルの場所があるので確認。
vendor/rails/actionpack/lib/action_controller/caching.rb
http://api.rubyonrails.org/classes/ActionController/Caching/Sweeping.html
ソースを読む:cache_sweeperキーワード
rails>>
def cache_sweeper(*sweepers)
return unless perform_caching
configuration = sweepers.extract_options!
sweepers.each do |sweeper|
ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
sweeper_instance = Object.const_get(Inflector.classify(sweeper)).instance
if sweeper_instance.is_a?(Sweeper)
around_filter(sweeper_instance, :only => configuration[:only])
else
after_filter(sweeper_instance, :only => configuration[:only])
end
end
end
<<--
ソースを読む:after処理のエントリポイント
rails>>
def after(controller)
callback(:after)
Clean up, so that the controller can be collected after this request
self.controller = nil
end
<<--
ソースを読む:after_controller_action処理の呼び出し
rails>>
def callback(timing)
controller_callback_method_name = "#{ timing}#{controller.controller_name.underscore}"
action_callback_method_name = "#{controller_callback_method_name}#{controller.action_name}"
send!(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
send!(action_callback_method_name) if respond_to?(action_callback_method_name, true)
end
<<--
最後の、"send!(action...:自分にメソッドが定義されていたら送る"ってところが実際の呼び出し。
愛が足りないながんばろ。