thoughtbot--shoulda-matchers/spec/support/unit/helpers/model_builder.rb

92 lines
2.1 KiB
Ruby
Raw Normal View History

require_relative 'class_builder'
module ModelBuilder
include ClassBuilder
def self.drop_created_tables
connection = ActiveRecord::Base.connection
created_tables.each do |table_name|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
end
end
def self.created_tables
@_created_tables ||= []
end
def create_table(table_name, options = {}, &block)
connection = ActiveRecord::Base.connection
2010-06-22 18:14:52 +00:00
begin
connection.execute("DROP TABLE IF EXISTS #{table_name}")
connection.create_table(table_name, options, &block)
ModelBuilder.created_tables << table_name
connection
rescue Exception => e
connection.execute("DROP TABLE IF EXISTS #{table_name}")
raise e
end
end
def define_model_class(class_name, &block)
2012-04-20 21:24:18 +00:00
define_class(class_name, ActiveRecord::Base, &block)
end
def define_active_model_class(class_name, options = {}, &block)
2012-04-22 00:21:30 +00:00
define_class(class_name) do
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
end
end
def define_model(name, columns = {}, &block)
class_name = name.to_s.pluralize.classify
table_name = class_name.tableize.gsub('/', '_')
2012-12-20 05:04:27 +00:00
table_block = lambda do |table|
columns.each do |name, specification|
if specification.is_a?(Hash)
table.column name, specification[:type], specification[:options]
else
table.column name, specification
end
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
define_model_class(class_name).tap do |model|
if block
model.class_eval(&block)
end
model.table_name = table_name
end
end
end
RSpec.configure do |config|
config.include ModelBuilder
config.before do
ModelBuilder.created_tables.clear
end
config.after do
ModelBuilder.drop_created_tables
end
end