Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Updated to include created objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pjdavis committed Mar 7, 2010
1 parent 276b3e5 commit ec2ac3e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
spec/debug.log
.DS_Store
.DS_Store
spec/db/*.sqlite*
6 changes: 6 additions & 0 deletions lib/identity_map/active_record/base.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions 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
38 changes: 35 additions & 3 deletions 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
28 changes: 28 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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'
13 changes: 13 additions & 0 deletions 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")

0 comments on commit ec2ac3e

Please sign in to comment.