<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Onkis's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 04:58:40 GMT</pubDate>
    <description>DZone Snippets: Onkis's Code Snippets</description>
    <item>
      <title>Custom Rails Callback</title>
      <link>http://snippets.dzone.com/posts/show/5226</link>
      <description>// This shows you how to extend active record to allow a custom callback.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#ever needed to add your own callback into the active record callback chain?&lt;br /&gt;#here's how...&lt;br /&gt;module ActiveRecord&lt;br /&gt;  &lt;br /&gt;  #to keep things clean we'll define our own module&lt;br /&gt;  module SweetCustomCallbacks&lt;br /&gt;    &lt;br /&gt;    #this is the ruby method that is called when you call include on a module&lt;br /&gt;    #the method passed, base in our case, is the class your including yourself in&lt;br /&gt;    def self.included(base)&lt;br /&gt;      &lt;br /&gt;      #this clas_eval method takes a block that will be added to base, which is ActiveRecord::Base&lt;br /&gt;      #basically this is like writing code directly into the class ActiveRecord::Base&lt;br /&gt;      base.class_eval do&lt;br /&gt;        &lt;br /&gt;        #alias_method_chain is very cool&lt;br /&gt;        #it allows us override a method without removing the original and without the original knowing its been changed&lt;br /&gt;        #here's how it works:&lt;br /&gt;        # 1st argument is the name of the method you're overriding&lt;br /&gt;        # 2nd argument is feature you want to add&lt;br /&gt;        # so if create_or_update is called it will actually call create_or_update_with_sweet_custom_callbacks&lt;br /&gt;        # the original is still avaliable at create_or_update_without_custom_callbacks&lt;br /&gt;        &lt;br /&gt;        alias_method_chain :create_or_update, :sweet_custom_callbacks&lt;br /&gt;        #the method create_or_update is an internals method to ActiveRecord that is called whenever you call .save or .update&lt;br /&gt;        &lt;br /&gt;        #this is the class method that allows the cool syntax:&lt;br /&gt;        # class Article &lt; ActiveRecord::Base&lt;br /&gt;        # before_before_save :add_authors_name&lt;br /&gt;        #private&lt;br /&gt;        #def add_authors_name&lt;br /&gt;        #.....&lt;br /&gt;        def self.before_before_save(*callbacks, &amp;block)&lt;br /&gt;          callbacks &lt;&lt; block if block_given? #add the block to the array of callback functions if a block was passed&lt;br /&gt;          write_inheritable_array(:before_before_save,callbacks) &lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    #the instance method, although not recomended is avaliable for overriding&lt;br /&gt;    #also if you wanted to have a callback that always did something say add the users's id to the object &lt;br /&gt;    #you could always code that in here and remove the class above&lt;br /&gt;    def before_before_save() end&lt;br /&gt;&lt;br /&gt;    #ok this is the method that is now called instead of create_or_update&lt;br /&gt;    #it calls a method callback on &lt;br /&gt;    def create_or_update_with_sweet_custom_callbacks&lt;br /&gt;      return false if callback(:before_before_save) == false  #this method does all the real work and calls the callback function in callbacks.rb that gets the methods and runs them &lt;br /&gt;      create_or_update_without_sweet_custom_callbacks&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;   end&lt;br /&gt; &lt;br /&gt;   class Base    &lt;br /&gt;     include SweetCustomCallbacks #include our module in ActiveRecord::Base&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Mar 2008 16:28:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5226</guid>
      <author>onkis (Mike Ball)</author>
    </item>
  </channel>
</rss>
