diff --git a/.gitignore b/.gitignore index 82d7eec..6daa6db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ spec/debug.log -.DS_Store \ No newline at end of file +.DS_Store +spec/db/*.sqlite* \ No newline at end of file diff --git a/lib/identity_map/active_record/base.rb b/lib/identity_map/active_record/base.rb index abe7d2a..6aabaf2 100644 --- a/lib/identity_map/active_record/base.rb +++ b/lib/identity_map/active_record/base.rb @@ -4,6 +4,7 @@ def self.included(base) base.extend(ClassMethods) class << base alias_method_chain :instantiate, :identity_map + alias_method_chain :create, :identity_map end end @@ -23,6 +24,11 @@ def enlist_in_transaction(object) identity_map.put(object) end + def create_with_identity_map(options, &block) + me = create_without_identity_map(options, &block) + enlist_in_transaction(me) + end + end end diff --git a/spec/database.yml b/spec/database.yml new file mode 100644 index 0000000..195eca2 --- /dev/null +++ b/spec/database.yml @@ -0,0 +1,21 @@ +sqlite: + :adapter: sqlite + :database: vendor/plugins/identity_map/spec/db/identity_map_plugin.sqlite.db + +sqlite3: + :adapter: sqlite3 + :database: vendor/plugins/identity_map/spec/db/identity_map_plugin.sqlite3.db + +postgresql: + :adapter: postgresql + :username: postgres + :password: postgres + :database: identity_map_plugin_test + :min_messages: ERROR + +mysql: + :adapter: mysql + :host: localhost + :username: root + :password: + :database: identity_map_plugin_test diff --git a/spec/db/schema.rb b/spec/db/schema.rb new file mode 100644 index 0000000..16573f5 --- /dev/null +++ b/spec/db/schema.rb @@ -0,0 +1,10 @@ +ActiveRecord::Schema.define(:version => 0) do + puts "Creating Schema" + create_table :customers, :force => true do |t| + t.string :name + end + create_table :phone_numbers, :force => true do |t| + t.string :number + t.integer :customer_id + end +end \ No newline at end of file diff --git a/spec/identity_map_spec.rb b/spec/identity_map_spec.rb index 1347ca9..d67db6c 100644 --- a/spec/identity_map_spec.rb +++ b/spec/identity_map_spec.rb @@ -1,5 +1,37 @@ require File.dirname(__FILE__) + '/spec_helper' - -describe "NewFu" do - it "should have a pending spec" + +describe "Customers" do + + before(:each) do + Thread.current["identity_map"] = Cache.new + end + + it "should load the same model twice" do + c1 = Customer.first + c2 = Customer.first + c1.__id__.should == c2.__id__ + end + + it "should work for has_many associations" do + p1 = PhoneNumber.first + p2 = Customer.first.phone_numbers.first + p1.__id__.should == p2.__id__ + end + + it "should work for belongs_to assocations" do + d1 = Customer.first + d2 = PhoneNumber.first.customer + d1.__id__.should == d2.__id__ + end + + it "should work for creating objects" do + c1 = Customer.create(:name => "billy") + c2 = Customer.find_by_name("billy") + c1.__id__.should == c2.__id__ + end + + after(:each) do + Thread.current["identity_map"] = nil + end + end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3424846..905933c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,3 +8,31 @@ plugin_spec_dir = File.dirname(__FILE__) ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log") +def load_schema + config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) + ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") + + db_adapter = ENV['DB'] + # no db passed, try one of these fine config-free DBs before bombing. + db_adapter ||= + begin + require 'rubygems' + require 'sqlite' + 'sqlite' + rescue MissingSourceFile + begin + require 'sqlite3' + 'sqlite3' + rescue MissingSourceFile + end + end + + if db_adapter.nil? + raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3." + end + + ActiveRecord::Base.establish_connection(config[db_adapter]) + load(File.dirname(__FILE__) + "/db/schema.rb") + require File.dirname(__FILE__) + '/../init.rb' +end +require File.dirname(__FILE__) + '/test_models.rb' diff --git a/spec/test_models.rb b/spec/test_models.rb new file mode 100644 index 0000000..f1808bf --- /dev/null +++ b/spec/test_models.rb @@ -0,0 +1,13 @@ +load_schema + +class Customer < ActiveRecord::Base + has_many :phone_numbers +end + +customer = Customer.create(:name => "Boneman") + +class PhoneNumber < ActiveRecord::Base + belongs_to :customer +end + +phone_number = customer.phone_numbers.create(:number => "8675309") \ No newline at end of file