Skip to content
This repository
Browse code

Slight refactor of model builder.

  • Loading branch information...
commit 31595b027f13c1df083238c7c1a3cd1f7499f9ca 1 parent eebb806
Gabe Berke-Williams authored March 09, 2012

Showing 1 changed file with 20 additions and 13 deletions. Show diff stats Hide diff stats

  1. 33  spec/support/model_builder.rb
33  spec/support/model_builder.rb
@@ -8,7 +8,9 @@ def self.included(example_group)
8 8
         @created_tables ||= []
9 9
       end
10 10
 
11  
-      after { teardown_defined_constants }
  11
+      after do
  12
+        teardown_defined_constants
  13
+      end
12 14
     end
13 15
   end
14 16
 
@@ -29,14 +31,18 @@ def create_table(table_name, options = {}, &block)
29 31
   def define_constant(class_name, base, &block)
30 32
     class_name = class_name.to_s.camelize
31 33
 
32  
-    klass = Class.new(base)
33  
-    Object.const_set(class_name, klass)
34  
-    klass.unloadable
  34
+    Class.new(base).tap do |constant_class|
  35
+      Object.const_set(class_name, constant_class)
  36
+      constant_class.unloadable
35 37
 
36  
-    klass.class_eval(&block) if block_given?
37  
-    klass.reset_column_information if klass.respond_to?(:reset_column_information)
  38
+      if block_given?
  39
+        constant_class.class_eval(&block)
  40
+      end
38 41
 
39  
-    klass
  42
+      if constant_class.respond_to?(:reset_column_information)
  43
+        constant_class.reset_column_information
  44
+      end
  45
+    end
40 46
   end
41 47
 
42 48
   def define_model_class(class_name, &block)
@@ -51,7 +57,9 @@ def define_active_model_class(class_name, options = {}, &block)
51 57
         attr_accessor column.to_sym
52 58
       end
53 59
 
54  
-      class_eval(&block) if block_given?
  60
+      if block_given?
  61
+        class_eval(&block)
  62
+      end
55 63
     end
56 64
   end
57 65
 
@@ -70,7 +78,7 @@ def define_model(name, columns = {}, &block)
70 78
 
71 79
   def define_mailer(name, paths, &block)
72 80
     class_name = name.to_s.pluralize.classify
73  
-    klass = define_constant(class_name, ActionMailer::Base, &block)
  81
+    define_constant(class_name, ActionMailer::Base, &block)
74 82
   end
75 83
 
76 84
   def define_controller(class_name, &block)
@@ -126,10 +134,10 @@ def create_view(path, contents)
126 134
   def teardown_defined_constants
127 135
     ActiveSupport::Dependencies.clear
128 136
 
  137
+    connection = ActiveRecord::Base.connection
  138
+
129 139
     @created_tables.each do |table_name|
130  
-      ActiveRecord::Base.
131  
-        connection.
132  
-        execute("DROP TABLE IF EXISTS #{table_name}")
  140
+      connection.execute("DROP TABLE IF EXISTS #{table_name}")
133 141
     end
134 142
 
135 143
     FileUtils.rm_rf(TMP_VIEW_PATH)
@@ -141,4 +149,3 @@ def teardown_defined_constants
141 149
 RSpec.configure do |config|
142 150
   config.include ModelBuilder
143 151
 end
144  
-

0 notes on commit 31595b0

Please sign in to comment.
Something went wrong with that request. Please try again.