Permalink
Browse files

Adding support for ActiveModel to make us compatible with latest merb.

  • Loading branch information...
1 parent 7a6786a commit f4d62725f3dd51d01eb131fc68acf4690af1a279 @pk committed Oct 17, 2009
View
@@ -53,18 +53,25 @@ use_orm :sequel
== Compatibility
-===Ruby 1.8.7:
+=== Ruby 1.8.7:
Sequel 2.11.0:: All pass
Sequel 2.12.0:: All pass
Sequel 3.0.0:: All pass
Sequel 3.5.0:: All pass
-===Ruby 1.9.1:
+=== Ruby 1.9.1:
Sequel 2.11.0:: All pass except session spec failing due to Marshall issues.
Sequel 2.12.0:: All pass except session spec failing due to Marshall issues.
Sequel 3.0.0:: All pass except session spec failing due to Marshall issues.
Sequel 3.5.0:: All pass except session spec failing due to Marshall issues.
+=== ActiveModel compatibility
+In Merb > 1.0.13 we use ActiveModel interface for the ORM plugins. merb_sequel
+now supports this for all versions of Sequel by including compatibilit layers
+by default or loading active model plugin when using Sequel > 3.5.0
+
+You can use configuration to not load compatibility layer:
+Merb::Plugins.config[:merb_sequel][:load_activemodel_compatibility] = false
== RSpec transactional examples
@@ -77,7 +84,11 @@ your spec_helper.rb or any spec you want to be transactional:
Now <b>each example is wrapped in a Sequel transaction and when example finishes
Sequel::Error::Rollback is raised which cause transaction to rollback</b>.
-== Connection options
+== Connection options & Settings
+
+Settings:
+
+* :load_activemodel_compatibility. By default is set to true. If set to false, ActiveModelCompatibility is not loaded.
Merb Sequel plug-in uses config/database.yml for connection configuration.
@@ -27,7 +27,7 @@ def config
@config ||= begin
# Convert string keys to symbols
full_config = Erubis.load_yaml_file(config_file)
- config = (Merb::Plugins.config[:merb_sequel] = {})
+ config = Merb::Plugins.config[:merb_sequel]
(full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value|
config[key.to_sym] = value
end
@@ -0,0 +1,38 @@
+module Merb
+ module Orms
+ module Sequel
+ module Model
+
+ # This code has been taken from Sequel 3.5.0
+ # Sequel::Plugins::ActiveModel
+ # http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/ActiveModel.html
+ module ActiveModelCompatibility
+ # Record that an object was destroyed, for later use by
+ # destroyed?
+ def after_destroy
+ super
+ @destroyed = true
+ end
+
+ # Whether the object was destroyed by destroy. Not true
+ # for objects that were deleted.
+ def destroyed?
+ @destroyed == true
+ end
+
+ # An alias for new?
+ def new_record?
+ new?
+ end
+
+ # With the ActiveModel plugin, Sequel model objects are already
+ # compliant, so this returns self.
+ def to_model
+ self
+ end
+ end
+
+ end
+ end
+ end
+end
View
@@ -1,11 +1,12 @@
if defined?(Merb::Plugins)
- Merb::Plugins.config[:merb_sequel] = {}
- require File.join(File.dirname(__FILE__) / "sequel_ext" / "model")
+
+ # Default settings
+ Merb::Plugins.config[:merb_sequel] = { :load_activemodel_compatibility => true }
+
+ require File.join(File.dirname(__FILE__) / "merb" / "orms" / "sequel" / "model")
require File.join(File.dirname(__FILE__) / "merb" / "orms" / "sequel" / "connection")
Merb::Plugins.add_rakefiles "merb_sequel" / "merbtasks"
- Sequel::Model.send(:include, Merb::Orms::Sequel::ModelExtensions)
-
# Connects to the database and handles session
#
# Connects to the database and loads sequel sessions if we use them.
@@ -20,7 +21,29 @@ def self.run
require File.join(File.dirname(__FILE__) / "merb" / "session" / "sequel_session")
end
+ # Set identifiy to use Sequel primary key field
Merb::Router.root_behavior = Merb::Router.root_behavior.identify(Sequel::Model => :pk)
+
+ # Load compatibility extensions
+ if Merb::Plugins.config[:merb_sequel][:load_activemodel_compatibility]
+ load_activemodel_compatibility
+ end
+ end
+
+ # Load active model plugin if available
+ #
+ # Merb > 1.0.13 expects models to be ActiveModel compatible
+ # Sequel 3.5.0 added plugin to make Sequel models AtciveModel
+ # compatible.
+ #
+ # We're loading plugin to all models here if plugin is available
+ # if the plugin is not available we must include compatibility module.
+ def self.load_activemodel_compatibility
+ begin
+ Sequel::Model.plugin :active_model
+ rescue LoadError, NoMethodError
+ Sequel::Model.send(:include, Merb::Orms::Sequel::Model::ActiveModelCompatibility)
+ end
end
end
@@ -1,11 +0,0 @@
-module Merb
- module Orms
- module Sequel
- module ModelExtensions
- def new_record?
- self.new?
- end
- end
- end
- end
-end
@@ -1,11 +1,15 @@
require File.dirname(__FILE__) + "/spec_helper"
describe Merb::Orms::Sequel::Connect do
- it "is loaded at plugin bootstrap" do
+ it "should be loaded at plugin bootstrap" do
defined?(Merb::Orms::Sequel::Connect).should == "constant"
end
- it "is a merb BootLoader" do
+ it "should be Merb::BootLoader" do
Merb::Orms::Sequel::Connect.superclass.should eql(Merb::BootLoader)
end
+
+ it "should provide default settings" do
+ Merb::Plugins.config[:merb_sequel].should include(:load_activemodel_compatibility)
+ end
end
@@ -1,15 +1,10 @@
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/../../lib/merb_sequel/rspec/sequel'
-describe "Transaction enabled examples" do
-
- before(:all) do
- CreateSpecModel.apply(SpecModel.db, :up)
- end
+CreateSpecModel.apply(Sequel::Model.db, :up)
+class SpecModel < Sequel::Model; end
- after(:all) do
- CreateSpecModel.apply(SpecModel.db, :down)
- end
+describe "Transaction enabled examples" do
it "should wrap each example in transaction and rollback" do
a = SpecModel.create(:name => 'Pavel')
@@ -1,16 +1,29 @@
require File.dirname(__FILE__) + "/spec_helper"
-describe 'Sequel::Model#new_record?' do
- it_should_behave_like "it has a SpecModel"
-
- it "is defined" do
- SpecModel.instance_methods.map {|m| m.to_sym }.should include(:new_record?)
+CreateSpecModel.apply(Sequel::Model.db, :up)
+class SpecModel < Sequel::Model; end
+
+describe 'Merb::Orms::Sequel::Model' do
+
+ describe "active model" do
+ it "should load active model plugin when > 3.5" do
+ SpecModel.plugins.should include(Sequel::Plugins::ActiveModel)
+ end
+
+ it "should include ActiveModelCompatibility module if plugin is not available" do
+ begin
+ Sequel::Model.plugin :active_model
+ pending("For this SPEC to run and pass we need to install Sequel < 3.5")
+ rescue LoadError, NoMethodError
+ SpecModel.ancestors.should include(Merb::Orms::Sequel::Model::ActiveModelCompatibility)
+ end
+ end
end
-
- it "Returns true or new model" do
- a = SpecModel.new
- a.should be_new_record
- a.save
- a.should_not be_new_record
+
+ describe "ActiveModelCompatiblity" do
+ it "should add new_record?" do
+ m = SpecModel.new
+ m.should be_new_record
+ end
end
end
View
@@ -1,24 +1,24 @@
-$:.insert 0, File.join(File.dirname(__FILE__), "..", "lib")
+$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require 'rubygems'
require 'merb-core'
require 'spec'
-require 'spec/mocks'
-dependency 'sequel'
-require 'merb-core/test'
-require 'merb-core/test/helpers'
+require 'sequel'
+require 'merb_sequel'
-require File.join( File.dirname(__FILE__), "..", "lib", 'merb_sequel')
-
-Merb.start :environment => 'test',
+Merb.start(:environment => 'test',
:adapter => 'runner',
:session_store => 'sequel',
- :merb_root => File.dirname(__FILE__)
+ :merb_root => File.dirname(__FILE__))
# Load extensions if we use new versions of Sequel
require 'sequel/extensions/migration' if Merb::Orms::Sequel.new_sequel?
Spec::Runner.configure do |config|
config.include Merb::Test::RequestHelper
+
+ config.after(:all) do
+ CreateSpecModel.apply(Sequel::Model.db, :up)
+ end
end
require File.join( File.dirname(__FILE__), 'spec_model')
View
@@ -1,7 +1,3 @@
-# Dummy model for testing
-class SpecModel < Sequel::Model
-end
-
# Migrations to create the table
class CreateSpecModel < Sequel::Migration
def up
@@ -15,14 +11,3 @@ def down
drop_table :spec_models
end
end
-
-# Shared describe to run migration
-describe "it has a SpecModel", :shared => true do
- before(:each) do
- CreateSpecModel.apply(SpecModel.db, :up)
- end
-
- after(:each) do
- CreateSpecModel.apply(SpecModel.db, :down)
- end
-end

0 comments on commit f4d6272

Please sign in to comment.