Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Slight refactor of model builder.

  • Loading branch information...
commit 31595b027f13c1df083238c7c1a3cd1f7499f9ca 1 parent eebb806
@gabebw gabebw authored
Showing with 20 additions and 13 deletions.
  1. +20 −13 spec/support/model_builder.rb
View
33 spec/support/model_builder.rb
@@ -8,7 +8,9 @@ def self.included(example_group)
@created_tables ||= []
end
- after { teardown_defined_constants }
+ after do
+ teardown_defined_constants
+ end
end
end
@@ -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)
@@ -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
@@ -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)
@@ -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)
@@ -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.
Something went wrong with that request. Please try again.