Mangle ActiveRecord

Dear Gurus on Rails!

How do I change the behaviour of one method in
ActiveRecord::ConnectionAdapters::SchemaStatements ?

Full story:
I’d like to mangle
ActiveRecord::ConnectionAdapters::SchemaStatements
so when it creates a new sql table, it also (optionally) creates my
created_at, deleted, key_id, session_id etc columns that I use on
basically
all tables.
It would not be DRY to repeat this on all my migrations.

I figured

module ActiveRecord::ConnectionAdapters::SchemaStatements
def create_table(name, options={})
table_definition = TableDefinition.new(self)
table_definition.primary_key(options[:primary_key] || “id”) unless
options[:id] == false
table_definition.column(:obj , :bigint ) unless
options[:obj] == false
table_definition.column(:session_id, :bigint ) unless
options[:session] == false
table_definition.column(:created_at, :datetime) unless
options[:timestamp] == false
table_definition.column(:key_id, :bigint ) unless
options[:key] == false
table_definition.column(:deleted, :bigint ) unless
options[:deleted] == false
yield table_definition
if options[:force]
drop_table(name) rescue nil
end
create_sql = "CREATE#{’ TEMPORARY’ if options[:temporary]} TABLE "
create_sql << “#{name} (”
create_sql << table_definition.to_sql
create_sql << “) #{options[:options]}”
execute create_sql
end
end

placed in the lib in a file would do the trick, but there are problems:
Ruby complains about TableDefinition, it is an uninitialised constant.
What do I do?
I tried
require ‘active_record/connection_adapters/abstract_adapter’
so it sucks in the needed missing parts, but no luck.

Note: bigint is already hacked in, the hack works in a similar way as
this
would

Later I’d like to change the find behaviour to have :last modifier so it
finds the latest record with the same obj values.

I’d rather not change the original source, I’d like to override this one
method’s behaviour.
It would be even wiser to have code that just calls the original
create_table ( the code may change with a new ActiveRecord version ),
and
then alters the table for a little unimportant speed penalty.

If you say my objective can be reached in a better way, say so, and how.

Regards:
Laszlo
noob on rails

Here’s how I achieved something similar. I haven’t put too much
thought into this, so someone might have a better solution. All I’ve
done is intercept create_table so I can add my columns before yielding
to the original block

module DefaultMigrationColumns
def create_table(name, options = {}, &block)
method_missing(:create_table, name, options) do |table|
table.column(:created_at, :datetime)
table.column(:updated_at, :datetime)
table.column(:created_by, :integer)
table.column(:updated_by, :integer)
yield table
end
end
end

ActiveRecord::Migration.send :extend, DefaultMigrationColumns

Tom

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

László Varga wrote:

Ruby complains about TableDefinition, it is an uninitialised constant.
What do I do?

module ActiveRecord::ConnectionAdapters::SchemaStatements
include ActiveRecord::ConnectionAdapters

…other code here…
end

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE0uADMyx0fW1d8G0RAvGjAJ9NNcxHUA80A1MLDGS2YKmdcKM/5ACeJlqi
GI+GzMWouzd/Emo7ck1dcME=
=VnVn
-----END PGP SIGNATURE-----

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs