Skip to content

Commit

Permalink
0.4.0
Browse files Browse the repository at this point in the history
Mongoid adapter, fallback ORM adapter for those not using machinist or active_record, bugfixes

  * replace ActiveRecord 'factory' adapter with Orm adapter.
  
      If you don't have machinist or factory_girl, the Orm factory adapter will fallback to your Orm to create classes.
      
      BC: if you have a line like this:
        Pickle.configure do |config|
          config.adapters = [:active_record]
        end

      You need to replace it with :orm
        Pickle.configure do |config|
          config.adapters = [:orm]
        end
  • Loading branch information
ianwhite committed Aug 19, 2010
1 parent 7d02dd0 commit 08f5808
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 28 deletions.
20 changes: 17 additions & 3 deletions History.txt
@@ -1,8 +1,22 @@
== master
== 0.4.0
Mongoid adapter, fallback ORM adapter for those not using machinist or active_record, bugfixes

* 1 major improvment
* 2 major improvements
* adapter for Mongoid [Sebastian Zuchmanski]

* replace ActiveRecord 'factory' adapter with Orm adapter.

If you don't have machinist or factory_girl, the Orm factory adapter will fallback to your Orm to create classes.

BC: if you have a line like this:
Pickle.configure do |config|
config.adapters = [:active_record]
end

You need to replace it with :orm
Pickle.configure do |config|
config.adapters = [:orm]
end

* 1 minor improvement
* Pickle::Session::ModelNotKnownError is raised instead of a generic RuntimeError

Expand Down
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -118,7 +118,7 @@ Adapters are very simple and exist a module or class with the name "PickleAdapte
User.const_get(:PickleAdapter) #=> should return a pickle adapter

The Active Record and DataMapper ones can be found at
ActiveRecord::Base::PickleAdapter and DataMapper::Resource::PickleAdapter respectively.
ActiveRecord::Base::PickleAdapter, DataMapper::Resource::PickleAdapter, Mongoid::Document::PickleAdapter respectively.

See how to implement one by looking at the ones provided in the pickle source in lib/pickle/adapters/*

Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.3.6
0.4.0
16 changes: 11 additions & 5 deletions lib/pickle/adapter.rb
Expand Up @@ -25,14 +25,15 @@ def create(attrs = {})

self.model_classes = nil

# Include this module into your adapter
# Include this module into your ORM adapter
# this will register the adapter with pickle and it will be picked up for you
# To create an adapter you should create an inner constant "PickleAdapter"
#
# e.g. ActiveRecord::Base::PickleAdapter
#
# @see pickle/adapters/active_record
# @see pickle/adapters/datamapper
# @see pickle/adapters/mongoid
module Base
def self.included(base)
adapters << base
Expand Down Expand Up @@ -69,6 +70,10 @@ def find_first_model(klass, conditions)
def find_all_models(klass, conditions)
klass.const_get(:PickleAdapter).find_all_models(klass, conditions)
end

def create_model(klass, attributes)
klass.const_get(:PickleAdapter).create_model(klass, attributes)
end
end

# machinist adapter
Expand Down Expand Up @@ -113,18 +118,19 @@ def create(attrs = {})
end
end

# fallback active record adapter
class ActiveRecord < Adapter
# ORM adapter. If you have no factory adapter, you can use this adapter to
# use your orm as 'factory' - ie create objects
class Orm < Adapter
def self.factories
::ActiveRecord::Base::PickleAdapter.model_classes.map{|k| new(k)}
model_classes.map{|k| new(k)}
end

def initialize(klass)
@klass, @name = klass, klass.name.underscore.gsub('/','_')
end

def create(attrs = {})
@klass.send(:create!, attrs)
Pickle::Adapter.create_model(@klass, attrs)
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/pickle/adapters/active_record.rb
Expand Up @@ -48,5 +48,10 @@ def self.find_first_model(klass, conditions)
def self.find_all_models(klass, conditions)
klass.find(:all, :conditions => conditions)
end

# Create a model using attributes
def self.create_model(klass, attributes)
klass.create!(attributes)
end
end
end
5 changes: 5 additions & 0 deletions lib/pickle/adapters/data_mapper.rb
Expand Up @@ -33,5 +33,10 @@ def self.find_first_model(klass, conditions)
def self.find_all_models(klass, conditions)
klass.all(conditions)
end

# Create a model using attributes
def self.create_model(klass, attributes)
klass.create(attributes)
end
end
end
5 changes: 5 additions & 0 deletions lib/pickle/adapters/mongoid.rb
Expand Up @@ -34,6 +34,11 @@ def self.find_first_model(klass, conditions)
def self.find_all_models(klass, conditions)
klass.all(conditions)
end

# Create a model with given attributes
def self.create_model(klass, attributes)
klass.create!(attributes)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pickle/config.rb
Expand Up @@ -11,7 +11,7 @@ def configure(&block)
end

def adapters
@adapters ||= [:machinist, :factory_girl]
@adapters ||= [:machinist, :factory_girl, :orm]
end

def adapter_classes
Expand Down
2 changes: 1 addition & 1 deletion lib/pickle/world.rb
@@ -1,6 +1,6 @@
require 'pickle'

# auto require for active record and datamapper
# auto require for active record, datamapper and mongoid
require 'pickle/adapters/active_record' if defined?(ActiveRecord::Base)
require 'pickle/adapters/data_mapper' if defined?(DataMapper::Resource)
require 'pickle/adapters/mongoid' if defined?(Mongoid::Document)
Expand Down
12 changes: 6 additions & 6 deletions spec/pickle/adapter_spec.rb
Expand Up @@ -66,19 +66,19 @@

describe 'with class stubs' do
before do
ActiveRecord::Base::PickleAdapter.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
Pickle::Adapter::Orm.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
end

it ".factories should create one for each active record class" do
Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass1).once
Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass2).once
Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass3).once
Pickle::Adapter::ActiveRecord.factories
Pickle::Adapter::Orm.should_receive(:new).with(@klass1).once
Pickle::Adapter::Orm.should_receive(:new).with(@klass2).once
Pickle::Adapter::Orm.should_receive(:new).with(@klass3).once
Pickle::Adapter::Orm.factories.length.should == 3
end

describe ".new(Class)" do
before do
@factory = Pickle::Adapter::ActiveRecord.new(@klass2)
@factory = Pickle::Adapter::Orm.new(@klass2)
end

it "should have underscored (s/_) name of Class as #name" do
Expand Down
18 changes: 9 additions & 9 deletions spec/pickle/config_spec.rb
Expand Up @@ -5,12 +5,12 @@
@config = Pickle::Config.new
end

it "#adapters should default to :machinist, :factory_girl, :active_record" do
@config.adapters.should == [:machinist, :factory_girl, :active_record]
it "#adapters should default to :machinist, :factory_girl, :orm" do
@config.adapters.should == [:machinist, :factory_girl, :orm]
end

it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryGirl, Adapter::ActiveRecord" do
@config.adapter_classes.should == [Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::ActiveRecord]
it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryGirl, Adapter::Orm" do
@config.adapter_classes.should == [Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::Orm]
end

describe "setting adapters to [:machinist, SomeAdapter]" do
Expand All @@ -29,22 +29,22 @@ class SomeAdapter; end
it "should call adaptor.factories for each adaptor" do
Pickle::Adapter::Machinist.should_receive(:factories).and_return([])
Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([])
Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([])
Pickle::Adapter::Orm.should_receive(:factories).and_return([])
@config.factories
end

it "should aggregate factories into a hash using factory name as key" do
Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist = mock('machinist', :name => 'machinist')])
Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl = mock('factory_girl', :name => 'factory_girl')])
Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record = mock('active_record', :name => 'active_record')])
@config.factories.should == {'machinist' => @machinist, 'factory_girl' => @factory_girl, 'active_record' => @active_record}
Pickle::Adapter::Orm.should_receive(:factories).and_return([@orm = mock('orm', :name => 'orm')])
@config.factories.should == {'machinist' => @machinist, 'factory_girl' => @factory_girl, 'orm' => @orm}
end

it "should give preference to adaptors first in the list" do
Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist_one = mock('one', :name => 'one')])
Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl_one = mock('one', :name => 'one'), @factory_girl_two = mock('two', :name => 'two')])
Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record_two = mock('two', :name => 'two'), @active_record_three = mock('three', :name => 'three')])
@config.factories.should == {'one' => @machinist_one, 'two' => @factory_girl_two, 'three' => @active_record_three}
Pickle::Adapter::Orm.should_receive(:factories).and_return([@orm_two = mock('two', :name => 'two'), @orm_three = mock('three', :name => 'three')])
@config.factories.should == {'one' => @machinist_one, 'two' => @factory_girl_two, 'three' => @orm_three}
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/pickle/session_spec.rb
Expand Up @@ -23,7 +23,7 @@ class Model
end

let :user_factory do
Pickle::Adapter::ActiveRecord.new(user_class)
Pickle::Adapter::Orm.new(user_class)
end

before do
Expand Down

0 comments on commit 08f5808

Please sign in to comment.