I probably won’t take the time to code this up but in case someone else
here is eager to do this, I can easily show you where to start. The
funny thing is that it would actually be ridiculously easy.
First start here:
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001150
Here is the (abridged for relevance) code for “create_table”:
def create_table(table_name, options = {})
instantiate a “TableDefinition” class
table_definition = TableDefinition.new(self)
#…set the tables “id”…
#…
yield the instance to the user (let them make calls to the object)
yield table_definition
#make the sql based on the columns added to the instance
create_sql = #… turn the table_definition into SQL
#run the sql that was built based on the columns stored in the
instance
execute create_sql
end
This is actually not very complicated at all. Simply “yielding” to the
code passed into the block so:
create_table “examples” do |t|
t.column :example, :integer
end
simply calls the “column” function on the “t” variable which is actually
an instance of TableDefinition
So, what is TableDefinition?
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html
Just a class with some basic functions that adds columns to an instance
variable array and has a method for turning the columns array into sql.
That is basically the jist of it. I leave actually coding it as an
exercise to the reader, but I guarantee that it is doable.