16 - adding hooks to table events.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 58 行

RB
58
字号
require 'cookbook_dbconnect'require 'og'require 'glue/aspects'class BlogPost  property :title, :content, String  after :on => :og_insert do |post|    puts %{Sending email notification of new post "#{post.title}"}    # Actually send the email here...  endendog_connectpost = BlogPost.newpost.title = 'Robots are taking over'post.content = 'Think about it! When was the last time you saw another human?'post.save!# Sending email notification of new post "Robots are taking over"#---require 'cookbook_dbconnect'activerecord_connectclass BlogPost < ActiveRecord::Base  def after_create    puts %{Sending email notification of new blog post "#{title}"}    # Actually send the email here...  endendpost = BlogPost.create(:title => 'Robots: Gentle Yet Misunderstood',                       :content => 'Popular misconceptions about robERROR 40')# Sending email notification of new blog post "Robots: Gentle Yet Misunderstood#---require 'cookbook_dbconnect'activerecord_connectclass BlogPost < ActiveRecord::Baseendclass MailObserver < ActiveRecord::Observer observe BlogPost  def after_create(post)    puts %{Sending email notification of new blog post "#{post.title}"}    # Actually send the email here.  endendActiveRecord::Base.observers = MailObserverpost = BlogPost.new(:title => "ERROR 40",                    :content => "ERROR ERROR ERROR ERROR ERROR")post.save# Sending email notification of new blog post "ERROR 40"#---# environment.rbconfig.active_record.observers = MailObserver#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?