mongoid 2.0.0.beta.5 → 2.0.0.beta.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/lib/mongoid.rb +10 -2
  2. data/lib/mongoid/associations.rb +82 -58
  3. data/lib/mongoid/associations/embeds_one.rb +6 -6
  4. data/lib/mongoid/associations/foreign_key.rb +35 -0
  5. data/lib/mongoid/associations/meta_data.rb +9 -0
  6. data/lib/mongoid/associations/options.rb +1 -1
  7. data/lib/mongoid/associations/proxy.rb +9 -0
  8. data/lib/mongoid/associations/{belongs_to_related.rb → referenced_in.rb} +6 -5
  9. data/lib/mongoid/associations/{has_many_related.rb → references_many.rb} +69 -26
  10. data/lib/mongoid/associations/references_many_as_array.rb +78 -0
  11. data/lib/mongoid/associations/{has_one_related.rb → references_one.rb} +16 -2
  12. data/lib/mongoid/atomicity.rb +42 -0
  13. data/lib/mongoid/attributes.rb +148 -146
  14. data/lib/mongoid/callbacks.rb +5 -1
  15. data/lib/mongoid/collections.rb +31 -1
  16. data/lib/mongoid/components.rb +4 -1
  17. data/lib/mongoid/config.rb +2 -1
  18. data/lib/mongoid/criteria.rb +9 -0
  19. data/lib/mongoid/dirty.rb +211 -212
  20. data/lib/mongoid/document.rb +126 -185
  21. data/lib/mongoid/extensions.rb +5 -0
  22. data/lib/mongoid/extensions/array/conversions.rb +3 -5
  23. data/lib/mongoid/extensions/hash/conversions.rb +19 -22
  24. data/lib/mongoid/extensions/object/conversions.rb +3 -5
  25. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  26. data/lib/mongoid/field.rb +11 -0
  27. data/lib/mongoid/finders.rb +8 -0
  28. data/lib/mongoid/hierarchy.rb +76 -0
  29. data/lib/mongoid/identity.rb +37 -29
  30. data/lib/mongoid/paths.rb +46 -47
  31. data/lib/mongoid/persistence.rb +111 -113
  32. data/lib/mongoid/persistence/insert.rb +1 -1
  33. data/lib/mongoid/persistence/insert_embedded.rb +10 -5
  34. data/lib/mongoid/persistence/remove_all.rb +3 -2
  35. data/lib/mongoid/persistence/update.rb +8 -3
  36. data/lib/mongoid/railtie.rb +3 -0
  37. data/lib/mongoid/railties/database.rake +33 -18
  38. data/lib/mongoid/timestamps.rb +9 -12
  39. data/lib/mongoid/validations/uniqueness.rb +16 -4
  40. data/lib/mongoid/version.rb +1 -1
  41. data/lib/mongoid/versioning.rb +10 -11
  42. data/lib/rails/generators/mongoid/config/config_generator.rb +0 -16
  43. metadata +64 -24
@@ -22,7 +22,7 @@ module Mongoid #:nodoc:
22
22
  #
23
23
  # The +Document+, whether the insert succeeded or not.
24
24
  def persist
25
- return @document if @validate && !@document.valid?
25
+ return @document if @validate && @document.invalid?(:create)
26
26
  @document.run_callbacks(:create) do
27
27
  @document.run_callbacks(:save) do
28
28
  if insert
@@ -24,12 +24,17 @@ module Mongoid #:nodoc:
24
24
  #
25
25
  # The +Document+, whether the insert succeeded or not.
26
26
  def persist
27
+ return @document if @validate && @document.invalid?(:create)
27
28
  parent = @document._parent
28
- if parent.new_record?
29
- parent.insert
30
- else
31
- update = { @document._inserter => { @document._position => @document.raw_attributes } }
32
- @collection.update(parent._selector, update, @options.merge(:multi => false))
29
+ @document.run_callbacks(:create) do
30
+ @document.run_callbacks(:save) do
31
+ if parent.new_record?
32
+ parent.insert
33
+ else
34
+ update = { @document._inserter => { @document._position => @document.raw_attributes } }
35
+ @collection.update(parent._selector, update, @options.merge(:multi => false))
36
+ end
37
+ end
33
38
  end
34
39
  @document.new_record = false; @document
35
40
  end
@@ -28,8 +28,9 @@ module Mongoid #:nodoc:
28
28
  protected
29
29
  # Remove the document from the database.
30
30
  def remove
31
- count = @collection.find(@selector.merge(:_type => @klass.name)).count
32
- @collection.remove(@selector, @options)
31
+ selector = (@klass.hereditary ? @selector.merge(:_type => @klass.name) : @selector)
32
+ count = @collection.find(selector).count
33
+ @collection.remove(selector, @options)
33
34
  count
34
35
  end
35
36
  end
@@ -39,11 +39,15 @@ module Mongoid #:nodoc:
39
39
  #
40
40
  # +true+ or +false+, depending on validation.
41
41
  def persist
42
- return false if validate && !@document.valid?
42
+ return false if validate && @document.invalid?(:update)
43
43
  @document.run_callbacks(:save) do
44
44
  @document.run_callbacks(:update) do
45
45
  if update
46
46
  @document.move_changes
47
+ @document._children.each do |child|
48
+ child.move_changes
49
+ child.new_record = false if child.new_record?
50
+ end
47
51
  else
48
52
  return false
49
53
  end
@@ -54,8 +58,9 @@ module Mongoid #:nodoc:
54
58
  protected
55
59
  # Update the document in the database atomically.
56
60
  def update
57
- if @document.changed?
58
- @collection.update(@document._selector, { "$set" => @document.setters }, @options.merge(:multi => false))
61
+ updates = @document._updates
62
+ unless updates.empty?
63
+ @collection.update(@document._selector, updates, @options.merge(:multi => false))
59
64
  end; true
60
65
  end
61
66
  end
@@ -1,3 +1,4 @@
1
+ require "rails"
1
2
  module Rails #:nodoc:
2
3
  module Mongoid #:nodoc:
3
4
  class Railtie < Rails::Railtie #:nodoc:
@@ -5,6 +6,8 @@ module Rails #:nodoc:
5
6
  # do we want a custom log subscriber for mongoid?
6
7
  # log_subscriber :mongoid, ::Mongoid::Railties::LogSubscriber.new
7
8
 
9
+ config.generators.orm :mongoid, :migration => false
10
+
8
11
  rake_tasks do
9
12
  load "mongoid/railties/database.rake"
10
13
  end
@@ -1,36 +1,51 @@
1
1
  namespace :db do
2
2
 
3
- desc 'Drops all the collections for the database for the current Rails.env'
4
- task :drop => :environment do
5
- Mongoid.master.collections.each{|col| col.drop unless col.name == 'system.users' }
3
+ if not Rake::Task.task_defined?("db:drop")
4
+ desc 'Drops all the collections for the database for the current Rails.env'
5
+ task :drop => :environment do
6
+ Mongoid.master.collections.each{|col| col.drop unless col.name == 'system.users' || col.name == "system.indexes" }
7
+ end
6
8
  end
7
9
 
8
- desc 'Load the seed data from db/seeds.rb'
9
- task :seed => :environment do
10
- seed_file = File.join(Rails.root, 'db', 'seeds.rb')
11
- load(seed_file) if File.exist?(seed_file)
10
+ if not Rake::Task.task_defined?("db:seed")
11
+ # if another ORM has defined db:seed, don't run it twice.
12
+ desc 'Load the seed data from db/seeds.rb'
13
+ task :seed => :environment do
14
+ seed_file = File.join(Rails.root, 'db', 'seeds.rb')
15
+ load(seed_file) if File.exist?(seed_file)
16
+ end
12
17
  end
13
18
 
14
- desc 'Create the database, and initialize with the seed data'
15
- task :setup => [ 'db:create', 'db:seed' ]
16
-
17
- desc 'Delete data and seed'
18
- task :reseed => [ 'db:drop', 'db:seed' ]
19
+ if not Rake::Task.task_defined?("db:setup")
20
+ desc 'Create the database, and initialize with the seed data'
21
+ task :setup => [ 'db:create', 'db:seed' ]
22
+ end
19
23
 
20
- task :create => :environment do
21
- # noop
24
+ if not Rake::Task.task_defined?("db:reseed")
25
+ desc 'Delete data and seed'
26
+ task :reseed => [ 'db:drop', 'db:seed' ]
22
27
  end
23
28
 
24
- task :migrate => :environment do
25
- # noop
29
+ if not Rake::Task.task_defined?("db:create")
30
+ task :create => :environment do
31
+ # noop
32
+ end
26
33
  end
27
34
 
28
- namespace :schema do
29
- task :load do
35
+ if not Rake::Task.task_defined?("db:migrate")
36
+ task :migrate => :environment do
30
37
  # noop
31
38
  end
32
39
  end
33
40
 
41
+ if not Rake::Task.task_defined?("db:schema:load")
42
+ namespace :schema do
43
+ task :load do
44
+ # noop
45
+ end
46
+ end
47
+ end
48
+
34
49
  ########
35
50
  # TODO: lots more useful db tasks can be added here. stuff like copyDatabase, etc
36
51
  ########
@@ -9,19 +9,16 @@ module Mongoid #:nodoc:
9
9
  set_callback :save, :before, :set_updated_at
10
10
  end
11
11
 
12
- module InstanceMethods
13
-
14
- # Update the created_at field on the Document to the current time. This is
15
- # only called on create.
16
- def set_created_at
17
- self.created_at = Time.now.utc if !created_at
18
- end
12
+ # Update the created_at field on the Document to the current time. This is
13
+ # only called on create.
14
+ def set_created_at
15
+ self.created_at = Time.now.utc if !created_at
16
+ end
19
17
 
20
- # Update the updated_at field on the Document to the current time.
21
- # This is only called on create and on save.
22
- def set_updated_at
23
- self.updated_at = Time.now.utc
24
- end
18
+ # Update the updated_at field on the Document to the current time.
19
+ # This is only called on create and on save.
20
+ def set_updated_at
21
+ self.updated_at = Time.now.utc
25
22
  end
26
23
  end
27
24
  end
@@ -13,11 +13,23 @@ module Mongoid #:nodoc:
13
13
  # validates_uniqueness_of :title
14
14
  # end
15
15
  class UniquenessValidator < ActiveModel::EachValidator
16
+ # Unfortunately, we have to tie Uniqueness validators to a class.
17
+ def setup(klass)
18
+ @klass = klass
19
+ end
20
+
16
21
  def validate_each(document, attribute, value)
17
- conditions = {attribute => value}
18
- conditions[options[:scope]] = document.attributes[options[:scope]] if options.has_key? :scope
19
- return if document.class.where(conditions).empty?
20
- if document.new_record? || key_changed?(document)
22
+ criteria = @klass.where(attribute => value)
23
+
24
+ Array.wrap(options[:scope]).each do |item|
25
+ criteria = criteria.where(item => document.attributes[item])
26
+ end
27
+
28
+ unless document.new_record?
29
+ criteria = criteria.where(:_id => {'$ne' => document._id})
30
+ end
31
+
32
+ if criteria.exists?
21
33
  document.errors.add(attribute, :taken, :default => options[:message], :value => value)
22
34
  end
23
35
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
- VERSION = "2.0.0.beta.5"
3
+ VERSION = "2.0.0.beta.7"
4
4
  end
@@ -10,17 +10,16 @@ module Mongoid #:nodoc:
10
10
  embeds_many :versions, :class_name => self.name
11
11
  set_callback :save, :before, :revise
12
12
  end
13
- module InstanceMethods
14
- # Create a new version of the +Document+. This will load the previous
15
- # document from the database and set it as the next version before saving
16
- # the current document. It then increments the version number.
17
- def revise
18
- last_version = self.class.first(:conditions => { :_id => id, :version => version })
19
- if last_version
20
- self.versions << last_version.clone
21
- self.version = version + 1
22
- @modifications["versions"] = [ nil, @attributes["versions"] ] if @modifications
23
- end
13
+
14
+ # Create a new version of the +Document+. This will load the previous
15
+ # document from the database and set it as the next version before saving
16
+ # the current document. It then increments the version number.
17
+ def revise
18
+ last_version = self.class.first(:conditions => { :_id => id, :version => version })
19
+ if last_version
20
+ self.versions << last_version.clone
21
+ self.version = version + 1
22
+ @modifications["versions"] = [ nil, @attributes["versions"] ] if @modifications
24
23
  end
25
24
  end
26
25
  end
@@ -20,22 +20,6 @@ module Mongoid
20
20
  template 'mongoid.yml', File.join('config', "mongoid.yml")
21
21
  end
22
22
 
23
- def inject_mongoid_into_application
24
- config_application_path = File.join("config", "application.rb")
25
- config_contents = File.read(config_application_path)
26
-
27
- mongoid_require = "\n\nrequire 'mongoid/railtie'"
28
-
29
- # check to see if its already been included
30
- return if config_contents.include?(mongoid_require)
31
-
32
- if config_contents.include?("require 'rails/all'")
33
- inject_into_file config_application_path, mongoid_require, :after => "require 'rails/all'"
34
- elsif config_contents.include?("require \"action_controller/railtie\"")
35
- inject_into_file config_application_path, mongoid_require, :after => "require \"action_controller/railtie\""
36
- end
37
- end
38
-
39
23
  end
40
24
  end
41
25
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 62196461
4
5
  prerelease: true
5
6
  segments:
6
7
  - 2
7
8
  - 0
8
9
  - 0
9
10
  - beta
10
- - 5
11
- version: 2.0.0.beta.5
11
+ - 7
12
+ version: 2.0.0.beta.7
12
13
  platform: ruby
13
14
  authors:
14
15
  - Durran Jordan
@@ -16,94 +17,124 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2010-05-17 00:00:00 -04:00
20
+ date: 2010-07-26 00:00:00 -04:00
20
21
  default_executable:
21
22
  dependencies:
22
23
  - !ruby/object:Gem::Dependency
23
24
  name: activemodel
24
25
  prerelease: false
25
26
  requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
26
28
  requirements:
27
- - - "="
29
+ - - ~>
28
30
  - !ruby/object:Gem::Version
31
+ hash: 31098225
29
32
  segments:
30
33
  - 3
31
34
  - 0
32
35
  - 0
33
- - beta3
34
- version: 3.0.0.beta3
36
+ - beta
37
+ version: 3.0.0.beta
35
38
  type: :runtime
36
39
  version_requirements: *id001
37
40
  - !ruby/object:Gem::Dependency
38
- name: will_paginate
41
+ name: tzinfo
39
42
  prerelease: false
40
43
  requirement: &id002 !ruby/object:Gem::Requirement
44
+ none: false
41
45
  requirements:
42
- - - "="
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ hash: 63
49
+ segments:
50
+ - 0
51
+ - 3
52
+ - 22
53
+ version: 0.3.22
54
+ type: :runtime
55
+ version_requirements: *id002
56
+ - !ruby/object:Gem::Dependency
57
+ name: will_paginate
58
+ prerelease: false
59
+ requirement: &id003 !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ~>
43
63
  - !ruby/object:Gem::Version
64
+ hash: 961915916
44
65
  segments:
45
66
  - 3
46
67
  - 0
47
68
  - pre
48
69
  version: 3.0.pre
49
70
  type: :runtime
50
- version_requirements: *id002
71
+ version_requirements: *id003
51
72
  - !ruby/object:Gem::Dependency
52
73
  name: mongo
53
74
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
75
+ requirement: &id004 !ruby/object:Gem::Requirement
76
+ none: false
55
77
  requirements:
56
78
  - - ~>
57
79
  - !ruby/object:Gem::Version
80
+ hash: 21
58
81
  segments:
59
82
  - 1
60
83
  - 0
61
84
  - 1
62
85
  version: 1.0.1
63
86
  type: :runtime
64
- version_requirements: *id003
87
+ version_requirements: *id004
65
88
  - !ruby/object:Gem::Dependency
66
89
  name: bson
67
90
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
91
+ requirement: &id005 !ruby/object:Gem::Requirement
92
+ none: false
69
93
  requirements:
70
94
  - - ~>
71
95
  - !ruby/object:Gem::Version
96
+ hash: 21
72
97
  segments:
73
98
  - 1
74
99
  - 0
75
100
  - 1
76
101
  version: 1.0.1
77
102
  type: :runtime
78
- version_requirements: *id004
103
+ version_requirements: *id005
79
104
  - !ruby/object:Gem::Dependency
80
105
  name: rspec
81
106
  prerelease: false
82
- requirement: &id005 !ruby/object:Gem::Requirement
107
+ requirement: &id006 !ruby/object:Gem::Requirement
108
+ none: false
83
109
  requirements:
84
110
  - - "="
85
111
  - !ruby/object:Gem::Version
112
+ hash: 62196475
86
113
  segments:
87
- - 1
88
- - 3
114
+ - 2
115
+ - 0
89
116
  - 0
90
- version: 1.3.0
117
+ - beta
118
+ - 12
119
+ version: 2.0.0.beta.12
91
120
  type: :development
92
- version_requirements: *id005
121
+ version_requirements: *id006
93
122
  - !ruby/object:Gem::Dependency
94
123
  name: mocha
95
124
  prerelease: false
96
- requirement: &id006 !ruby/object:Gem::Requirement
125
+ requirement: &id007 !ruby/object:Gem::Requirement
126
+ none: false
97
127
  requirements:
98
128
  - - "="
99
129
  - !ruby/object:Gem::Version
130
+ hash: 43
100
131
  segments:
101
132
  - 0
102
133
  - 9
103
134
  - 8
104
135
  version: 0.9.8
105
136
  type: :development
106
- version_requirements: *id006
137
+ version_requirements: *id007
107
138
  description: Mongoid is an ODM (Object Document Mapper) Framework for MongoDB, written in Ruby.
108
139
  email:
109
140
  - durran@gmail.com
@@ -114,16 +145,19 @@ extensions: []
114
145
  extra_rdoc_files: []
115
146
 
116
147
  files:
117
- - lib/mongoid/associations/belongs_to_related.rb
118
148
  - lib/mongoid/associations/embedded_in.rb
119
149
  - lib/mongoid/associations/embeds_many.rb
120
150
  - lib/mongoid/associations/embeds_one.rb
121
- - lib/mongoid/associations/has_many_related.rb
122
- - lib/mongoid/associations/has_one_related.rb
151
+ - lib/mongoid/associations/foreign_key.rb
123
152
  - lib/mongoid/associations/meta_data.rb
124
153
  - lib/mongoid/associations/options.rb
125
154
  - lib/mongoid/associations/proxy.rb
155
+ - lib/mongoid/associations/referenced_in.rb
156
+ - lib/mongoid/associations/references_many.rb
157
+ - lib/mongoid/associations/references_many_as_array.rb
158
+ - lib/mongoid/associations/references_one.rb
126
159
  - lib/mongoid/associations.rb
160
+ - lib/mongoid/atomicity.rb
127
161
  - lib/mongoid/attributes.rb
128
162
  - lib/mongoid/callbacks.rb
129
163
  - lib/mongoid/collection.rb
@@ -170,6 +204,7 @@ files:
170
204
  - lib/mongoid/extensions/object/conversions.rb
171
205
  - lib/mongoid/extensions/objectid/conversions.rb
172
206
  - lib/mongoid/extensions/proc/scoping.rb
207
+ - lib/mongoid/extensions/set/conversions.rb
173
208
  - lib/mongoid/extensions/string/conversions.rb
174
209
  - lib/mongoid/extensions/string/inflections.rb
175
210
  - lib/mongoid/extensions/symbol/inflections.rb
@@ -180,6 +215,7 @@ files:
180
215
  - lib/mongoid/field.rb
181
216
  - lib/mongoid/fields.rb
182
217
  - lib/mongoid/finders.rb
218
+ - lib/mongoid/hierarchy.rb
183
219
  - lib/mongoid/identity.rb
184
220
  - lib/mongoid/indexes.rb
185
221
  - lib/mongoid/javascript/functions.yml
@@ -237,16 +273,20 @@ rdoc_options: []
237
273
  require_paths:
238
274
  - lib
239
275
  required_ruby_version: !ruby/object:Gem::Requirement
276
+ none: false
240
277
  requirements:
241
278
  - - ">="
242
279
  - !ruby/object:Gem::Version
280
+ hash: 3
243
281
  segments:
244
282
  - 0
245
283
  version: "0"
246
284
  required_rubygems_version: !ruby/object:Gem::Requirement
285
+ none: false
247
286
  requirements:
248
287
  - - ">="
249
288
  - !ruby/object:Gem::Version
289
+ hash: 23
250
290
  segments:
251
291
  - 1
252
292
  - 3
@@ -255,7 +295,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
255
295
  requirements: []
256
296
 
257
297
  rubyforge_project: mongoid
258
- rubygems_version: 1.3.6
298
+ rubygems_version: 1.3.7
259
299
  signing_key:
260
300
  specification_version: 3
261
301
  summary: Elegent Persistance in Ruby for MongoDB.