Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 1.9.3 -> 2.0 alias_method and extend

I'm trying to upgrade a Ruby 1.9.3 app to 2.0, and everything seems to have gone smoothly except for one hiccup. I wrote a module which I include in my models to override activerecord destroy. It aliases the existing destroy method to destroy! and then overrides destroy to change a deleted_at timestamp on the record. Only when I upgrade to ruby 2.0 destroy! no longer destroys the record, but behaves just like my new override method. Any idea why this would be? The more relevant section of code is below. Full gist here.

  def self.included(base)                                                          
    base.class_eval do                                                             
      alias_method :destroy!, :destroy                                             
      alias_method :delete!, :delete                                               
      default_scope -> { where(:deleted_at => nil) }                               
    end                                                                            

    base.send :extend, ClassMethods                                                
    base.send :include, InstanceMethods                                            
  end
like image 880
lobati Avatar asked Nov 21 '13 00:11

lobati


1 Answers

Check out the paranoia gem. It's a Rails 3/4 compatible implementation of soft deletes that does just what you're after. If all you want to do is provide a soft-delete then I'd use the gem and be done with it. If you want to implement soft deletes yourself, then the implementation can give you some insights into how it's been done before.

like image 91
AndyV Avatar answered Sep 27 '22 22:09

AndyV