Skip to content

Commit

Permalink
Load fabricators from folders specified in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Elliott committed May 29, 2011
1 parent cc14605 commit cd12487
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
10 changes: 8 additions & 2 deletions lib/fabrication/config.rb
Expand Up @@ -9,10 +9,16 @@ def self.fabricator_dir=(folders)
OPTIONS[:fabricator_dir] = (Array.new << folders).flatten
end

def self.reset_defaults
OPTIONS.clear
OPTIONS.merge!(DEFAULTS)
end

private

OPTIONS = {
:fabricator_dir => ['test/fabricators', 'spec/fabricators']
DEFAULTS = {
:fabricator_dir => ['test', 'spec']
}
OPTIONS = {}.merge!(DEFAULTS)
end
end
10 changes: 3 additions & 7 deletions lib/fabrication/support.rb
Expand Up @@ -23,13 +23,9 @@ def variable_name_to_class_name(name)
end

def find_definitions
base_path = defined?(Rails) ? Rails.root : "."
['test', 'spec'].each do |folder|
path = File.expand_path(File.join(base_path, folder, 'fabricators'))

load("#{path}.rb") if File.exists?("#{path}.rb")

File.directory? path and Dir[File.join(path, '*.rb')].sort.each do |file|
path_prefix = defined?(Rails) ? Rails.root : "."
Fabrication::Config.fabricator_dir.each do |folder|
Dir.glob(File.join(path_prefix, folder, '**', '*fabricator.rb')).sort.each do |file|
load file
end
end
Expand Down
15 changes: 12 additions & 3 deletions spec/fabrication/config_spec.rb
Expand Up @@ -2,18 +2,27 @@

describe Fabrication::Config do
subject { Fabrication::Config }
after { Fabrication::Config.reset_defaults }

context "default configs" do
its(:fabricator_dir) { should == ['test/fabricators', 'spec/fabricators'] }
its(:fabricator_dir) { should == ['test', 'spec'] }
end

describe ".fabricator_dir" do
context "with a single folder" do
before do
Fabrication::Config.fabricator_dir = 'lib/fabricators'
Fabrication::Config.fabricator_dir = 'lib'
end

its(:fabricator_dir) { should == ['lib/fabricators'] }
its(:fabricator_dir) { should == ['lib'] }
end

context "with multiple folders" do
before do
Fabrication::Config.fabricator_dir = %w(lib support)
end

its(:fabricator_dir) { should == ['lib', 'support'] }
end
end
end

0 comments on commit cd12487

Please sign in to comment.