2014-10-14 05:16:12 +00:00
|
|
|
require_relative 'class_builder'
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
module ModelBuilder
|
2014-10-14 05:16:12 +00:00
|
|
|
include ClassBuilder
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2014-10-14 05:16:12 +00:00
|
|
|
def self.drop_created_tables
|
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
|
|
|
|
created_tables.each do |table_name|
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-14 05:16:12 +00:00
|
|
|
def self.created_tables
|
|
|
|
@_created_tables ||= []
|
|
|
|
end
|
|
|
|
|
2011-09-12 10:25:31 +00:00
|
|
|
def create_table(table_name, options = {}, &block)
|
2008-12-15 18:13:56 +00:00
|
|
|
connection = ActiveRecord::Base.connection
|
2010-06-22 18:14:52 +00:00
|
|
|
|
2008-12-15 18:13:56 +00:00
|
|
|
begin
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
2011-09-12 10:25:31 +00:00
|
|
|
connection.create_table(table_name, options, &block)
|
2014-10-14 05:16:12 +00:00
|
|
|
ModelBuilder.created_tables << table_name
|
2009-01-25 21:19:07 +00:00
|
|
|
connection
|
2008-12-15 18:13:56 +00:00
|
|
|
rescue Exception => e
|
2009-01-24 21:23:11 +00:00
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
2008-12-15 18:13:56 +00:00
|
|
|
raise e
|
|
|
|
end
|
2009-01-24 21:23:11 +00:00
|
|
|
end
|
2008-12-15 18:13:56 +00:00
|
|
|
|
2009-01-31 23:42:48 +00:00
|
|
|
def define_model_class(class_name, &block)
|
2012-04-20 21:24:18 +00:00
|
|
|
define_class(class_name, ActiveRecord::Base, &block)
|
2009-01-31 23:42:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-24 16:19:34 +00:00
|
|
|
def define_active_model_class(class_name, options = {}, &block)
|
2012-04-22 00:21:30 +00:00
|
|
|
define_class(class_name) do
|
2011-01-24 16:19:34 +00:00
|
|
|
include ActiveModel::Validations
|
|
|
|
|
|
|
|
options[:accessors].each do |column|
|
|
|
|
attr_accessor column.to_sym
|
|
|
|
end
|
|
|
|
|
2012-03-09 16:53:24 +00:00
|
|
|
if block_given?
|
|
|
|
class_eval(&block)
|
|
|
|
end
|
2011-01-24 16:19:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-24 21:23:11 +00:00
|
|
|
def define_model(name, columns = {}, &block)
|
|
|
|
class_name = name.to_s.pluralize.classify
|
2014-10-08 05:19:07 +00:00
|
|
|
table_name = class_name.tableize.gsub('/', '_')
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
table_block = lambda do |table|
|
2013-11-11 23:38:07 +00:00
|
|
|
columns.each do |name, specification|
|
|
|
|
if specification.is_a?(Hash)
|
|
|
|
table.column name, specification[:type], specification[:options]
|
|
|
|
else
|
|
|
|
table.column name, specification
|
|
|
|
end
|
2009-01-24 21:23:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
if columns.key?(:id) && columns[:id] == false
|
|
|
|
columns.delete(:id)
|
2014-01-17 20:20:44 +00:00
|
|
|
create_table(table_name, id: false, &table_block)
|
2012-12-20 05:04:27 +00:00
|
|
|
else
|
|
|
|
create_table(table_name, &table_block)
|
|
|
|
end
|
|
|
|
|
2014-10-08 05:19:07 +00:00
|
|
|
define_model_class(class_name).tap do |model|
|
|
|
|
if block
|
|
|
|
model.class_eval(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
model.table_name = table_name
|
|
|
|
end
|
2009-01-24 21:23:11 +00:00
|
|
|
end
|
2008-12-15 18:13:56 +00:00
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.include ModelBuilder
|
2014-10-14 05:16:12 +00:00
|
|
|
|
|
|
|
config.before do
|
|
|
|
ModelBuilder.created_tables.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
config.after do
|
|
|
|
ModelBuilder.drop_created_tables
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|