5 months ago
Check out my fork of acts_as_soft_deletable.
I added a little extension to ActiveRecord::Migrator to automatically update the columns of the deleted_* tables.
Before this had to be done manually by running
Post::Deleted.update_columns
in your migration everytime you updated the Post table.

I always forgot to do it and thought this might as well be automated. So here's the piece of code that does it all:
module MigratorExtension
  def self.included(base)
    base.class_eval do
      include InstanceMethods    
      alias_method_chain :migrate, :deleted_table_update
    end
  end
  module InstanceMethods
    def migrate_with_deleted_table_update(*args)
      migrate_without_deleted_table_update(*args)
# reloading column definitions, was having issues with Post.columns not reflecting changes  
 ActiveRecord::Base.reset_column_information_and_inheritable_attributes_for_all_subclasses
 ActiveRecord::Acts::SoftDeletable::Live::InstanceMethods.included_in_classes.each do |klass|
        klass::Deleted.update_columns
        puts "Updated columns of #{klass.name}::Deleted"
      end
    end
  end
end

class ActiveRecord::Migrator; include MigratorExtension; end

First, we wrap the migrate instance method of ActiveRecord::Migrator using alias_method_chain.
In this method we first call the original migrate method. Then we reset the column definitions for all tables, I had problems where Post.columns was not reflecting the just migrated changes and this fixed it (might only be a problem for the app I used it in but doesn't hurt :).

At last, we lookup all classes that include acts_as_soft_deletable and update the columns of the corresponding deleted_* table.

Check it out here:
http://github.com/sens3/acts_as_soft_deletable
comments
blog comments powered by Disqus