On 3/1/06, Adam Ballai <[EMAIL PROTECTED]> wrote:
> Rick Olson wrote:
> > Perhaps plugins shouldn't be defining those methods, and instead use
> > the callback class methods:
> >
> > class Foo < AR::Base
> >   before_save :do_this_one_last_thing
> >
> >   protected
> >   def do_this_one_last_thing
> >   end
> > end
> >
> > Otherwise, this could cause issues for someone overwriting a parent's
> > callback, expecting to override it completely.
> >
> Well thats what the plugins _are_ doing in a mix-in form.
>
>   class_eval do
>     before_save :something
>   end
>
> acts_as_auditable sets before_save :something to -> Parent
> acts_as_ldapable set before_save :something_else to -> Child
> :something to-> Parent becomes ignored instead of calling the parent as
> well...
>
> before_save is a callback for the observer, but it is still a method, so
> you will always override it, unless you call super on it.  In one way
> this is proper behavior, but it begs the question, what if we need to do
> more than one different thing before or after a save and they are
> different between types in a single table inheritance scheme.
>
> Either the design meant to make callbacks overrideable via inheritance,
> or missed a tiny detail in implementation.
>
> --
> --
> Adam Ballai <[EMAIL PROTECTED]>
> Integrum Technologies, LLC
> Phone: +1 602 792 1270 x 104
> Mobile: +1 602 373 3072

The before_save class method adds the callbacks to an inheritable
array, so they should be passed to the subclasses.  Unless, of course,
your subclass is loaded before the before filter:

class Parent < AR::Base
  belongs_to :child # in Rails 1.0, this loads Child, and no callbacks
are passed on
  before_save :one_last_thing_before_you_go
end

To prove this, I just typed this into script/console

>> class Parent < ActiveRecord::Base
>>   before_save { puts 'parent' }
>> end
=> [#<Proc:0x025b20ec@(irb):3>]
>> class Child < Parent
>>   before_save { puts 'child' }
>>   def before_save
>>     puts 'only child'
>>   end
>> end
=> nil
>> Child.new.save
parent
child
only child

Am I just totally missing what you're saying?

I have an article on this topic too:
http://weblog.techno-weenie.net/2006/2/21/unitialized-constant-technoweenie.

--
Rick Olson
http://techno-weenie.net
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to