<p>Ok, so I've been refactoring my code in my little Rails app in an effort to remove duplication, and in general make my life easier (as I like an easy life).  Part of this refactoring, has been to move code that's common to two of my models to a module that I can include where I need it.</p>

<p>So far, so good. Looks like it's going to work out, but I've just hit a problem that I'm not sure how to get around.  The module (which I've called sendable), is just going to be the code that handles faxing, e-mailing, or printing a PDF of the document.  So, for example, I have a purchase order, and I have Internal Sales Orders (imaginatively abbreviated to ISO).</p>

<p>The problem I've struck, is that I want some variables initialised (initialized for people who don't spell correctly :P ) after the object is loaded, so I've been using the <strong>after_initialize</strong> hook. No problem... until I start adding some more mixins.</p>

<p>The problem I have, is that I can have an <strong><code>after_initialize</code></strong> in any one of my mixins, so I need to include a <strong>super</strong> call at the start to make sure the other mixin <strong><code>after_initialize</code></strong> calls get called.  Which is great, until I end up calling super and I get an error because there is no super to call.</p>

<p>Here's a little example, in case I haven't been confusing enough:</p>

<pre><code>class Iso &lt; ActiveRecord::Base
  include Shared::TracksSerialNumberExtension
  include Shared::OrderLines
  extend  Shared::Filtered
  include Sendable::Model

  validates_presence_of   :customer
  validates_associated    :lines

  owned_by                :customer
  order_lines             :despatched # Mixin

  tracks_serial_numbers   :items  # Mixin

  sendable :customer                      # Mixin

  attr_accessor :address

  def initialize( params = nil )
    super
    self.created_at ||= Time.now.to_date
  end
end
</code></pre>

<p>So, if each one of the mixins have an after_initialize call, with a <strong>super</strong> call, how can I stop that last <strong>super</strong> call from raising the error? How can I test that the super method exists before I call it?</p>

[{'answer_id': 11719, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 0, 'selected': False, 'text': "<p>Rather than checking if the super method exists, you can just define it</p>\n\n<pre><code>class ActiveRecord::Base\n    def after_initialize\n    end\nend\n</code></pre>\n\n<p>This works in my testing, and shouldn't break any of your existing code, because all your other classes which define it will just be silently overriding this method anyway</p>\n"}, {'answer_id': 11744, 'author': 'newtonapple', 'author_id': 1376, 'author_profile': 'https://Stackoverflow.com/users/1376', 'pm_score': 2, 'selected': False, 'text': '<p>Have you tried <code>alias_method_chain</code>?  You can basically chained up all your <code>after_initialize</code> calls.  It acts like a decorator: each new method adds a new layer of functionality and passes the control onto the "overridden" method to do the rest.  </p>\n'}, {'answer_id': 11866, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': "<p>The including class (the thing that inherits from <code>ActiveRecord::Base</code>, which, in this case is <code>Iso</code>) <em>could</em> define its own <code>after_initialize</code>, so any solution other than <code>alias_method_chain</code> (or other aliasing that saves the original) risks overwriting code.  @Orion Edwards' solution is the best I can come up with.  There are others, but they are <strong>far</strong> more hackish.</p>\n\n<p><code>alias_method_chain</code> also has the benefit of creating named versions of the after_initialize method, meaning you can customize the call order in those rare cases that it matters.  Otherwise, you're at the mercy of whatever order the including class includes the mixins.</p>\n\n<p><em>later</em>:</p>\n\n<p>I've posted a question to the ruby-on-rails-core mailing list about creating default empty implementations of all callbacks.  The saving process checks for them all anyway, so I don't see why they shouldn't be there.  The only downside is creating extra empty stack frames, but that's pretty cheap on every known implementation.</p>\n"}, {'answer_id': 905638, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>You can just throw a quick conditional in there:</p>\n\n<pre><code>super if respond_to?('super')\n</code></pre>\n\n<p>and you should be fine - no adding useless methods; nice and clean.</p>\n"}, {'answer_id': 3867490, 'author': 'valo', 'author_id': 467285, 'author_profile': 'https://Stackoverflow.com/users/467285', 'pm_score': 6, 'selected': True, 'text': '<p>You can use this:</p>\n\n<pre><code>super if defined?(super)\n</code></pre>\n\n<p>Here is an example:</p>\n\n<pre><code>class A\nend\n\nclass B &lt; A\n  def t\n    super if defined?(super)\n    puts "Hi from B"\n  end\nend\n\nB.new.t\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/10808', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/841/']