mrkurt-mongo_mapper 0.6.8 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.8
1
+ 0.6.9
@@ -137,3 +137,4 @@ require 'mongo_mapper/associations/many_embedded_proxy'
137
137
  require 'mongo_mapper/associations/many_embedded_polymorphic_proxy'
138
138
  require 'mongo_mapper/associations/many_documents_as_proxy'
139
139
  require 'mongo_mapper/associations/one_proxy'
140
+ require 'mongo_mapper/associations/in_array_proxy'
@@ -4,7 +4,7 @@ module MongoMapper
4
4
  attr_reader :type, :name, :options, :finder_options
5
5
 
6
6
  # Options that should not be considered MongoDB query options/criteria
7
- AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :polymorphic]
7
+ AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :polymorphic]
8
8
 
9
9
  def initialize(type, name, options={}, &extension)
10
10
  @type, @name, @options, @finder_options = type, name, {}, {}
@@ -56,6 +56,14 @@ module MongoMapper
56
56
  def as?
57
57
  !!@options[:as]
58
58
  end
59
+
60
+ def in_array?
61
+ !!@options[:in]
62
+ end
63
+
64
+ def embeddable?
65
+ many? && klass.embeddable?
66
+ end
59
67
 
60
68
  def type_key_name
61
69
  @type_key_name ||= many? ? '_type' : "#{as}_type"
@@ -73,10 +81,6 @@ module MongoMapper
73
81
  @ivar ||= "@_#{name}"
74
82
  end
75
83
 
76
- def embeddable?
77
- many? && klass.embeddable?
78
- end
79
-
80
84
  def proxy_class
81
85
  @proxy_class ||= begin
82
86
  if many?
@@ -87,6 +91,8 @@ module MongoMapper
87
91
  ManyPolymorphicProxy
88
92
  elsif as?
89
93
  ManyDocumentsAsProxy
94
+ elsif in_array?
95
+ InArrayProxy
90
96
  else
91
97
  ManyDocumentsProxy
92
98
  end
@@ -96,8 +102,8 @@ module MongoMapper
96
102
  else
97
103
  polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
98
104
  end
99
- end # end begin
100
- end # end proxy_class
105
+ end
106
+ end
101
107
 
102
108
  private
103
109
 
@@ -0,0 +1,137 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class InArrayProxy < Collection
4
+ include ::MongoMapper::Finders
5
+
6
+ def find(*args)
7
+ options = args.extract_options!
8
+
9
+ case args.first
10
+ when :first
11
+ first(options)
12
+ when :last
13
+ last(options)
14
+ when :all
15
+ all(options)
16
+ else
17
+ klass.find(*scoped_ids(args) << scoped_options(options))
18
+ end
19
+ end
20
+
21
+ def find!(*args)
22
+ options = args.extract_options!
23
+
24
+ case args.first
25
+ when :first
26
+ first(options)
27
+ when :last
28
+ last(options)
29
+ when :all
30
+ all(options)
31
+ else
32
+ klass.find!(*scoped_ids(args) << scoped_options(options))
33
+ end
34
+ end
35
+
36
+ def paginate(options)
37
+ klass.paginate(scoped_options(options))
38
+ end
39
+
40
+ def all(options={})
41
+ klass.all(scoped_options(options))
42
+ end
43
+
44
+ def first(options={})
45
+ klass.first(scoped_options(options))
46
+ end
47
+
48
+ def last(options={})
49
+ klass.last(scoped_options(options))
50
+ end
51
+
52
+ def count(options={})
53
+ options.blank? ? ids.size : klass.count(scoped_options(options))
54
+ end
55
+
56
+ def destroy_all(options={})
57
+ all(options).each do |doc|
58
+ ids.delete(doc.id)
59
+ doc.destroy
60
+ end
61
+ reset
62
+ end
63
+
64
+ def delete_all(options={})
65
+ docs = all(options.merge(:select => ['_id']))
66
+ docs.each { |doc| ids.delete(doc.id) }
67
+ klass.delete(docs.map(&:id))
68
+ reset
69
+ end
70
+
71
+ def nullify
72
+ replace([])
73
+ reset
74
+ end
75
+
76
+ def create(attrs={})
77
+ doc = klass.create(attrs)
78
+ unless doc.new?
79
+ ids << doc.id
80
+ owner.save
81
+ end
82
+ doc
83
+ end
84
+
85
+ def create!(attrs={})
86
+ doc = klass.create!(attrs)
87
+ unless doc.new?
88
+ ids << doc.id
89
+ owner.save
90
+ end
91
+ doc
92
+ end
93
+
94
+ def <<(*docs)
95
+ flatten_deeper(docs).each do |doc|
96
+ doc.save if doc.new?
97
+ unless ids.include?(doc.id)
98
+ ids << doc.id
99
+ end
100
+ end
101
+ reset
102
+ end
103
+ alias_method :push, :<<
104
+ alias_method :concat, :<<
105
+
106
+ def replace(docs)
107
+ doc_ids = docs.map do |doc|
108
+ doc.save if doc.new?
109
+ doc.id
110
+ end
111
+ ids.replace(doc_ids.uniq)
112
+ reset
113
+ end
114
+
115
+ private
116
+ def scoped_conditions
117
+ {:_id => ids}
118
+ end
119
+
120
+ def scoped_options(options)
121
+ reflection.finder_options.merge(options).merge(scoped_conditions)
122
+ end
123
+
124
+ def scoped_ids(args)
125
+ args.flatten.reject { |id| !ids.include?(id) }
126
+ end
127
+
128
+ def find_target
129
+ ids.blank? ? [] : all
130
+ end
131
+
132
+ def ids
133
+ owner[options[:in]]
134
+ end
135
+ end
136
+ end
137
+ end
@@ -123,7 +123,7 @@ module MongoMapper
123
123
  end
124
124
 
125
125
  def find_by_id(id)
126
- find_one(:_id => id)
126
+ find(id)
127
127
  end
128
128
 
129
129
  def count(options={})
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_mapper}
8
- s.version = "0.6.8"
8
+ s.version = "0.6.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Nunemaker"]
12
- s.date = %q{2009-12-15}
12
+ s.date = %q{2010-01-01}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -30,11 +30,13 @@ Gem::Specification.new do |s|
30
30
  "lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb",
31
31
  "lib/mongo_mapper/associations/belongs_to_proxy.rb",
32
32
  "lib/mongo_mapper/associations/collection.rb",
33
+ "lib/mongo_mapper/associations/in_array_proxy.rb",
33
34
  "lib/mongo_mapper/associations/many_documents_as_proxy.rb",
34
35
  "lib/mongo_mapper/associations/many_documents_proxy.rb",
35
36
  "lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb",
36
37
  "lib/mongo_mapper/associations/many_embedded_proxy.rb",
37
38
  "lib/mongo_mapper/associations/many_polymorphic_proxy.rb",
39
+ "lib/mongo_mapper/associations/one_proxy.rb",
38
40
  "lib/mongo_mapper/associations/proxy.rb",
39
41
  "lib/mongo_mapper/callbacks.rb",
40
42
  "lib/mongo_mapper/dirty.rb",
@@ -43,7 +45,7 @@ Gem::Specification.new do |s|
43
45
  "lib/mongo_mapper/embedded_document.rb",
44
46
  "lib/mongo_mapper/finder_options.rb",
45
47
  "lib/mongo_mapper/key.rb",
46
- "lib/mongo_mapper/observing.rb",
48
+ "lib/mongo_mapper/mongo_mapper.rb",
47
49
  "lib/mongo_mapper/pagination.rb",
48
50
  "lib/mongo_mapper/rails_compatibility/document.rb",
49
51
  "lib/mongo_mapper/rails_compatibility/embedded_document.rb",
@@ -56,11 +58,13 @@ Gem::Specification.new do |s|
56
58
  "test/NOTE_ON_TESTING",
57
59
  "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
58
60
  "test/functional/associations/test_belongs_to_proxy.rb",
61
+ "test/functional/associations/test_in_array_proxy.rb",
59
62
  "test/functional/associations/test_many_documents_as_proxy.rb",
60
63
  "test/functional/associations/test_many_documents_proxy.rb",
61
64
  "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
62
65
  "test/functional/associations/test_many_embedded_proxy.rb",
63
66
  "test/functional/associations/test_many_polymorphic_proxy.rb",
67
+ "test/functional/associations/test_one_proxy.rb",
64
68
  "test/functional/test_associations.rb",
65
69
  "test/functional/test_binary.rb",
66
70
  "test/functional/test_callbacks.rb",
@@ -68,6 +72,7 @@ Gem::Specification.new do |s|
68
72
  "test/functional/test_document.rb",
69
73
  "test/functional/test_embedded_document.rb",
70
74
  "test/functional/test_logger.rb",
75
+ "test/functional/test_modifiers.rb",
71
76
  "test/functional/test_pagination.rb",
72
77
  "test/functional/test_rails_compatibility.rb",
73
78
  "test/functional/test_string_id_compatibility.rb",
@@ -85,7 +90,6 @@ Gem::Specification.new do |s|
85
90
  "test/unit/test_finder_options.rb",
86
91
  "test/unit/test_key.rb",
87
92
  "test/unit/test_mongo_mapper.rb",
88
- "test/unit/test_observing.rb",
89
93
  "test/unit/test_pagination.rb",
90
94
  "test/unit/test_rails_compatibility.rb",
91
95
  "test/unit/test_serializations.rb",
@@ -101,11 +105,13 @@ Gem::Specification.new do |s|
101
105
  s.test_files = [
102
106
  "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
103
107
  "test/functional/associations/test_belongs_to_proxy.rb",
108
+ "test/functional/associations/test_in_array_proxy.rb",
104
109
  "test/functional/associations/test_many_documents_as_proxy.rb",
105
110
  "test/functional/associations/test_many_documents_proxy.rb",
106
111
  "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
107
112
  "test/functional/associations/test_many_embedded_proxy.rb",
108
113
  "test/functional/associations/test_many_polymorphic_proxy.rb",
114
+ "test/functional/associations/test_one_proxy.rb",
109
115
  "test/functional/test_associations.rb",
110
116
  "test/functional/test_binary.rb",
111
117
  "test/functional/test_callbacks.rb",
@@ -113,6 +119,7 @@ Gem::Specification.new do |s|
113
119
  "test/functional/test_document.rb",
114
120
  "test/functional/test_embedded_document.rb",
115
121
  "test/functional/test_logger.rb",
122
+ "test/functional/test_modifiers.rb",
116
123
  "test/functional/test_pagination.rb",
117
124
  "test/functional/test_rails_compatibility.rb",
118
125
  "test/functional/test_string_id_compatibility.rb",
@@ -130,7 +137,6 @@ Gem::Specification.new do |s|
130
137
  "test/unit/test_finder_options.rb",
131
138
  "test/unit/test_key.rb",
132
139
  "test/unit/test_mongo_mapper.rb",
133
- "test/unit/test_observing.rb",
134
140
  "test/unit/test_pagination.rb",
135
141
  "test/unit/test_rails_compatibility.rb",
136
142
  "test/unit/test_serializations.rb",
@@ -145,7 +151,7 @@ Gem::Specification.new do |s|
145
151
 
146
152
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
147
153
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
148
- s.add_runtime_dependency(%q<mongo>, ["= 0.18.1"])
154
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18.2"])
149
155
  s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
150
156
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
151
157
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -153,7 +159,7 @@ Gem::Specification.new do |s|
153
159
  s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
154
160
  else
155
161
  s.add_dependency(%q<activesupport>, [">= 2.3"])
156
- s.add_dependency(%q<mongo>, ["= 0.18.1"])
162
+ s.add_dependency(%q<mongo>, ["= 0.18.2"])
157
163
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
158
164
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
159
165
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -162,7 +168,7 @@ Gem::Specification.new do |s|
162
168
  end
163
169
  else
164
170
  s.add_dependency(%q<activesupport>, [">= 2.3"])
165
- s.add_dependency(%q<mongo>, ["= 0.18.1"])
171
+ s.add_dependency(%q<mongo>, ["= 0.18.2"])
166
172
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
167
173
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
168
174
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -0,0 +1,180 @@
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{mrkurt-mongo_mapper}
8
+ s.version = "0.6.9"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Nunemaker", "Kurt Mackey"]
12
+ s.date = %q{2010-01-02}
13
+ s.default_executable = %q{mmconsole}
14
+ s.email = %q{mrkurt@gmail.com}
15
+ s.executables = ["mmconsole"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "bin/mmconsole",
27
+ "lib/mongo_mapper.rb",
28
+ "lib/mongo_mapper/associations.rb",
29
+ "lib/mongo_mapper/associations/base.rb",
30
+ "lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb",
31
+ "lib/mongo_mapper/associations/belongs_to_proxy.rb",
32
+ "lib/mongo_mapper/associations/collection.rb",
33
+ "lib/mongo_mapper/associations/in_array_proxy.rb",
34
+ "lib/mongo_mapper/associations/many_documents_as_proxy.rb",
35
+ "lib/mongo_mapper/associations/many_documents_proxy.rb",
36
+ "lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb",
37
+ "lib/mongo_mapper/associations/many_embedded_proxy.rb",
38
+ "lib/mongo_mapper/associations/many_polymorphic_proxy.rb",
39
+ "lib/mongo_mapper/associations/one_proxy.rb",
40
+ "lib/mongo_mapper/associations/proxy.rb",
41
+ "lib/mongo_mapper/callbacks.rb",
42
+ "lib/mongo_mapper/dirty.rb",
43
+ "lib/mongo_mapper/document.rb",
44
+ "lib/mongo_mapper/dynamic_finder.rb",
45
+ "lib/mongo_mapper/embedded_document.rb",
46
+ "lib/mongo_mapper/finder_options.rb",
47
+ "lib/mongo_mapper/key.rb",
48
+ "lib/mongo_mapper/mongo_mapper.rb",
49
+ "lib/mongo_mapper/pagination.rb",
50
+ "lib/mongo_mapper/rails_compatibility/document.rb",
51
+ "lib/mongo_mapper/rails_compatibility/embedded_document.rb",
52
+ "lib/mongo_mapper/serialization.rb",
53
+ "lib/mongo_mapper/serializers/json_serializer.rb",
54
+ "lib/mongo_mapper/support.rb",
55
+ "lib/mongo_mapper/validations.rb",
56
+ "mongo_mapper.gemspec",
57
+ "mrkurt-mongo_mapper.gemspec",
58
+ "specs.watchr",
59
+ "test/NOTE_ON_TESTING",
60
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
61
+ "test/functional/associations/test_belongs_to_proxy.rb",
62
+ "test/functional/associations/test_in_array_proxy.rb",
63
+ "test/functional/associations/test_many_documents_as_proxy.rb",
64
+ "test/functional/associations/test_many_documents_proxy.rb",
65
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
66
+ "test/functional/associations/test_many_embedded_proxy.rb",
67
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
68
+ "test/functional/associations/test_one_proxy.rb",
69
+ "test/functional/test_associations.rb",
70
+ "test/functional/test_binary.rb",
71
+ "test/functional/test_callbacks.rb",
72
+ "test/functional/test_dirty.rb",
73
+ "test/functional/test_document.rb",
74
+ "test/functional/test_embedded_document.rb",
75
+ "test/functional/test_logger.rb",
76
+ "test/functional/test_modifiers.rb",
77
+ "test/functional/test_pagination.rb",
78
+ "test/functional/test_rails_compatibility.rb",
79
+ "test/functional/test_string_id_compatibility.rb",
80
+ "test/functional/test_validations.rb",
81
+ "test/models.rb",
82
+ "test/support/custom_matchers.rb",
83
+ "test/support/timing.rb",
84
+ "test/test_helper.rb",
85
+ "test/unit/associations/test_base.rb",
86
+ "test/unit/associations/test_proxy.rb",
87
+ "test/unit/serializers/test_json_serializer.rb",
88
+ "test/unit/test_document.rb",
89
+ "test/unit/test_dynamic_finder.rb",
90
+ "test/unit/test_embedded_document.rb",
91
+ "test/unit/test_finder_options.rb",
92
+ "test/unit/test_key.rb",
93
+ "test/unit/test_mongo_mapper.rb",
94
+ "test/unit/test_pagination.rb",
95
+ "test/unit/test_rails_compatibility.rb",
96
+ "test/unit/test_serializations.rb",
97
+ "test/unit/test_support.rb",
98
+ "test/unit/test_time_zones.rb",
99
+ "test/unit/test_validations.rb"
100
+ ]
101
+ s.homepage = %q{http://github.com/mrkurt/mongomapper}
102
+ s.rdoc_options = ["--charset=UTF-8"]
103
+ s.require_paths = ["lib"]
104
+ s.rubygems_version = %q{1.3.5}
105
+ s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
106
+ s.test_files = [
107
+ "test/test_helper.rb",
108
+ "test/functional/test_document.rb",
109
+ "test/functional/test_pagination.rb",
110
+ "test/functional/test_dirty.rb",
111
+ "test/functional/test_validations.rb",
112
+ "test/functional/test_callbacks.rb",
113
+ "test/functional/test_rails_compatibility.rb",
114
+ "test/functional/test_associations.rb",
115
+ "test/functional/test_string_id_compatibility.rb",
116
+ "test/functional/test_binary.rb",
117
+ "test/functional/test_logger.rb",
118
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
119
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
120
+ "test/functional/associations/test_many_embedded_proxy.rb",
121
+ "test/functional/associations/test_many_documents_as_proxy.rb",
122
+ "test/functional/associations/test_many_documents_proxy.rb",
123
+ "test/functional/associations/test_one_proxy.rb",
124
+ "test/functional/associations/test_in_array_proxy.rb",
125
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
126
+ "test/functional/associations/test_belongs_to_proxy.rb",
127
+ "test/functional/test_modifiers.rb",
128
+ "test/functional/test_embedded_document.rb",
129
+ "test/models.rb",
130
+ "test/support/timing.rb",
131
+ "test/support/custom_matchers.rb",
132
+ "test/unit/test_document.rb",
133
+ "test/unit/serializers/test_json_serializer.rb",
134
+ "test/unit/test_finder_options.rb",
135
+ "test/unit/test_pagination.rb",
136
+ "test/unit/test_validations.rb",
137
+ "test/unit/test_rails_compatibility.rb",
138
+ "test/unit/test_dynamic_finder.rb",
139
+ "test/unit/test_support.rb",
140
+ "test/unit/test_serializations.rb",
141
+ "test/unit/test_key.rb",
142
+ "test/unit/test_time_zones.rb",
143
+ "test/unit/test_mongo_mapper.rb",
144
+ "test/unit/associations/test_proxy.rb",
145
+ "test/unit/associations/test_base.rb",
146
+ "test/unit/test_embedded_document.rb"
147
+ ]
148
+
149
+ if s.respond_to? :specification_version then
150
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
151
+ s.specification_version = 3
152
+
153
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
154
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
155
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18.2"])
156
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
157
+ s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
158
+ s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
159
+ s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
160
+ s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
161
+ else
162
+ s.add_dependency(%q<activesupport>, [">= 2.3"])
163
+ s.add_dependency(%q<mongo>, ["= 0.18.2"])
164
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
165
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
166
+ s.add_dependency(%q<shoulda>, ["= 2.10.2"])
167
+ s.add_dependency(%q<timecop>, ["= 0.3.1"])
168
+ s.add_dependency(%q<mocha>, ["= 0.9.8"])
169
+ end
170
+ else
171
+ s.add_dependency(%q<activesupport>, [">= 2.3"])
172
+ s.add_dependency(%q<mongo>, ["= 0.18.2"])
173
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
174
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
175
+ s.add_dependency(%q<shoulda>, ["= 2.10.2"])
176
+ s.add_dependency(%q<timecop>, ["= 0.3.1"])
177
+ s.add_dependency(%q<mocha>, ["= 0.9.8"])
178
+ end
179
+ end
180
+
@@ -0,0 +1,309 @@
1
+ require 'test_helper'
2
+
3
+ class InArrayProxyTest < Test::Unit::TestCase
4
+ context "description" do
5
+ setup do
6
+ class ::List
7
+ include MongoMapper::Document
8
+ key :name, String, :required => true
9
+ end
10
+
11
+ class ::User
12
+ include MongoMapper::Document
13
+ key :name, String, :required => true
14
+ key :list_ids, Array
15
+ many :lists, :in => :list_ids
16
+ end
17
+ User.collection.remove
18
+ List.collection.remove
19
+ end
20
+
21
+ teardown do
22
+ Object.send :remove_const, 'List' if defined?(::List)
23
+ Object.send :remove_const, 'User' if defined?(::User)
24
+ end
25
+
26
+ should "default reader to empty array" do
27
+ User.new.lists.should == []
28
+ end
29
+
30
+ should "allow adding to association like it was an array" do
31
+ user = User.new(:name => 'John')
32
+ user.lists << List.new(:name => 'Foo1!')
33
+ user.lists.push List.new(:name => 'Foo2!')
34
+ user.lists.concat List.new(:name => 'Foo3!')
35
+ user.lists.size.should == 3
36
+ end
37
+
38
+ should "ignore adding duplicate ids" do
39
+ user = User.create(:name => 'John')
40
+ list = List.create(:name => 'Foo')
41
+ user.lists << list
42
+ user.lists << list
43
+ user.lists << list
44
+
45
+ user.list_ids.should == [list.id]
46
+ user.lists.count.should == 1
47
+ end
48
+
49
+ should "be able to replace the association" do
50
+ user = User.new(:name => 'John')
51
+ list = List.new(:name => 'Foo')
52
+ user.lists = [list]
53
+ user.save.should be_true
54
+
55
+ user.reload
56
+ user.list_ids.should == [list.id]
57
+ user.lists.size.should == 1
58
+ user.lists[0].name.should == 'Foo'
59
+ end
60
+
61
+ context "create" do
62
+ setup do
63
+ @user = User.create(:name => 'John')
64
+ @list = @user.lists.create(:name => 'Foo!')
65
+ end
66
+
67
+ should "add id to key" do
68
+ @user.list_ids.should include(@list.id)
69
+ end
70
+
71
+ should "persist id addition to key in database" do
72
+ @user.reload
73
+ @user.list_ids.should include(@list.id)
74
+ end
75
+
76
+ should "add doc to association" do
77
+ @user.lists.should include(@list)
78
+ end
79
+
80
+ should "save doc" do
81
+ @list.should_not be_new
82
+ end
83
+ end
84
+
85
+ context "create!" do
86
+ setup do
87
+ @user = User.create(:name => 'John')
88
+ @list = @user.lists.create!(:name => 'Foo!')
89
+ end
90
+
91
+ should "add id to key" do
92
+ @user.list_ids.should include(@list.id)
93
+ end
94
+
95
+ should "persist id addition to key in database" do
96
+ @user.reload
97
+ @user.list_ids.should include(@list.id)
98
+ end
99
+
100
+ should "add doc to association" do
101
+ @user.lists.should include(@list)
102
+ end
103
+
104
+ should "save doc" do
105
+ @list.should_not be_new
106
+ end
107
+
108
+ should "raise exception if invalid" do
109
+ assert_raises(MongoMapper::DocumentNotValid) do
110
+ @user.lists.create!
111
+ end
112
+ end
113
+ end
114
+
115
+ context "Finding scoped to association" do
116
+ setup do
117
+ @user = User.create(:name => 'John')
118
+ @user2 = User.create(:name => 'Brandon')
119
+ @list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
120
+ @list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
121
+ @list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
122
+ end
123
+
124
+ context "all" do
125
+ should "work" do
126
+ @user.lists.find(:all, :order => :position.asc).should == [@list1, @list2]
127
+ @user.lists.all(:order => :position.asc).should == [@list1, @list2]
128
+ end
129
+
130
+ should "work with conditions" do
131
+ @user.lists.find(:all, :name => 'Foo 1').should == [@list1]
132
+ @user.lists.all(:name => 'Foo 1').should == [@list1]
133
+ end
134
+ end
135
+
136
+ context "first" do
137
+ should "work" do
138
+ @user.lists.find(:first, :order => 'position').should == @list1
139
+ @user.lists.first(:order => 'position').should == @list1
140
+ end
141
+
142
+ should "work with conditions" do
143
+ @user.lists.find(:first, :position => 2).should == @list2
144
+ @user.lists.first(:position => 2).should == @list2
145
+ end
146
+ end
147
+
148
+ context "last" do
149
+ should "work" do
150
+ @user.lists.find(:last, :order => 'position').should == @list2
151
+ @user.lists.last(:order => 'position').should == @list2
152
+ end
153
+
154
+ should "work with conditions" do
155
+ @user.lists.find(:last, :position => 2, :order => 'position').should == @list2
156
+ @user.lists.last(:position => 2, :order => 'position').should == @list2
157
+ end
158
+ end
159
+
160
+ context "with one id" do
161
+ should "work for id in association" do
162
+ @user.lists.find(@list1.id).should == @list1
163
+ end
164
+
165
+ should "not work for id not in association" do
166
+ @user.lists.find(@list3.id).should be_nil
167
+ end
168
+
169
+ should "raise error when using ! and not found" do
170
+ assert_raises MongoMapper::DocumentNotFound do
171
+ @user.lists.find!(@list3.id)
172
+ end
173
+ end
174
+ end
175
+
176
+ context "with multiple ids" do
177
+ should "work for ids in association" do
178
+ @user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
179
+ end
180
+
181
+ should "not work for ids not in association" do
182
+ @user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
183
+ end
184
+ end
185
+
186
+ context "with #paginate" do
187
+ setup do
188
+ @lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
189
+ end
190
+
191
+ should "return total pages" do
192
+ @lists.total_pages.should == 2
193
+ end
194
+
195
+ should "return total entries" do
196
+ @lists.total_entries.should == 2
197
+ end
198
+
199
+ should "return the subject" do
200
+ @lists.collect(&:name).should == ['Foo 1']
201
+ end
202
+ end
203
+
204
+ context "dynamic finders" do
205
+ should "work with single key" do
206
+ @user.lists.find_by_name('Foo 1').should == @list1
207
+ @user.lists.find_by_name!('Foo 1').should == @list1
208
+ @user.lists.find_by_name('Foo 3').should be_nil
209
+ end
210
+
211
+ should "work with multiple keys" do
212
+ @user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
213
+ @user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
214
+ @user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
215
+ end
216
+
217
+ should "raise error when using ! and not found" do
218
+ assert_raises(MongoMapper::DocumentNotFound) do
219
+ @user.lists.find_by_name!('Foo 3')
220
+ end
221
+ end
222
+
223
+ context "find_or_create_by" do
224
+ should "not create document if found" do
225
+ lambda {
226
+ list = @user.lists.find_or_create_by_name('Foo 1')
227
+ list.should == @list1
228
+ }.should_not change { List.count }
229
+ end
230
+
231
+ should "create document if not found" do
232
+ lambda {
233
+ list = @user.lists.find_or_create_by_name('Home')
234
+ @user.lists.should include(list)
235
+ }.should change { List.count }
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ context "count" do
242
+ setup do
243
+ @user = User.create(:name => 'John')
244
+ @user2 = User.create(:name => 'Brandon')
245
+ @list1 = @user.lists.create!(:name => 'Foo 1')
246
+ @list2 = @user.lists.create!(:name => 'Foo 2')
247
+ @list3 = @user2.lists.create!(:name => 'Foo 3')
248
+ end
249
+
250
+ should "return number of ids" do
251
+ @user.lists.count.should == 2
252
+ @user2.lists.count.should == 1
253
+ end
254
+
255
+ should "return correct count when given criteria" do
256
+ @user.lists.count(:name => 'Foo 1').should == 1
257
+ @user2.lists.count(:name => 'Foo 1').should == 0
258
+ end
259
+ end
260
+
261
+ context "Removing documents" do
262
+ setup do
263
+ @user = User.create(:name => 'John')
264
+ @user2 = User.create(:name => 'Brandon')
265
+ @list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
266
+ @list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
267
+ @list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
268
+ end
269
+
270
+ context "destroy_all" do
271
+ should "work" do
272
+ @user.lists.count.should == 2
273
+ @user.lists.destroy_all
274
+ @user.lists.count.should == 0
275
+ end
276
+
277
+ should "work with conditions" do
278
+ @user.lists.count.should == 2
279
+ @user.lists.destroy_all(:name => 'Foo 1')
280
+ @user.lists.count.should == 1
281
+ end
282
+ end
283
+
284
+ context "delete_all" do
285
+ should "work" do
286
+ @user.lists.count.should == 2
287
+ @user.lists.delete_all
288
+ @user.lists.count.should == 0
289
+ end
290
+
291
+ should "work with conditions" do
292
+ @user.lists.count.should == 2
293
+ @user.lists.delete_all(:name => 'Foo 1')
294
+ @user.lists.count.should == 1
295
+ end
296
+ end
297
+
298
+ should "work with nullify" do
299
+ @user.lists.count.should == 2
300
+
301
+ lambda {
302
+ @user.lists.nullify
303
+ }.should_not change { List.count }
304
+
305
+ @user.lists.count.should == 0
306
+ end
307
+ end
308
+ end
309
+ end
@@ -22,10 +22,10 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
22
22
 
23
23
  should "be able to replace the association" do
24
24
  project = Project.new
25
- project.statuses = [Status.new("name" => "ready")]
25
+ project.statuses = [Status.new(:name => "ready")]
26
26
  project.save.should be_true
27
27
 
28
- project = project.reload
28
+ project.reload
29
29
  project.statuses.size.should == 1
30
30
  project.statuses[0].name.should == "ready"
31
31
  end
@@ -36,7 +36,7 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
36
36
  project.statuses.push Status.new(:name => 'push')
37
37
  project.statuses.concat Status.new(:name => 'concat')
38
38
 
39
- project = project.reload
39
+ project.reload
40
40
  project.statuses[0].project_id.should == project.id
41
41
  project.statuses[1].project_id.should == project.id
42
42
  project.statuses[2].project_id.should == project.id
@@ -235,84 +235,44 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
235
235
  lambda {
236
236
  status = @project1.statuses.find_or_create_by_name('Delivered')
237
237
  status.project.should == @project1
238
- }.should change { Status.count }.by(1)
238
+ }.should change { Status.count }
239
239
  end
240
240
  end
241
241
  end
242
242
 
243
- context "with :all" do
243
+ context "all" do
244
244
  should "work" do
245
245
  @project1.statuses.find(:all, :order => "position asc").should == [@brand_new, @complete]
246
- end
247
-
248
- should "work with conditions" do
249
- statuses = @project1.statuses.find(:all, :name => 'Complete')
250
- statuses.should == [@complete]
251
- end
252
-
253
- should "work with order" do
254
- statuses = @project1.statuses.find(:all, :order => 'name asc')
255
- statuses.should == [@complete, @brand_new]
256
- end
257
- end
258
-
259
- context "with #all" do
260
- should "work" do
261
246
  @project1.statuses.all(:order => "position asc").should == [@brand_new, @complete]
262
247
  end
263
248
 
264
249
  should "work with conditions" do
265
- statuses = @project1.statuses.all(:name => 'Complete')
266
- statuses.should == [@complete]
267
- end
268
-
269
- should "work with order" do
270
- statuses = @project1.statuses.all(:order => 'name asc')
271
- statuses.should == [@complete, @brand_new]
250
+ @project1.statuses.find(:all, :name => 'Complete').should == [@complete]
251
+ @project1.statuses.all(:name => 'Complete').should == [@complete]
272
252
  end
273
253
  end
274
254
 
275
- context "with :first" do
255
+ context "first" do
276
256
  should "work" do
277
257
  @project1.statuses.find(:first, :order => 'name').should == @complete
278
- end
279
-
280
- should "work with conditions" do
281
- status = @project1.statuses.find(:first, :name => 'Complete')
282
- status.should == @complete
283
- end
284
- end
285
-
286
- context "with #first" do
287
- should "work" do
288
258
  @project1.statuses.first(:order => 'name').should == @complete
289
259
  end
290
260
 
291
261
  should "work with conditions" do
292
- status = @project1.statuses.first(:name => 'Complete')
293
- status.should == @complete
262
+ @project1.statuses.find(:first, :name => 'Complete').should == @complete
263
+ @project1.statuses.first(:name => 'Complete').should == @complete
294
264
  end
295
265
  end
296
266
 
297
- context "with :last" do
267
+ context "last" do
298
268
  should "work" do
299
269
  @project1.statuses.find(:last, :order => "position asc").should == @complete
300
- end
301
-
302
- should "work with conditions" do
303
- status = @project1.statuses.find(:last, :order => 'position', :name => 'New')
304
- status.should == @brand_new
305
- end
306
- end
307
-
308
- context "with #last" do
309
- should "work" do
310
270
  @project1.statuses.last(:order => "position asc").should == @complete
311
271
  end
312
272
 
313
273
  should "work with conditions" do
314
- status = @project1.statuses.last(:order => 'position', :name => 'New')
315
- status.should == @brand_new
274
+ @project1.statuses.find(:last, :order => 'position', :name => 'New').should == @brand_new
275
+ @project1.statuses.last(:order => 'position', :name => 'New').should == @brand_new
316
276
  end
317
277
  end
318
278
 
@@ -326,10 +326,11 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
326
326
  end
327
327
 
328
328
  should "work using many's :extend option" do
329
+
329
330
  room = Room.new(:name => "Amazing Room")
330
331
  accounts = room.accounts = [
331
332
  Bot.new(:last_logged_in => 3.weeks.ago),
332
- User.new(:last_logged_in => nil),
333
+ AccountUser.new(:last_logged_in => nil),
333
334
  Bot.new(:last_logged_in => 1.week.ago)
334
335
  ]
335
336
  room.save
@@ -100,7 +100,7 @@ class Account
100
100
 
101
101
  belongs_to :room
102
102
  end
103
- class User < Account; end
103
+ class AccountUser < Account; end
104
104
  class Bot < Account; end
105
105
 
106
106
  class Answer
@@ -64,23 +64,6 @@ class AssociationBaseTest < Test::Unit::TestCase
64
64
  end
65
65
  end
66
66
 
67
- context "finder_options" do
68
- should "default to empty hash" do
69
- base = Base.new(:many, :foos)
70
- base.finder_options.should == {}
71
- end
72
-
73
- should "work with order" do
74
- base = Base.new(:many, :foos, :order => 'position')
75
- base.finder_options.should == {:order => 'position'}
76
- end
77
-
78
- should "correctly parse from options" do
79
- base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
80
- base.finder_options.should == {:order => 'position', :somekey => 'somevalue'}
81
- end
82
- end
83
-
84
67
  context "belongs_to?" do
85
68
  should "be true if belongs_to" do
86
69
  Base.new(:belongs_to, :foo).belongs_to?.should be_true
@@ -101,6 +84,43 @@ class AssociationBaseTest < Test::Unit::TestCase
101
84
  end
102
85
  end
103
86
 
87
+ context "as?" do
88
+ should "be true if one" do
89
+ Base.new(:one, :foo, :as => :commentable).as?.should be_true
90
+ end
91
+
92
+ should "be false if not one" do
93
+ Base.new(:many, :foo).as?.should be_false
94
+ end
95
+ end
96
+
97
+ context "in_array?" do
98
+ should "be true if one" do
99
+ Base.new(:one, :foo, :in => :list_ids).in_array?.should be_true
100
+ end
101
+
102
+ should "be false if not one" do
103
+ Base.new(:many, :foo).in_array?.should be_false
104
+ end
105
+ end
106
+
107
+ context "finder_options" do
108
+ should "default to empty hash" do
109
+ base = Base.new(:many, :foos)
110
+ base.finder_options.should == {}
111
+ end
112
+
113
+ should "work with order" do
114
+ base = Base.new(:many, :foos, :order => 'position')
115
+ base.finder_options.should == {:order => 'position'}
116
+ end
117
+
118
+ should "correctly parse from options" do
119
+ base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
120
+ base.finder_options.should == {:order => 'position', :somekey => 'somevalue'}
121
+ end
122
+ end
123
+
104
124
  context "type_key_name" do
105
125
  should "be _type for many" do
106
126
  Base.new(:many, :foos).type_key_name.should == '_type'
@@ -177,6 +197,11 @@ class AssociationBaseTest < Test::Unit::TestCase
177
197
  base = Base.new(:one, :target, :polymorphic => true)
178
198
  base.proxy_class.should == OneProxy
179
199
  end
200
+
201
+ should "be InArrayProxy for many with :in option" do
202
+ base = Base.new(:many, :messages, :in => :message_ids)
203
+ base.proxy_class.should == InArrayProxy
204
+ end
180
205
  end
181
206
 
182
207
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mrkurt-mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.8
4
+ version: 0.6.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-01 00:00:00 -06:00
13
+ date: 2010-01-02 00:00:00 -06:00
14
14
  default_executable: mmconsole
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -105,6 +105,7 @@ files:
105
105
  - lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb
106
106
  - lib/mongo_mapper/associations/belongs_to_proxy.rb
107
107
  - lib/mongo_mapper/associations/collection.rb
108
+ - lib/mongo_mapper/associations/in_array_proxy.rb
108
109
  - lib/mongo_mapper/associations/many_documents_as_proxy.rb
109
110
  - lib/mongo_mapper/associations/many_documents_proxy.rb
110
111
  - lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb
@@ -128,10 +129,12 @@ files:
128
129
  - lib/mongo_mapper/support.rb
129
130
  - lib/mongo_mapper/validations.rb
130
131
  - mongo_mapper.gemspec
132
+ - mrkurt-mongo_mapper.gemspec
131
133
  - specs.watchr
132
134
  - test/NOTE_ON_TESTING
133
135
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
134
136
  - test/functional/associations/test_belongs_to_proxy.rb
137
+ - test/functional/associations/test_in_array_proxy.rb
135
138
  - test/functional/associations/test_many_documents_as_proxy.rb
136
139
  - test/functional/associations/test_many_documents_proxy.rb
137
140
  - test/functional/associations/test_many_embedded_polymorphic_proxy.rb
@@ -215,6 +218,7 @@ test_files:
215
218
  - test/functional/associations/test_many_documents_as_proxy.rb
216
219
  - test/functional/associations/test_many_documents_proxy.rb
217
220
  - test/functional/associations/test_one_proxy.rb
221
+ - test/functional/associations/test_in_array_proxy.rb
218
222
  - test/functional/associations/test_many_embedded_polymorphic_proxy.rb
219
223
  - test/functional/associations/test_belongs_to_proxy.rb
220
224
  - test/functional/test_modifiers.rb