bigamy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/MIT-LICENSE +20 -0
- data/README +11 -0
- data/Rakefile +22 -0
- data/VERSION +1 -0
- data/bigamy.gemspec +76 -0
- data/lib/bigamy.rb +119 -0
- data/lib/bigamy/ar.rb +20 -0
- data/lib/bigamy/mongo.rb +16 -0
- data/lib/bigamy/proxy.rb +154 -0
- data/test/functional/test_helper.rb +1 -0
- data/test/test_helper.rb +27 -0
- data/test/unit/test_ar_side.rb +159 -0
- data/test/unit/test_helper.rb +1 -0
- data/test/unit/test_mongo_side.rb +166 -0
- metadata +163 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ryan Angilly
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "bigamy"
|
5
|
+
gemspec.summary = "Have associations between ActiveRecord objects and MongoMapper documents"
|
6
|
+
gemspec.description = ""
|
7
|
+
gemspec.email = "ryan@angilly.com"
|
8
|
+
gemspec.homepage = "http://github.com/ryana/bigamy"
|
9
|
+
gemspec.authors = ["Ryan Angilly"]
|
10
|
+
|
11
|
+
gemspec.add_development_dependency 'shoulda', '2.11.0'
|
12
|
+
gemspec.add_development_dependency 'mocha', '0.9.8'
|
13
|
+
gemspec.add_development_dependency 'factory_girl', '1.3.1'
|
14
|
+
gemspec.add_development_dependency 'ruby-debug', '0.10.3'
|
15
|
+
gemspec.add_development_dependency 'mongo_mapper', '0.8.2'
|
16
|
+
gemspec.add_development_dependency 'active_record', '>=2.3.5'
|
17
|
+
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
22
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bigamy.gemspec
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{bigamy}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ryan Angilly"]
|
12
|
+
s.date = %q{2010-06-30}
|
13
|
+
s.description = %q{}
|
14
|
+
s.email = %q{ryan@angilly.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"bigamy.gemspec",
|
25
|
+
"lib/bigamy.rb",
|
26
|
+
"lib/bigamy/ar.rb",
|
27
|
+
"lib/bigamy/mongo.rb",
|
28
|
+
"lib/bigamy/proxy.rb",
|
29
|
+
"test/functional/test_helper.rb",
|
30
|
+
"test/test_helper.rb",
|
31
|
+
"test/unit/test_ar_side.rb",
|
32
|
+
"test/unit/test_helper.rb",
|
33
|
+
"test/unit/test_mongo_side.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/ryana/bigamy}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.6}
|
39
|
+
s.summary = %q{Have associations between ActiveRecord objects and MongoMapper documents}
|
40
|
+
s.test_files = [
|
41
|
+
"test/functional/test_helper.rb",
|
42
|
+
"test/test_helper.rb",
|
43
|
+
"test/unit/test_ar_side.rb",
|
44
|
+
"test/unit/test_helper.rb",
|
45
|
+
"test/unit/test_mongo_side.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<shoulda>, ["= 2.11.0"])
|
54
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
|
55
|
+
s.add_development_dependency(%q<factory_girl>, ["= 1.3.1"])
|
56
|
+
s.add_development_dependency(%q<ruby-debug>, ["= 0.10.3"])
|
57
|
+
s.add_development_dependency(%q<mongo_mapper>, ["= 0.8.2"])
|
58
|
+
s.add_development_dependency(%q<active_record>, [">= 2.3.5"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<shoulda>, ["= 2.11.0"])
|
61
|
+
s.add_dependency(%q<mocha>, ["= 0.9.8"])
|
62
|
+
s.add_dependency(%q<factory_girl>, ["= 1.3.1"])
|
63
|
+
s.add_dependency(%q<ruby-debug>, ["= 0.10.3"])
|
64
|
+
s.add_dependency(%q<mongo_mapper>, ["= 0.8.2"])
|
65
|
+
s.add_dependency(%q<active_record>, [">= 2.3.5"])
|
66
|
+
end
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<shoulda>, ["= 2.11.0"])
|
69
|
+
s.add_dependency(%q<mocha>, ["= 0.9.8"])
|
70
|
+
s.add_dependency(%q<factory_girl>, ["= 1.3.1"])
|
71
|
+
s.add_dependency(%q<ruby-debug>, ["= 0.10.3"])
|
72
|
+
s.add_dependency(%q<mongo_mapper>, ["= 0.8.2"])
|
73
|
+
s.add_dependency(%q<active_record>, [">= 2.3.5"])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
data/lib/bigamy.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
require 'bigamy/proxy'
|
5
|
+
require 'bigamy/mongo'
|
6
|
+
require 'bigamy/ar'
|
7
|
+
|
8
|
+
module Bigamy
|
9
|
+
class NewRecordAssignment < StandardError; end
|
10
|
+
|
11
|
+
def self.setup *args
|
12
|
+
args.each do |klass|
|
13
|
+
case
|
14
|
+
when klass.ancestors.include?(::ActiveRecord::Base)
|
15
|
+
Bigamy::ActiveRecord.configure klass
|
16
|
+
when (klass.included_modules & [::MongoMapper::Document, ::MongoMapper::EmbeddedDocument]).present?
|
17
|
+
klass.plugin Bigamy::Mongo
|
18
|
+
else
|
19
|
+
raise "NO #{klass}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Base
|
25
|
+
def self.configure(model)
|
26
|
+
model.class_inheritable_accessor :bigamy_associations
|
27
|
+
model.bigamy_associations = {}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module Mongo
|
32
|
+
|
33
|
+
def self.configure(model)
|
34
|
+
::Bigamy::Base.configure(model)
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
def divorce_everyone
|
39
|
+
self.bigamy_associations.each {|k,v| v.divorce_everyone }
|
40
|
+
end
|
41
|
+
|
42
|
+
def belongs_to_ar name, options = {}, &ext
|
43
|
+
bigamy_associations[name] = MongoBelongsTo.new(self, name, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def has_one_ar name, options = {}, &ext
|
47
|
+
bigamy_associations[name] = MongoHasOne.new(self, name, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def has_many_ar name, options = {}, &ext
|
51
|
+
bigamy_associations[name] = MongoHasMany.new(self, name, options)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module InstanceMethods
|
56
|
+
def set_value c, val
|
57
|
+
write_key c, val
|
58
|
+
end
|
59
|
+
|
60
|
+
def read_val c
|
61
|
+
read_key c
|
62
|
+
end
|
63
|
+
|
64
|
+
def export_id_val i
|
65
|
+
i.to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
def import_id_val i
|
69
|
+
i
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module ActiveRecord
|
75
|
+
def self.configure(model)
|
76
|
+
::Bigamy::Base.configure(model)
|
77
|
+
|
78
|
+
model.extend ClassMethods
|
79
|
+
model.send :include, InstanceMethods
|
80
|
+
end
|
81
|
+
|
82
|
+
module ClassMethods
|
83
|
+
def divorce_everyone
|
84
|
+
self.bigamy_associations.each {|k,v| v.divorce_everyone }
|
85
|
+
end
|
86
|
+
|
87
|
+
def belongs_to_ar name, options = {}, &ext
|
88
|
+
bigamy_associations[name] = ARBelongsTo.new(self, name, options)
|
89
|
+
end
|
90
|
+
|
91
|
+
def has_one_ar name, options = {}, &ext
|
92
|
+
bigamy_associations[name] = ARHasOne.new(self, name, options)
|
93
|
+
end
|
94
|
+
|
95
|
+
def has_many_ar name, options = {}, &ext
|
96
|
+
bigamy_associations[name] = ARHasMany.new(self, name, options)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
module InstanceMethods
|
101
|
+
def set_value c, val
|
102
|
+
self[c] = val
|
103
|
+
end
|
104
|
+
|
105
|
+
def read_val c
|
106
|
+
read_attribute c
|
107
|
+
end
|
108
|
+
|
109
|
+
def export_id_val i
|
110
|
+
i
|
111
|
+
end
|
112
|
+
|
113
|
+
def import_id_val i
|
114
|
+
BSON::ObjectID.from_string(i)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
data/lib/bigamy/ar.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Bigamy
|
2
|
+
|
3
|
+
class ARBelongsTo < BelongsTo
|
4
|
+
end
|
5
|
+
|
6
|
+
class ARHasOne < HasOne
|
7
|
+
def initialize parent, name, options
|
8
|
+
super
|
9
|
+
target_klass.key foreign_key
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ARHasMany < HasMany
|
14
|
+
def initialize parent, name, options
|
15
|
+
super
|
16
|
+
target_klass.key foreign_key
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/lib/bigamy/mongo.rb
ADDED
data/lib/bigamy/proxy.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
module Bigamy
|
2
|
+
|
3
|
+
class Proxy
|
4
|
+
attr_accessor :name, :me, :primary_key, :foreign_key, :klass, :methods_added,
|
5
|
+
:options
|
6
|
+
|
7
|
+
def initialize parent, name, options
|
8
|
+
self.name = name
|
9
|
+
self.me = parent
|
10
|
+
self.primary_key = options.delete(:primary_key) || :id
|
11
|
+
self.klass = options.delete(:class) || target_klass
|
12
|
+
self.methods_added = Set.new
|
13
|
+
self.options = options
|
14
|
+
|
15
|
+
serialize_foreign_key
|
16
|
+
create_accessors
|
17
|
+
serialize_foreign_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def foreign_key
|
21
|
+
options[:foreign_key] || :"#{name.to_s.singularize}_id"
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_accessors
|
25
|
+
methods_added << name
|
26
|
+
methods_added << "#{name}="
|
27
|
+
|
28
|
+
add_getter
|
29
|
+
add_setter
|
30
|
+
end
|
31
|
+
|
32
|
+
def add_getter
|
33
|
+
raise
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_setter
|
37
|
+
raise
|
38
|
+
end
|
39
|
+
|
40
|
+
def serialize_foreign_key
|
41
|
+
target_klass.class_eval <<-EOF
|
42
|
+
def #{foreign_key}
|
43
|
+
import_id_val read_val(:#{foreign_key})
|
44
|
+
end
|
45
|
+
EOF
|
46
|
+
end
|
47
|
+
|
48
|
+
def divorce_everyone
|
49
|
+
methods_added.each {|m| me.send(:undef_method, m) if me.respond_to?(m)}
|
50
|
+
self.methods_added = Set.new
|
51
|
+
end
|
52
|
+
|
53
|
+
def target_klass
|
54
|
+
name.to_s.camelcase.singularize.constantize
|
55
|
+
end
|
56
|
+
|
57
|
+
def target_klass_name
|
58
|
+
name.to_s.underscore.singularize.gsub('/', '_')
|
59
|
+
end
|
60
|
+
|
61
|
+
def root_klass
|
62
|
+
me
|
63
|
+
end
|
64
|
+
|
65
|
+
def root_klass_name
|
66
|
+
me.to_s.underscore.singularize.gsub('/', '_')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class HasOne < Proxy
|
71
|
+
def foreign_key
|
72
|
+
options[:foreign_key] || :"#{root_klass_name}_id"
|
73
|
+
end
|
74
|
+
|
75
|
+
def add_getter
|
76
|
+
me.class_eval <<-EOF
|
77
|
+
def #{name}
|
78
|
+
self.id.nil? ? nil : #{target_klass}.first(:conditions => {:#{foreign_key} => export_id_val(self.id)})
|
79
|
+
end
|
80
|
+
EOF
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_setter
|
84
|
+
me.class_eval <<-EOF
|
85
|
+
def #{name}= v
|
86
|
+
raise NewRecordAssignment.new('Child must be saved') if v.new_record?
|
87
|
+
raise NewRecordAssignment.new('Parent must be saved') if self.new_record?
|
88
|
+
raise TypeError unless v.is_a? #{klass}
|
89
|
+
|
90
|
+
v.#{foreign_key} = export_id_val(self.id)
|
91
|
+
v.save!
|
92
|
+
end
|
93
|
+
EOF
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class HasMany < Proxy
|
98
|
+
def foreign_key
|
99
|
+
options[:foreign_key] || :"#{root_klass_name}_id"
|
100
|
+
end
|
101
|
+
|
102
|
+
def add_getter
|
103
|
+
me.class_eval <<-EOF
|
104
|
+
def #{name}
|
105
|
+
self.id.nil? ? nil : #{target_klass}.all(:conditions => {:#{foreign_key} => export_id_val(self.id)})
|
106
|
+
end
|
107
|
+
EOF
|
108
|
+
end
|
109
|
+
|
110
|
+
def add_setter
|
111
|
+
me.class_eval <<-EOF
|
112
|
+
def #{name}= val
|
113
|
+
raise NewRecordAssignment.new('All children must be saved') if val.select(&:new_record?).present?
|
114
|
+
raise NewRecordAssignment.new('Parent must be saved') if self.new_record?
|
115
|
+
|
116
|
+
val.each {|v| v.send "#{foreign_key}=", export_id_val(self.id); v.save! }
|
117
|
+
end
|
118
|
+
EOF
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
class BelongsTo < Proxy
|
123
|
+
def initialize parent, name, options
|
124
|
+
super
|
125
|
+
end
|
126
|
+
|
127
|
+
def foreign_key
|
128
|
+
options[:foreign_key] || :"#{target_klass_name}_id"
|
129
|
+
end
|
130
|
+
|
131
|
+
def add_getter
|
132
|
+
code = <<-EOF
|
133
|
+
def #{name}
|
134
|
+
self.id.blank? ? nil : #{klass}.first(:conditions => {:#{primary_key} => export_id_val(self.id)})
|
135
|
+
end
|
136
|
+
EOF
|
137
|
+
|
138
|
+
me.class_eval code, __FILE__, __LINE__
|
139
|
+
end
|
140
|
+
|
141
|
+
def add_setter
|
142
|
+
code = <<-EOF
|
143
|
+
def #{name}= val
|
144
|
+
raise NewRecordAssignment if val.new_record?
|
145
|
+
raise TypeError.new("Should get #{klass}") unless val.is_a? #{klass}
|
146
|
+
|
147
|
+
set_value :#{foreign_key}, val.id
|
148
|
+
end
|
149
|
+
EOF
|
150
|
+
|
151
|
+
me.class_eval code, __FILE__, __LINE__
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'mocha'
|
3
|
+
require 'ruby-debug'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'factory_girl'
|
6
|
+
require 'active_record'
|
7
|
+
require 'mongo_mapper'
|
8
|
+
|
9
|
+
MongoMapper.database = 'bigamy-test'
|
10
|
+
ActiveRecord::Base.establish_connection :adapter => 'mysql', :database => 'bigamy_test', :username => 'root', :password => 'ryan'
|
11
|
+
ActiveRecord::Migration.execute 'drop table if exists users'
|
12
|
+
ActiveRecord::Migration.create_table :users do |t|
|
13
|
+
t.string :name
|
14
|
+
t.integer :id
|
15
|
+
t.string :doc_id
|
16
|
+
end
|
17
|
+
|
18
|
+
class Doc
|
19
|
+
include MongoMapper::Document
|
20
|
+
end
|
21
|
+
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
end
|
24
|
+
|
25
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
26
|
+
|
27
|
+
require 'bigamy'
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class TestArSide < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "have Bigamy" do
|
6
|
+
assert Bigamy
|
7
|
+
end
|
8
|
+
|
9
|
+
should "have Bigmany::Mongo plugin" do
|
10
|
+
assert Bigamy::ActiveRecord
|
11
|
+
assert Bigamy::ActiveRecord::ClassMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
Doc.delete_all
|
16
|
+
User.delete_all
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have User & Doc" do
|
20
|
+
assert User
|
21
|
+
assert Doc
|
22
|
+
end
|
23
|
+
|
24
|
+
context "User" do
|
25
|
+
setup do
|
26
|
+
Bigamy.setup User, Doc
|
27
|
+
end
|
28
|
+
|
29
|
+
teardown do
|
30
|
+
User.divorce_everyone
|
31
|
+
end
|
32
|
+
|
33
|
+
should "have divorce_everyone" do
|
34
|
+
assert User.respond_to?(:divorce_everyone)
|
35
|
+
end
|
36
|
+
|
37
|
+
context "that has_one_ar :doc" do
|
38
|
+
setup do
|
39
|
+
User.has_one_ar :doc
|
40
|
+
@doc = Doc.create!
|
41
|
+
end
|
42
|
+
|
43
|
+
should "create accessors" do
|
44
|
+
assert User.new.respond_to?(:doc)
|
45
|
+
assert User.new.respond_to?(:doc=)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "raise on assignment if User is new" do
|
49
|
+
assert_raises(Bigamy::NewRecordAssignment) { User.new.doc = @doc }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "With a created userument" do
|
53
|
+
setup do
|
54
|
+
@user = User.create!
|
55
|
+
@user.doc = @doc
|
56
|
+
end
|
57
|
+
|
58
|
+
should "set @doc.foreign_key on assignment" do
|
59
|
+
assert_equal @doc.user_id, @user.id
|
60
|
+
end
|
61
|
+
|
62
|
+
should "save doc on assignment" do
|
63
|
+
u = Doc.find(@doc.id)
|
64
|
+
assert_equal u.user_id, @user.id
|
65
|
+
end
|
66
|
+
|
67
|
+
should "retrieve doc from user" do
|
68
|
+
d = User.find(@user.id)
|
69
|
+
assert_equal d.doc, @doc
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "that has_many_ar :doc" do
|
75
|
+
setup do
|
76
|
+
User.has_many_ar :docs
|
77
|
+
end
|
78
|
+
|
79
|
+
should "create accessors" do
|
80
|
+
assert User.new.respond_to?(:docs)
|
81
|
+
assert User.new.respond_to?(:docs=)
|
82
|
+
end
|
83
|
+
|
84
|
+
context "with instance and doc" do
|
85
|
+
setup do
|
86
|
+
@user = User.new
|
87
|
+
@u1 = Doc.new
|
88
|
+
@u2 = Doc.new
|
89
|
+
end
|
90
|
+
|
91
|
+
should "raise if user is new" do
|
92
|
+
@u1.save! && @u2.save!
|
93
|
+
assert_raises(Bigamy::NewRecordAssignment) { @user.docs = [@u1, @u2] }
|
94
|
+
end
|
95
|
+
|
96
|
+
should "raise if any docs are new" do
|
97
|
+
@user.save!
|
98
|
+
@u1.save!
|
99
|
+
assert_raises(Bigamy::NewRecordAssignment) { @user.docs = [@u1, @u2] }
|
100
|
+
end
|
101
|
+
|
102
|
+
context "with users & docs saved" do
|
103
|
+
setup do
|
104
|
+
@user.save! && @u1.save! && @u2.save!
|
105
|
+
@user.docs = [@u1, @u2]
|
106
|
+
end
|
107
|
+
|
108
|
+
should "assign user_id on docs" do
|
109
|
+
assert Doc.find(@u1.id, @u2.id).all? {|x| x.user_id == @user.id}
|
110
|
+
end
|
111
|
+
|
112
|
+
should "retrieve targets" do
|
113
|
+
assert_same_elements [@u1, @u2], User.find(@user.id).docs
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "that belongs_to_ar :doc" do
|
121
|
+
setup do
|
122
|
+
User.belongs_to_ar :doc
|
123
|
+
end
|
124
|
+
|
125
|
+
should "create accessors" do
|
126
|
+
assert User.new.respond_to?(:doc)
|
127
|
+
assert User.new.respond_to?(:doc=)
|
128
|
+
end
|
129
|
+
|
130
|
+
context "with an instance and doc" do
|
131
|
+
setup do
|
132
|
+
@user = User.new
|
133
|
+
@doc = Doc.new
|
134
|
+
end
|
135
|
+
|
136
|
+
should "raise if assigning a new Doc" do
|
137
|
+
assert_raises(Bigamy::NewRecordAssignment) do
|
138
|
+
@user.doc = @doc
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "with a saved doc" do
|
143
|
+
setup { @doc.save! }
|
144
|
+
|
145
|
+
should "save user" do
|
146
|
+
@user.doc = @doc
|
147
|
+
@user.save!
|
148
|
+
|
149
|
+
assert !@user.doc_id.nil?
|
150
|
+
assert_equal @user.doc_id, @doc.id
|
151
|
+
assert_equal 1, Doc.count
|
152
|
+
assert_equal 1, User.count
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class TestMongoSide < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "have Bigamy" do
|
6
|
+
assert Bigamy
|
7
|
+
end
|
8
|
+
|
9
|
+
should "have Bigmany::Mongo plugin" do
|
10
|
+
assert Bigamy::Mongo
|
11
|
+
assert Bigamy::Mongo::ClassMethods
|
12
|
+
assert Bigamy::Mongo::InstanceMethods
|
13
|
+
assert Bigamy::Mongo.respond_to?(:configure)
|
14
|
+
end
|
15
|
+
|
16
|
+
def setup
|
17
|
+
User.delete_all
|
18
|
+
Doc.delete_all
|
19
|
+
end
|
20
|
+
|
21
|
+
should "have Doc & User" do
|
22
|
+
assert Doc
|
23
|
+
assert User
|
24
|
+
end
|
25
|
+
|
26
|
+
context "Doc" do
|
27
|
+
setup do
|
28
|
+
Bigamy.setup Doc, User
|
29
|
+
end
|
30
|
+
|
31
|
+
teardown do
|
32
|
+
Doc.divorce_everyone
|
33
|
+
end
|
34
|
+
|
35
|
+
should "setup bigamy" do
|
36
|
+
assert Doc.included_modules.include?(Bigamy::Mongo::InstanceMethods)
|
37
|
+
assert User.included_modules.include?(Bigamy::ActiveRecord::InstanceMethods)
|
38
|
+
end
|
39
|
+
|
40
|
+
context "that has_one_ar :user" do
|
41
|
+
setup do
|
42
|
+
Doc.has_one_ar :user
|
43
|
+
@user = User.create!
|
44
|
+
end
|
45
|
+
|
46
|
+
should "create accessors" do
|
47
|
+
assert Doc.new.respond_to?(:user)
|
48
|
+
assert Doc.new.respond_to?(:user=)
|
49
|
+
end
|
50
|
+
|
51
|
+
should "raise on assignment if Doc is new" do
|
52
|
+
assert_raises(Bigamy::NewRecordAssignment) { Doc.new.user = @user }
|
53
|
+
end
|
54
|
+
|
55
|
+
context "With a created document" do
|
56
|
+
setup do
|
57
|
+
@doc = Doc.create!
|
58
|
+
@doc.user = @user
|
59
|
+
end
|
60
|
+
|
61
|
+
should "set @user.foreign_key on assignment" do
|
62
|
+
assert_equal @user.doc_id, @doc.id
|
63
|
+
end
|
64
|
+
|
65
|
+
should "save user on assignment" do
|
66
|
+
u = User.find(@user.id)
|
67
|
+
assert_equal u.doc_id, @doc.id
|
68
|
+
end
|
69
|
+
|
70
|
+
should "retrieve user from doc" do
|
71
|
+
d = Doc.find(@doc.id)
|
72
|
+
assert_equal d.user, @user
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "that has_many_ar :user" do
|
78
|
+
setup do
|
79
|
+
Doc.has_many_ar :users
|
80
|
+
end
|
81
|
+
|
82
|
+
should "create accessors" do
|
83
|
+
assert Doc.new.respond_to?(:users)
|
84
|
+
assert Doc.new.respond_to?(:users=)
|
85
|
+
end
|
86
|
+
|
87
|
+
context "with instance and user" do
|
88
|
+
setup do
|
89
|
+
@doc = Doc.new
|
90
|
+
@u1 = User.new
|
91
|
+
@u2 = User.new
|
92
|
+
end
|
93
|
+
|
94
|
+
should "raise if doc is new" do
|
95
|
+
@u1.save! && @u2.save!
|
96
|
+
assert_raises(Bigamy::NewRecordAssignment) { @doc.users = [@u1, @u2] }
|
97
|
+
end
|
98
|
+
|
99
|
+
should "raise if any users are new" do
|
100
|
+
@doc.save!
|
101
|
+
@u1.save!
|
102
|
+
assert_raises(Bigamy::NewRecordAssignment) { @doc.users = [@u1, @u2] }
|
103
|
+
end
|
104
|
+
|
105
|
+
context "with docs & users saved" do
|
106
|
+
setup do
|
107
|
+
@doc.save! && @u1.save! && @u2.save!
|
108
|
+
@doc.users = [@u1, @u2]
|
109
|
+
end
|
110
|
+
|
111
|
+
should "assign doc_id on users" do
|
112
|
+
assert User.find(@u1.id, @u2.id).all? {|x| x.doc_id == @doc.id}
|
113
|
+
end
|
114
|
+
|
115
|
+
should "retrieve targets" do
|
116
|
+
assert_same_elements [@u1, @u2], Doc.find(@doc.id).users
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "that belongs_to_ar :user" do
|
124
|
+
setup do
|
125
|
+
Doc.belongs_to_ar :user
|
126
|
+
end
|
127
|
+
|
128
|
+
should "create accessors" do
|
129
|
+
assert Doc.new.respond_to?(:user)
|
130
|
+
assert Doc.new.respond_to?(:user=)
|
131
|
+
end
|
132
|
+
|
133
|
+
should "create key" do
|
134
|
+
assert Doc.new.respond_to?(:user_id)
|
135
|
+
assert Doc.new.respond_to?(:user_id=)
|
136
|
+
end
|
137
|
+
|
138
|
+
context "with an instance and user" do
|
139
|
+
setup do
|
140
|
+
@doc = Doc.new
|
141
|
+
@user = User.new
|
142
|
+
end
|
143
|
+
|
144
|
+
should "raise if assigning a new User" do
|
145
|
+
assert_raises(Bigamy::NewRecordAssignment) do
|
146
|
+
@doc.user = @user
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "with a saved user" do
|
151
|
+
setup { @user.save! }
|
152
|
+
|
153
|
+
should "save doc" do
|
154
|
+
@doc.user = @user
|
155
|
+
@doc.save!
|
156
|
+
|
157
|
+
assert !@doc.user_id.nil?
|
158
|
+
assert_equal @doc.user_id, @user.id
|
159
|
+
assert_equal 1, User.count
|
160
|
+
assert_equal 1, Doc.count
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bigamy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ryan Angilly
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-30 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 11
|
30
|
+
- 0
|
31
|
+
version: 2.11.0
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: mocha
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 9
|
44
|
+
- 8
|
45
|
+
version: 0.9.8
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: factory_girl
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 1
|
57
|
+
- 3
|
58
|
+
- 1
|
59
|
+
version: 1.3.1
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: ruby-debug
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
- 10
|
72
|
+
- 3
|
73
|
+
version: 0.10.3
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: mongo_mapper
|
78
|
+
prerelease: false
|
79
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
- 8
|
86
|
+
- 2
|
87
|
+
version: 0.8.2
|
88
|
+
type: :development
|
89
|
+
version_requirements: *id005
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: active_record
|
92
|
+
prerelease: false
|
93
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 2
|
99
|
+
- 3
|
100
|
+
- 5
|
101
|
+
version: 2.3.5
|
102
|
+
type: :development
|
103
|
+
version_requirements: *id006
|
104
|
+
description: ""
|
105
|
+
email: ryan@angilly.com
|
106
|
+
executables: []
|
107
|
+
|
108
|
+
extensions: []
|
109
|
+
|
110
|
+
extra_rdoc_files:
|
111
|
+
- README
|
112
|
+
files:
|
113
|
+
- .gitignore
|
114
|
+
- MIT-LICENSE
|
115
|
+
- README
|
116
|
+
- Rakefile
|
117
|
+
- VERSION
|
118
|
+
- bigamy.gemspec
|
119
|
+
- lib/bigamy.rb
|
120
|
+
- lib/bigamy/ar.rb
|
121
|
+
- lib/bigamy/mongo.rb
|
122
|
+
- lib/bigamy/proxy.rb
|
123
|
+
- test/functional/test_helper.rb
|
124
|
+
- test/test_helper.rb
|
125
|
+
- test/unit/test_ar_side.rb
|
126
|
+
- test/unit/test_helper.rb
|
127
|
+
- test/unit/test_mongo_side.rb
|
128
|
+
has_rdoc: true
|
129
|
+
homepage: http://github.com/ryana/bigamy
|
130
|
+
licenses: []
|
131
|
+
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options:
|
134
|
+
- --charset=UTF-8
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
version: "0"
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
version: "0"
|
151
|
+
requirements: []
|
152
|
+
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 1.3.6
|
155
|
+
signing_key:
|
156
|
+
specification_version: 3
|
157
|
+
summary: Have associations between ActiveRecord objects and MongoMapper documents
|
158
|
+
test_files:
|
159
|
+
- test/functional/test_helper.rb
|
160
|
+
- test/test_helper.rb
|
161
|
+
- test/unit/test_ar_side.rb
|
162
|
+
- test/unit/test_helper.rb
|
163
|
+
- test/unit/test_mongo_side.rb
|