Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Apr 11, 2011
0 parents commit bf57289
Show file tree
Hide file tree
Showing 19 changed files with 490 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.DS_Store
pkg
tmp
*.log
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color --format documentation
2 changes: 2 additions & 0 deletions Gemfile
@@ -0,0 +1,2 @@
source :rubygems
gemspec
89 changes: 89 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,89 @@
PATH
remote: .
specs:
factory_girl-preload (0.1.0)
factory_girl (~> 1.3.3)

GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionpack (3.0.6)
activemodel (= 3.0.6)
activesupport (= 3.0.6)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.5.0)
rack (~> 1.2.1)
rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
activemodel (3.0.6)
activesupport (= 3.0.6)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activerecord (3.0.6)
activemodel (= 3.0.6)
activesupport (= 3.0.6)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activesupport (3.0.6)
archive-tar-minitar (0.5.2)
arel (2.0.9)
builder (2.1.2)
columnize (0.3.2)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
factory_girl (1.3.3)
i18n (0.5.0)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mysql2 (0.2.7)
rack (1.2.2)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
railties (3.0.6)
actionpack (= 3.0.6)
activesupport (= 3.0.6)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
rspec-rails (2.5.0)
actionpack (~> 3.0)
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.5.0)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
thor (0.14.6)
tzinfo (0.3.26)

PLATFORMS
ruby

DEPENDENCIES
actionpack (~> 3.0.6)
activerecord (~> 3.0.6)
factory_girl-preload!
mysql2 (~> 0.2.7)
rspec-rails (~> 2.5.0)
ruby-debug19
119 changes: 119 additions & 0 deletions README.rdoc
@@ -0,0 +1,119 @@
= factory_girl-preload

We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters Factory Girl (FG).

Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of specs.

So here enters Factory Girl Preload (FGP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).

== Installation

gem install factory_girl-preload

== Usage

I'm focusing Rails 3 + RSpec 2 stack, so I can't really guarantee that it will work on other setups. Here's how you get started:

Add both FG and FGP to your Gemfile:

source :rubygems
gem "rails", "3.0.6"
gem "mysql2", "~> 0.2.7"

group :test, :development do
gem "rspec-rails", "~> 2.5.0"
gem "factory_girl", "~> 1.3.3"
gem "factory_girl-preload", "~> 0.1.0"
end

On <tt>spec/spec_helper.rb</tt> file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
config.use_transactional_fixtures = true
config.mock_with :rspec
end

Create your fixtures on <tt>spec/support/factories.rb</tt>. You may have something like this:

Factory.sequence :email do |n|
"john#{n}@doe.com"
end

Factory.sequence :username do |n|
"john#{n}"
end

Factory.sequence :public_key do |n|
File.read(Rails.root.join("spec/fixtures/hash.rsa")) + n.to_s
end

Factory.define :user do |f|
f.name "John Doe"
f.email { Factory.next(:email) }
f.username { Factory.next(:username) }
f.password "test"
f.password_confirmation "test"
end

Factory.define :projects do |f|
f.name "My Project"
f.association :user
end

Finally, you can create your preloadable factories. Put those on <tt>spec/support/preloadable_factories.rb</tt>:

Factory.preload do
factory(:john) { Factory(:user) }
factory(:myapp) { Factory(:project, :user => users(:john)) }
end

Like Rails fixtures, FGP will define methods for each model. You can use it on your examples and alike.

require "spec_helper"

describe User do
let(:user) { users(:john) }

it "returns john's record" do
users(:john).should be_an(User)
end

it "returns myapp's record" do
projects(:myapp).user.should == users(:john)
end
end

Easy and, probably, faster!

== Maintainer

* Nando Vieira (http://nandovieira.com.br)

== License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions Rakefile
@@ -0,0 +1,5 @@
require "bundler"
Bundler::GemHelper.install_tasks

require "rspec/core/rake_task"
RSpec::Core::RakeTask.new
26 changes: 26 additions & 0 deletions factory_girl-preload.gemspec
@@ -0,0 +1,26 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "factory_girl-preload"

Gem::Specification.new do |s|
s.name = "factory_girl-preload"
s.version = Factory::Preload::Version::STRING
s.platform = Gem::Platform::RUBY
s.authors = ["Nando Vieira"]
s.email = ["fnando.vieira@gmail.com"]
s.homepage = "http://rubygems.org/gems/factory_girl-preload"
s.summary = "Preload factories (Factory Girl) just like fixtures. It will be easy and, probably, faster!"
s.description = s.summary

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency "factory_girl", "~> 1.3.3"
s.add_development_dependency "ruby-debug19" if RUBY_VERSION >= "1.9"
s.add_development_dependency "activerecord", "~> 3.0.6"
s.add_development_dependency "actionpack", "~> 3.0.6"
s.add_development_dependency "rspec-rails", "~> 2.5.0"
s.add_development_dependency "mysql2", "~> 0.2.7"
end
1 change: 1 addition & 0 deletions lib/factory_girl-preload.rb
@@ -0,0 +1 @@
require "factory_girl/preload"
42 changes: 42 additions & 0 deletions lib/factory_girl/preload.rb
@@ -0,0 +1,42 @@
class Factory
module Preload
autoload :Helpers, "factory_girl/preload/helpers"
autoload :Version, "factory_girl/preload/version"

require "factory_girl/preload/rspec2" if defined?(RSpec)

class << self
attr_accessor :preloaders
attr_accessor :factories
end

self.preloaders = []
self.factories = {}

def self.run
helper = Object.new.extend(Helpers)

ActiveRecord::Base.connection.transaction :requires_new => true do
preloaders.each do |block|
helper.instance_eval(&block)
end
end
end

def self.clean(*names)
query = ActiveRecord::Base.connection.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" ? "DELETE FROM `%s`" : "TRUNCATE TABLE `%s`"
names = ActiveRecord::Base.descendants.collect(&:table_name).uniq if names.empty?
names.each {|table| ActiveRecord::Base.connection.execute(query % table)}
end

def self.reload_factories
factories.each do |class_name, group|
group.values.each {|factory| factory.reload}
end
end
end

def self.preload(&block)
Preload.preloaders << block
end
end
46 changes: 46 additions & 0 deletions lib/factory_girl/preload/helpers.rb
@@ -0,0 +1,46 @@
class Factory
module Preload
module Helpers
def self.extended(base)
included(base)
end

def self.included(base)
Dir[Rails.application.root.join("app/models/**/*.rb")].each do |file|
require_dependency file
end

ActiveRecord::Base.descendants.each do |model|
method_name = model.name.underscore.gsub("/", "_").pluralize

class_eval <<-RUBY, __FILE__, __LINE__
def #{method_name}(name)
factory(name, #{model})
end
RUBY
end
end

def factory(name, model = nil, &block)
if block_given?
factory_set(name, &block)
else
factory_get(name, model)
end
end

private
def factory_get(name, model)
factory = Factory::Preload.factories[model.name][name] rescue nil
raise "Couldn't find #{name.inspect} factory for #{model.name.inspect} model" unless factory
factory
end

def factory_set(name, &block)
record = block.call
Factory::Preload.factories[record.class.name] ||= {}
Factory::Preload.factories[record.class.name][name.to_sym] = record
end
end
end
end
11 changes: 11 additions & 0 deletions lib/factory_girl/preload/rspec2.rb
@@ -0,0 +1,11 @@
RSpec.configure do |config|
config.include Factory::Preload::Helpers
config.before(:suite) do
Factory::Preload.clean
Factory::Preload.run
end

config.before(:each) do
Factory::Preload.reload_factories
end
end
10 changes: 10 additions & 0 deletions lib/factory_girl/preload/version.rb
@@ -0,0 +1,10 @@
class Factory
module Preload
module Version
MAJOR = 0
MINOR = 1
PATCH = 0
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
end
end
end

0 comments on commit bf57289

Please sign in to comment.