Permalink
Browse files

Added a Railtie for initialization. Automatically load under Rails 2.…

…* and 3.*
  • Loading branch information...
1 parent c1a8d77 commit 3ae597b2490837658f2903c9250eba96a4027abb @jyurek jyurek committed May 19, 2010
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
using factories - less error-prone, more explicit, and
all-around easier to work with than fixtures.}
- s.files = Dir['[A-Z]*', 'lib/**/*.rb', 'spec/**/*.rb', 'features/**/*']
+ s.files = Dir['[A-Z]*', 'lib/**/*.rb', 'spec/**/*.rb', 'features/**/*', 'rails/**/*']
s.require_path = 'lib'
s.test_files = Dir['spec/**/*_spec.rb', 'features/**/*']
View
@@ -11,6 +11,7 @@
require 'factory_girl/attribute/callback'
require 'factory_girl/sequence'
require 'factory_girl/aliases'
+require 'factory_girl/railtie'
# Shortcut for Factory.default_strategy.
#
@@ -19,16 +20,3 @@
def Factory (name, attrs = {})
Factory.default_strategy(name, attrs)
end
-
-if defined? Rails.configuration
- Rails.configuration.after_initialize do
- Factory.definition_file_paths = [
- File.join(Rails.root, 'test', 'factories'),
- File.join(Rails.root, 'spec', 'factories')
- ]
- Factory.find_definitions
- end
-else
- Factory.find_definitions
-end
-
@@ -1,5 +1,5 @@
class Factory
- VERSION = "1.2.4"
+ VERSION = "1.2.5.beta1"
# Raised when a factory is defined that attempts to instantiate itself.
class AssociationDefinitionError < RuntimeError
No changes.
@@ -0,0 +1,22 @@
+require 'factory_girl'
+
+class Factory
+ if defined? Rails::Railtie
+ require 'rails'
+ class Railtie < Rails::Railtie
+ config.after_initialize do
+ Factory::Railtie.configure
+ end
+ end
+ end
+
+ class Railtie
+ def self.configure
+ Factory.definition_file_paths = [
+ File.join(Rails.root, 'test', 'factories'),
+ File.join(Rails.root, 'spec', 'factories')
+ ]
+ Factory.find_definitions
+ end
+ end
+end
View
@@ -0,0 +1,2 @@
+require 'factory_girl/railtie'
+Factory::Railtie.configure

2 comments on commit 3ae597b

ql replied May 28, 2010

Hello!
Excuse me for lame question... does this code mean that Factory.find_definitions will run in production environment as well as in test?
Thank you!

Member

jyurek replied May 28, 2010

It will run if the gem is included, so if you include factory_girl in your production environment yes. You shouldn't, though.

Please sign in to comment.