• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

あすなろとダブルポストです。+最後の感想ちょこっと変更しました。

まとめ

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キーワード

       1    def cache_sweeper(*sweepers)
       2      return unless perform_caching
       3      configuration = sweepers.extract_options!
       4      sweepers.each do |sweeper|
       5        ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
       6        sweeper_instance = Object.const_get(Inflector.classify(sweeper)).instance
       7        if sweeper_instance.is_a?(Sweeper) 
       8         around_filter(sweeper_instance, :only => configuration[:only])
       9        else
      10          after_filter(sweeper_instance, :only => configuration[:only])
      11        end
      12      end
      13    end
    

  • ソースを読む:after処理のエントリポイント

       1   def after(controller)
       2      callback(:after)
       3      # Clean up, so that the controller can be collected after this request
       4      self.controller = nil
       5    end
    

  • ソースを読む:after_controller_action処理の呼び出し

       1    def callback(timing)
       2      controller_callback_method_name = "#{ timing}_#{controller.controller_name.underscore}"
       3      action_callback_method_name     = "#{controller_callback_method_name}_#{controller.action_name}"
       4      send!(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
       5      send!(action_callback_method_name)     if respond_to?(action_callback_method_name, true)
       6    end
    

最後の、"send!(action...:自分にメソッドが定義されていたら送る"ってところが実際の呼び出し。
愛が足りないながんばろ。

posted by Png satoko on Mon 14 Jan 2008 at 06:32

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry307
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ