Skip to content

Commit

Permalink
Gracefully degrade from missing machinist and use models with 'make' …
Browse files Browse the repository at this point in the history
…for machinist pickle names
  • Loading branch information
ianwhite committed Dec 27, 2008
1 parent 6f24d7e commit 93ef347
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 13 additions & 1 deletion lib/pickle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def model_names
end

def blueprint_names
@blueprint_names ||= []
require 'machinist'
@blueprint_names ||= machinist_make_methods
rescue LoadError
@blueprint_names = []
end

def factory_names
Expand All @@ -48,6 +51,15 @@ def map(search, options)
raise ArgumentError, "Usage: map 'search', :to => 'replace'" unless search.is_a?(String) && options[:to].is_a?(String)
self.mappings << [search, options[:to]]
end

protected
def machinist_make_methods
returning Array.new do |methods|
ActiveRecord::Base.send(:subclasses).each do |ar|
ar.respond_to?(:make) && methods.push(ar.name.underscore)
end
end
end
end
end
end
24 changes: 18 additions & 6 deletions spec/lib/pickle_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
describe Pickle::Config do
before do
# zero pickle config before each example
[:names, :model_names, :factory_names, :mappings].each do |config_var|
[:names, :model_names, :blueprint_names, :factory_names, :mappings].each do |config_var|
instance_variable_set "@orig_#{config_var}", Pickle::Config.send(config_var)
Pickle::Config.send("#{config_var}=", nil)
end
end

after do
# restore pickle config back after each example
[:model_names, :factory_names, :names, :mappings].each do |config_var|
[:model_names, :factory_names, :blueprint_names, :names, :mappings].each do |config_var|
Pickle::Config.send "#{config_var}=", instance_variable_get("@orig_#{config_var}")
end
end

it ":factory_names should default to stringified keys of Factory.factories" do
it "#factory_names should default to stringified keys of Factory.factories" do
Factory.factories.should_receive(:keys).and_return([:one, :two])
Pickle::Config.factory_names.should == ['one', 'two']
end
Expand All @@ -26,20 +26,32 @@
Pickle::Config.factory_names.should == []
end

it ":model_names should default to subclasses of AR" do
it "#blueprint_names should default to models with make methods" do
mock_classes = [mock('One', :make => true, :name => 'One'), mock('Two', :make => false, :name => 'Two')]
ActiveRecord::Base.should_receive(:subclasses).and_return(mock_classes)
Pickle::Config.blueprint_names
# Pickle::Config.blueprint_names.should == ['one']
end

it "when machinist not available, #blueprint_names.should == []" do
Pickle::Config.should_receive(:require).with('machinist').and_raise(LoadError)
Pickle::Config.blueprint_names.should == []
end

it "#model_names should default to subclasses of AR" do
mock_classes = [mock('One', :name => 'One'), mock('One::A', :name => 'One::A'), mock('One::A::B', :name => 'One::A::B')]
ActiveRecord::Base.should_receive(:subclasses).and_return(mock_classes)
Pickle::Config.model_names.should == ['one', 'one/a', 'one/a/b']
end

it ":names should default to set of (Array) :blueprint, :factory, and :model names" do
it "#names should default to set of (Array) #blueprint, #factory, and #model names" do
Pickle::Config.blueprint_names = ['one', 'two_a']
Pickle::Config.factory_names = ['one', 'two']
Pickle::Config.model_names = ['two', 'one/a', 'one/b']
Pickle::Config.names.sort.should == ['one', 'one/a', 'one/b', 'two', 'two_a']
end

it ":mappings should default to []" do
it "#mappings should default to []" do
Pickle::Config.mappings.should == []
end
end

0 comments on commit 93ef347

Please sign in to comment.