mongoid 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,7 @@
11
11
  <a href="http://www.pivotaltracker.com/projects/27482">Mongoid on Pivotal Tracker</a>
12
12
  <a href="http://groups.google.com/group/mongoid">Mongoid Google Group</a>
13
13
  <a href="http://mongoid.org:4444/">Mongoid on CI Joe</a>
14
+ <a href="http://mongoid.org">Mongoid Website and Documentation</a>
14
15
 
15
16
  <h3>Compatibility</h3>
16
17
 
@@ -26,173 +27,9 @@
26
27
  release notes and documentation with each release.
27
28
  </p>
28
29
 
29
- <h2>Mongoid in Action</h2>
30
+ <h2>Documentation</h2>
30
31
 
31
- Initialize Mongoid:
32
-
33
- <pre>
34
- connection = Mongo::Connection.new
35
- Mongoid.database = connection.db("my_db_name")
36
- </pre>
37
-
38
- Example of a simple domain model:
39
-
40
- <pre>
41
- class Person < Mongoid::Document
42
- include Mongoid::Versioning
43
-
44
- field :title
45
- field :age, :type => Integer, :default => 0
46
-
47
- has_one :name
48
- has_many :addresses
49
-
50
- has_one_related :game
51
- has_many_related :posts
52
- end
53
-
54
- class Address < Mongoid::Document
55
- field :street
56
- field :city
57
- field :state
58
- field :post_code
59
- belongs_to :person, :inverse_of => :addresses
60
- end
61
-
62
- class Name < Mongoid::Document
63
- include Mongoid::Timestamps
64
- field :first_name
65
- field :last_name
66
- belongs_to :person, :inverse_of => :name
67
- end
68
-
69
- class Post < Mongoid::Document
70
- include Mongoid::Timestamps
71
- field :title
72
- field :text
73
- field :tags, :type => Array
74
- belongs_to_related :person
75
- end
76
-
77
- class Game < ActiveRecord::Base
78
- belongs_to_related :person
79
- end
80
- </pre>
81
-
82
- Mongoid supports all the basic ActiveRecord creation and persistence methods.
83
-
84
- Creation:
85
-
86
- <pre>
87
- Person.create(:title => "Esquire")
88
- Person.create!(:title => "Sir", :name => { :first_name => "Elton", :last_name => "John" })
89
- </pre>
90
-
91
- Save:
92
-
93
- <pre>
94
- person.save
95
- person.save!
96
- </pre>
97
-
98
- Updating:
99
-
100
- <pre>
101
- person.update_attributes(:title => "Sir")
102
- person.update_attributes!(:name => { :first_name => "Emmanuel", :last_name => "Zorg" })
103
- </pre>
104
-
105
- Deleting:
106
-
107
- <pre>
108
- person.destroy
109
- Person.destroy_all(:title => "Sir")
110
- person.delete
111
- Person.delete_all(:title => "Sir")
112
- </pre>
113
-
114
- Count:
115
-
116
- <pre>
117
- Person.count
118
- </pre>
119
-
120
- Retrieval of Documents from the database can be done in the traditional ActiveRecord
121
- manner or done using the Mongoid criteria DSL.
122
-
123
- Old School:
124
-
125
- <pre>
126
- Person.find(:all, :conditions => { :title => "Esquire" })
127
- Person.find(:first, :conditions => { :title => "Esquire" })
128
- Person.first(:conditions => { :title => "Sir" })
129
- Person.all(:conditions => { :title => "Sir" })
130
- </pre>
131
-
132
- New School:
133
-
134
- <pre>
135
- Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).paginate
136
- Person.select(:title).aggregate
137
- Person.select(:first_name).order_by([[:first_name, :asc]])
138
- Criteria.translate(:conditions => { :title => "Sir" })
139
- </pre>
140
-
141
- Chaining Scopes:
142
-
143
- Class methods that return criteria, either through the Criteria API or the traditional Finder API, can
144
- be chained, similar to DataMapper.
145
-
146
- <pre>
147
- class Person < Mongoid::Document
148
- field :title
149
- field :age, :type => Integer
150
-
151
- class << self
152
- def knighted
153
- criteria.where(:title => "Sir")
154
- end
155
- def old
156
- criteria.where(:age => { "$gt" > 70 })
157
- end
158
- end
159
- end
160
-
161
- Person.old.knighted
162
- Person.old.paginate
163
- </pre>
164
-
165
- Paginate Document search results:
166
-
167
- <pre>
168
- Person.paginate(:conditions => {:title => "Esquire"}, :page => 1, :per_page => 20)
169
- </pre>
170
-
171
- Versioning:
172
-
173
- Versioning can be added by including Mongoid::Versioning in your document. This will give
174
- a version field, and will update the version number with each save. Additionally each
175
- version of the document will be stored in a versions array, embedded within the document.
176
-
177
- <pre>
178
- class Person < Mongoid::Document
179
- include Mongoid::Versioning
180
- field :title
181
- end
182
-
183
- @person = Person.new
184
- @person.save # Version 1
185
- @person.title = "Madam"
186
- @person.save # Version 2
187
- @person.versions.size = 1
188
- </pre>
189
-
190
- Validations:
191
-
192
- Mongoid supports all validations provided by Jay Fields' <tt>Validatable</tt> gem.
193
- For more information please see the <tt>Validatable</tt> documentation.
194
-
195
- <a href="http://validatable.rubyforge.org/">Validatable on RubyForge</a>
32
+ <p>Please see the new Mongoid website for up-to-date documentation: <a href="http://mongoid.org">mongoid.org</a></p>
196
33
 
197
34
  <h2>License</h2>
198
35
 
data/Rakefile CHANGED
@@ -12,11 +12,11 @@ begin
12
12
  gem.homepage = "http://github.com/durran/mongoid"
13
13
  gem.authors = ["Durran Jordan"]
14
14
 
15
- gem.add_dependency("durran-validatable", ">= 1.8.3")
16
- gem.add_dependency("leshill-will_paginate", ">= 2.3.11")
17
15
  gem.add_dependency("activesupport", ">= 2.3.4")
18
16
  gem.add_dependency("mongo", ">= 0.18.1")
19
17
  gem.add_dependency("mongo_ext", ">= 0.18.1")
18
+ gem.add_dependency("durran-validatable", ">= 1.8.3")
19
+ gem.add_dependency("leshill-will_paginate", ">= 2.3.11")
20
20
 
21
21
  gem.add_development_dependency("rspec", ">= 1.2.9")
22
22
  gem.add_development_dependency("mocha", ">= 0.9.8")
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.10
1
+ 0.9.11
@@ -21,11 +21,11 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  require "rubygems"
23
23
 
24
- gem "activesupport", "2.3.4"
25
- gem "mongo", "0.18.1"
26
- gem "mongo_ext", "0.18.1"
27
- gem "durran-validatable", "1.8.3"
28
- gem "leshill-will_paginate", "2.3.11"
24
+ gem "activesupport", ">= 2.3.4"
25
+ gem "mongo", ">= 0.18.1"
26
+ gem "mongo_ext", ">= 0.18.1"
27
+ gem "durran-validatable", ">= 1.8.3"
28
+ gem "leshill-will_paginate", ">= 2.3.11"
29
29
 
30
30
  require "delegate"
31
31
  require "observer"
@@ -86,6 +86,18 @@ module Mongoid #:nodoc:
86
86
  process(attrs)
87
87
  notify
88
88
  end
89
+
90
+ protected
91
+ # Used when supplying a :reject_if block as an option to
92
+ # accepts_nested_attributes_for
93
+ def reject(attributes, options)
94
+ rejector = options[:reject_if]
95
+ if rejector
96
+ attributes.delete_if do |key, value|
97
+ rejector.call(value)
98
+ end
99
+ end
100
+ end
89
101
  end
90
102
 
91
103
  module ClassMethods
@@ -101,8 +113,11 @@ module Mongoid #:nodoc:
101
113
  # accepts_nested_attributes_for :name, :addresses
102
114
  # end
103
115
  def accepts_nested_attributes_for(*args)
104
- args.flatten.each do |name|
116
+ associations = args.flatten
117
+ options = associations.last.is_a?(Hash) ? associations.pop : {}
118
+ associations.each do |name|
105
119
  define_method("#{name}_attributes=") do |attrs|
120
+ reject(attrs, options)
106
121
  association = send(name)
107
122
  update(association, true)
108
123
  association.nested_build(attrs)
@@ -5,7 +5,6 @@ require "mongoid/commands/delete_all"
5
5
  require "mongoid/commands/destroy"
6
6
  require "mongoid/commands/destroy_all"
7
7
  require "mongoid/commands/save"
8
- require "mongoid/commands/validate"
9
8
 
10
9
  module Mongoid #:nodoc:
11
10
 
@@ -11,7 +11,7 @@ module Mongoid #:nodoc:
11
11
  #
12
12
  # Returns: +Document+ if validation passes, +false+ if not.
13
13
  def self.execute(doc)
14
- return false unless Validate.execute(doc)
14
+ return false unless doc.valid?
15
15
  doc.run_callbacks :before_save
16
16
  parent = doc.parent
17
17
  doc.new_record = false
@@ -49,8 +49,8 @@ module Mongoid #:nodoc:
49
49
  #
50
50
  # <tt>field :score, :default => 0</tt>
51
51
  def field(name, options = {})
52
- define(name, options)
53
- default(name, options)
52
+ set_field(name, options)
53
+ set_default(name, options)
54
54
  end
55
55
 
56
56
  # Returns all the fields for the Document as a +Hash+ with names as keys.
@@ -99,17 +99,22 @@ module Mongoid #:nodoc:
99
99
  protected
100
100
 
101
101
  # Define a field attribute for the +Document+.
102
- def define(name, options = {})
102
+ def set_field(name, options = {})
103
103
  meth = options.delete(:as) || name
104
104
  @fields ||= {}.with_indifferent_access
105
105
  @fields[name] = Field.new(name.to_s, options)
106
+ create_accessors(name, meth, options)
107
+ end
108
+
109
+ # Create the field accessors.
110
+ def create_accessors(name, meth, options = {})
106
111
  define_method(meth) { read_attribute(name) }
107
112
  define_method("#{meth}=") { |value| write_attribute(name, value) }
108
113
  define_method("#{meth}?") { read_attribute(name) == true } if options[:type] == Boolean
109
114
  end
110
115
 
111
116
  # Set up a default value for a field.
112
- def default(name, options = {})
117
+ def set_default(name, options = {})
113
118
  value = options[:default]
114
119
  @defaults ||= {}.with_indifferent_access
115
120
  @defaults[name] = value if value
@@ -269,6 +274,15 @@ module Mongoid #:nodoc:
269
274
  notify
270
275
  end
271
276
 
277
+ # Needs to run the appropriate callbacks the delegate up to the validatable
278
+ # gem.
279
+ def valid?
280
+ run_callbacks(:before_validation)
281
+ result = super
282
+ run_callbacks(:after_validation)
283
+ result
284
+ end
285
+
272
286
  protected
273
287
  def generate_key
274
288
  if primary_key
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.9.10"
8
+ s.version = "0.9.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2009-12-19}
12
+ s.date = %q{2009-12-20}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -39,7 +39,6 @@ Gem::Specification.new do |s|
39
39
  "lib/mongoid/commands/destroy.rb",
40
40
  "lib/mongoid/commands/destroy_all.rb",
41
41
  "lib/mongoid/commands/save.rb",
42
- "lib/mongoid/commands/validate.rb",
43
42
  "lib/mongoid/criteria.rb",
44
43
  "lib/mongoid/document.rb",
45
44
  "lib/mongoid/dynamic_finder.rb",
@@ -89,7 +88,6 @@ Gem::Specification.new do |s|
89
88
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
90
89
  "spec/unit/mongoid/commands/destroy_spec.rb",
91
90
  "spec/unit/mongoid/commands/save_spec.rb",
92
- "spec/unit/mongoid/commands/validate_spec.rb",
93
91
  "spec/unit/mongoid/commands_spec.rb",
94
92
  "spec/unit/mongoid/criteria_spec.rb",
95
93
  "spec/unit/mongoid/document_spec.rb",
@@ -143,7 +141,6 @@ Gem::Specification.new do |s|
143
141
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
144
142
  "spec/unit/mongoid/commands/destroy_spec.rb",
145
143
  "spec/unit/mongoid/commands/save_spec.rb",
146
- "spec/unit/mongoid/commands/validate_spec.rb",
147
144
  "spec/unit/mongoid/commands_spec.rb",
148
145
  "spec/unit/mongoid/criteria_spec.rb",
149
146
  "spec/unit/mongoid/document_spec.rb",
@@ -178,28 +175,28 @@ Gem::Specification.new do |s|
178
175
  s.specification_version = 3
179
176
 
180
177
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
181
- s.add_runtime_dependency(%q<durran-validatable>, [">= 1.8.3"])
182
- s.add_runtime_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
183
178
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
184
179
  s.add_runtime_dependency(%q<mongo>, [">= 0.18.1"])
185
180
  s.add_runtime_dependency(%q<mongo_ext>, [">= 0.18.1"])
181
+ s.add_runtime_dependency(%q<durran-validatable>, [">= 1.8.3"])
182
+ s.add_runtime_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
186
183
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
187
184
  s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
188
185
  else
189
- s.add_dependency(%q<durran-validatable>, [">= 1.8.3"])
190
- s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
191
186
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
192
187
  s.add_dependency(%q<mongo>, [">= 0.18.1"])
193
188
  s.add_dependency(%q<mongo_ext>, [">= 0.18.1"])
189
+ s.add_dependency(%q<durran-validatable>, [">= 1.8.3"])
190
+ s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
194
191
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
195
192
  s.add_dependency(%q<mocha>, [">= 0.9.8"])
196
193
  end
197
194
  else
198
- s.add_dependency(%q<durran-validatable>, [">= 1.8.3"])
199
- s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
200
195
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
201
196
  s.add_dependency(%q<mongo>, [">= 0.18.1"])
202
197
  s.add_dependency(%q<mongo_ext>, [">= 0.18.1"])
198
+ s.add_dependency(%q<durran-validatable>, [">= 1.8.3"])
199
+ s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
203
200
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
204
201
  s.add_dependency(%q<mocha>, [">= 0.9.8"])
205
202
  end
@@ -37,7 +37,8 @@ class Person < Mongoid::Document
37
37
  has_one :name
38
38
  has_one :pet, :class_name => "Animal"
39
39
 
40
- accepts_nested_attributes_for :addresses, :name
40
+ accepts_nested_attributes_for :addresses, :reject_if => lambda { |attrs| attrs["street"].blank? }
41
+ accepts_nested_attributes_for :name
41
42
 
42
43
  index :title
43
44
  index :dob
@@ -131,12 +132,15 @@ end
131
132
 
132
133
  class Comment < Mongoid::Document
133
134
  include Mongoid::Versioning
135
+ include Mongoid::Timestamps
134
136
  field :text
135
137
  key :text
138
+ validates_presence_of :text
136
139
  end
137
140
 
138
141
  class Post < Mongoid::Document
139
142
  include Mongoid::Versioning
143
+ include Mongoid::Timestamps
140
144
  field :title
141
145
  belongs_to_related :person
142
146
  end
@@ -16,6 +16,21 @@ describe Mongoid::Attributes do
16
16
 
17
17
  context "on a has many association" do
18
18
 
19
+ context "when a reject block supplied" do
20
+
21
+ before do
22
+ @attributes = {
23
+ "0" => { "city" => "San Francisco" }
24
+ }
25
+ @person.addresses_attributes = @attributes
26
+ end
27
+
28
+ it "removes the attributes that match" do
29
+ @person.addresses.should be_empty
30
+ end
31
+
32
+ end
33
+
19
34
  context "when association is empty" do
20
35
 
21
36
  before do
@@ -177,4 +177,23 @@ describe Mongoid::Commands do
177
177
 
178
178
  end
179
179
 
180
+ describe "#valid?" do
181
+
182
+ before do
183
+ @comment = Comment.new
184
+ end
185
+
186
+ it "validates the document" do
187
+ @comment.valid?.should be_false
188
+ end
189
+
190
+ it "runs the validation callbacks" do
191
+ @comment.expects(:run_callbacks).with(:validate)
192
+ @comment.expects(:run_callbacks).with(:before_validation)
193
+ @comment.expects(:run_callbacks).with(:after_validation)
194
+ @comment.valid?
195
+ end
196
+
197
+ end
198
+
180
199
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.9.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,58 +9,58 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-19 00:00:00 -05:00
12
+ date: 2009-12-20 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: durran-validatable
16
+ name: activesupport
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.8.3
23
+ version: 2.3.4
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
- name: leshill-will_paginate
26
+ name: mongo
27
27
  type: :runtime
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.3.11
33
+ version: 0.18.1
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
- name: activesupport
36
+ name: mongo_ext
37
37
  type: :runtime
38
38
  version_requirement:
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 2.3.4
43
+ version: 0.18.1
44
44
  version:
45
45
  - !ruby/object:Gem::Dependency
46
- name: mongo
46
+ name: durran-validatable
47
47
  type: :runtime
48
48
  version_requirement:
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 0.18.1
53
+ version: 1.8.3
54
54
  version:
55
55
  - !ruby/object:Gem::Dependency
56
- name: mongo_ext
56
+ name: leshill-will_paginate
57
57
  type: :runtime
58
58
  version_requirement:
59
59
  version_requirements: !ruby/object:Gem::Requirement
60
60
  requirements:
61
61
  - - ">="
62
62
  - !ruby/object:Gem::Version
63
- version: 0.18.1
63
+ version: 2.3.11
64
64
  version:
65
65
  - !ruby/object:Gem::Dependency
66
66
  name: rspec
@@ -115,7 +115,6 @@ files:
115
115
  - lib/mongoid/commands/destroy.rb
116
116
  - lib/mongoid/commands/destroy_all.rb
117
117
  - lib/mongoid/commands/save.rb
118
- - lib/mongoid/commands/validate.rb
119
118
  - lib/mongoid/criteria.rb
120
119
  - lib/mongoid/document.rb
121
120
  - lib/mongoid/dynamic_finder.rb
@@ -165,7 +164,6 @@ files:
165
164
  - spec/unit/mongoid/commands/destroy_all_spec.rb
166
165
  - spec/unit/mongoid/commands/destroy_spec.rb
167
166
  - spec/unit/mongoid/commands/save_spec.rb
168
- - spec/unit/mongoid/commands/validate_spec.rb
169
167
  - spec/unit/mongoid/commands_spec.rb
170
168
  - spec/unit/mongoid/criteria_spec.rb
171
169
  - spec/unit/mongoid/document_spec.rb
@@ -241,7 +239,6 @@ test_files:
241
239
  - spec/unit/mongoid/commands/destroy_all_spec.rb
242
240
  - spec/unit/mongoid/commands/destroy_spec.rb
243
241
  - spec/unit/mongoid/commands/save_spec.rb
244
- - spec/unit/mongoid/commands/validate_spec.rb
245
242
  - spec/unit/mongoid/commands_spec.rb
246
243
  - spec/unit/mongoid/criteria_spec.rb
247
244
  - spec/unit/mongoid/document_spec.rb
@@ -1,21 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Commands
4
- class Validate
5
- # Performs validation of the supplied +Document+, handling all associated
6
- # callbacks.
7
- #
8
- # Options:
9
- #
10
- # doc: A +Document+ that is going to be persisted.
11
- #
12
- # Returns: +true+ if validation passes, +false+ if not.
13
- def self.execute(doc)
14
- doc.run_callbacks(:before_validation)
15
- validated = doc.valid?
16
- doc.run_callbacks(:after_validation)
17
- return validated
18
- end
19
- end
20
- end
21
- end
@@ -1,25 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Commands::Validate do
4
-
5
- describe "#execute" do
6
-
7
- before do
8
- @document = stub(:run_callbacks)
9
- end
10
-
11
- it "validates the document" do
12
- @document.expects(:valid?).returns(true)
13
- Mongoid::Commands::Validate.execute(@document).should be_true
14
- end
15
-
16
- it "runs the before and after validate callbacks" do
17
- @document.expects(:valid?).returns(true)
18
- @document.expects(:run_callbacks).with(:before_validation)
19
- @document.expects(:run_callbacks).with(:after_validation)
20
- Mongoid::Commands::Validate.execute(@document)
21
- end
22
-
23
- end
24
-
25
- end