Permalink
Browse files

Move DefineConstant code into macro

  • Loading branch information...
1 parent 7c9254a commit 0b2c4da0a4082ac9b8f484ba93026e0ab7103254 @joshuaclayton joshuaclayton committed Aug 17, 2011
@@ -1,11 +0,0 @@
-require 'active_record'
-
-ActiveRecord::Base.establish_connection(
- :adapter => 'sqlite3',
- :database => File.join(File.dirname(__FILE__), 'test.db')
-)
-
-RSpec.configure do |config|
- config.include DefinesConstants
-end
-
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "attribute aliases" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "a generated attributes hash" do
include FactoryGirl::Syntax::Methods
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "a generated attributes hash where order matters" do
include FactoryGirl::Syntax::Methods
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "build multiple instances" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "a built instance" do
include FactoryGirl::Syntax::Methods
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "a generated stub instance" do
include FactoryGirl::Syntax::Methods
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "callbacks" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "create multiple instances" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "a created instance" do
include FactoryGirl::Syntax::Methods
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "default strategy" do
it "uses create when not specified" do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "an instance generated by a factory with a custom class name" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "an instance generated by a factory" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
require 'active_support/ordered_hash'
describe "attribute overrides" do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "an instance generated by a factory that inherits from another factory" do
before do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "sequences" do
include FactoryGirl::Syntax::Methods
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
require 'factory_girl/syntax/blueprint'
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
require 'factory_girl/syntax/generate'
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
require 'factory_girl/syntax/make'
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
require 'factory_girl/syntax/sham'
@@ -1,5 +1,4 @@
require 'spec_helper'
-require 'acceptance/acceptance_helper'
describe "vintage syntax" do
before do
@@ -1,5 +1,4 @@
require "spec_helper"
-require "acceptance/acceptance_helper"
describe "an instance generated by a factory with multiple traits" do
before do
@@ -1,8 +1,6 @@
require 'spec_helper'
describe FactoryGirl::Factory do
- include DefinesConstants
-
before do
@name = :user
@class = define_class('User')
@@ -220,8 +218,6 @@
end
describe FactoryGirl::Factory, "with a name ending in s" do
- include DefinesConstants
-
let(:name) { :business }
let(:business_class) { Business }
@@ -239,8 +235,6 @@
end
describe FactoryGirl::Factory, "for namespaced class" do
- include DefinesConstants
-
let(:name) { :settings }
let(:settings_class) { Admin::Settings }
@@ -267,8 +261,6 @@
end
describe FactoryGirl::Factory do
- include DefinesConstants
-
let(:factory_with_non_existant_strategy) do
FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) { }
end
View
@@ -9,6 +9,8 @@
require "mocha"
require "bourne"
+Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
+
RSpec.configure do |config|
config.mock_framework = :mocha
@@ -18,70 +20,3 @@
FactoryGirl.traits.clear
end
end
-
-module DefinesConstants
- def self.included(example_group)
- example_group.class_eval do
- before do
- @defined_constants ||= []
- @created_tables ||= []
- end
-
- after do
- @defined_constants.reverse.each do |path|
- namespace, class_name = *constant_path(path)
- namespace.send(:remove_const, class_name)
- end
- @defined_constants.clear
-
- @created_tables.each do |table_name|
- ActiveRecord::Base.
- connection.
- execute("DROP TABLE IF EXISTS #{table_name}")
- end
- @created_tables.clear
- end
-
- def define_class(path, base = Object, &block)
- namespace, class_name = *constant_path(path)
- klass = Class.new(base)
- namespace.const_set(class_name, klass)
- klass.class_eval(&block) if block_given?
- @defined_constants << path
- klass
- end
-
- def define_model(name, columns = {}, &block)
- model = define_class(name, ActiveRecord::Base, &block)
- create_table(model.table_name) do |table|
- columns.each do |name, type|
- table.column name, type
- end
- end
- model
- end
-
- def create_table(table_name, &block)
- connection = ActiveRecord::Base.connection
-
- begin
- connection.execute("DROP TABLE IF EXISTS #{table_name}")
- connection.create_table(table_name, &block)
- @created_tables << table_name
- connection
- rescue Exception => exception
- connection.execute("DROP TABLE IF EXISTS #{table_name}")
- raise exception
- end
- end
-
- def constant_path(constant_name)
- names = constant_name.split('::')
- class_name = names.pop
- namespace = names.inject(Object) { |result, name| result.const_get(name) }
- [namespace, class_name]
- end
- end
- end
-end
-
@@ -0,0 +1,86 @@
+require 'active_record'
+
+module DefineConstantMacros
+ def define_class(path, base = Object, &block)
+ namespace, class_name = *constant_path(path)
+ klass = Class.new(base)
+ namespace.const_set(class_name, klass)
+ klass.class_eval(&block) if block_given?
+ @defined_constants << path
+ klass
+ end
+
+ def define_model(name, columns = {}, &block)
+ model = define_class(name, ActiveRecord::Base, &block)
+ create_table(model.table_name) do |table|
+ columns.each do |name, type|
+ table.column name, type
+ end
+ end
+ model
+ end
+
+ def create_table(table_name, &block)
+ connection = ActiveRecord::Base.connection
+
+ begin
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
+ connection.create_table(table_name, &block)
+ @created_tables << table_name
+ connection
+ rescue Exception => exception
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
+ raise exception
+ end
+ end
+
+ def constant_path(constant_name)
+ names = constant_name.split('::')
+ class_name = names.pop
+ namespace = names.inject(Object) { |result, name| result.const_get(name) }
+ [namespace, class_name]
+ end
+
+ def default_constants
+ @defined_constants ||= []
+ @created_tables ||= []
+ end
+
+ def clear_generated_constants
+ @defined_constants.reverse.each do |path|
+ namespace, class_name = *constant_path(path)
+ namespace.send(:remove_const, class_name)
+ end
+
+ @defined_constants.clear
+ end
+
+ def clear_generated_tables
+ @created_tables.each do |table_name|
+ ActiveRecord::Base.
+ connection.
+ execute("DROP TABLE IF EXISTS #{table_name}")
+ end
+ @created_tables.clear
+ end
+end
+
+RSpec.configure do |config|
+ config.include DefineConstantMacros
+
+ config.before(:all) do
+ ActiveRecord::Base.establish_connection(
+ :adapter => 'sqlite3',
+ :database => File.join(File.dirname(__FILE__), 'test.db')
+ )
+ end
+
+ config.before do
+ default_constants
+ end
+
+ config.after do
+ clear_generated_constants
+ clear_generated_tables
+ end
+end

0 comments on commit 0b2c4da

Please sign in to comment.