Skip to content

Commit

Permalink
Slight refactor of model builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Mar 9, 2012
1 parent eebb806 commit 31595b0
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions spec/support/model_builder.rb
Expand Up @@ -8,7 +8,9 @@ def self.included(example_group)
@created_tables ||= []
end

after { teardown_defined_constants }
after do
teardown_defined_constants
end
end
end

Expand All @@ -29,14 +31,18 @@ def create_table(table_name, options = {}, &block)
def define_constant(class_name, base, &block)
class_name = class_name.to_s.camelize

klass = Class.new(base)
Object.const_set(class_name, klass)
klass.unloadable
Class.new(base).tap do |constant_class|
Object.const_set(class_name, constant_class)
constant_class.unloadable

klass.class_eval(&block) if block_given?
klass.reset_column_information if klass.respond_to?(:reset_column_information)
if block_given?
constant_class.class_eval(&block)
end

klass
if constant_class.respond_to?(:reset_column_information)
constant_class.reset_column_information
end
end
end

def define_model_class(class_name, &block)
Expand All @@ -51,7 +57,9 @@ def define_active_model_class(class_name, options = {}, &block)
attr_accessor column.to_sym
end

class_eval(&block) if block_given?
if block_given?
class_eval(&block)
end
end
end

Expand All @@ -70,7 +78,7 @@ def define_model(name, columns = {}, &block)

def define_mailer(name, paths, &block)
class_name = name.to_s.pluralize.classify
klass = define_constant(class_name, ActionMailer::Base, &block)
define_constant(class_name, ActionMailer::Base, &block)
end

def define_controller(class_name, &block)
Expand Down Expand Up @@ -126,10 +134,10 @@ def create_view(path, contents)
def teardown_defined_constants
ActiveSupport::Dependencies.clear

connection = ActiveRecord::Base.connection

@created_tables.each do |table_name|
ActiveRecord::Base.
connection.
execute("DROP TABLE IF EXISTS #{table_name}")
connection.execute("DROP TABLE IF EXISTS #{table_name}")
end

FileUtils.rm_rf(TMP_VIEW_PATH)
Expand All @@ -141,4 +149,3 @@ def teardown_defined_constants
RSpec.configure do |config|
config.include ModelBuilder
end

0 comments on commit 31595b0

Please sign in to comment.