mongoid 0.6.5 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.5
1
+ 0.6.6
@@ -4,6 +4,7 @@ require "mongoid/commands/delete_all"
4
4
  require "mongoid/commands/destroy"
5
5
  require "mongoid/commands/destroy_all"
6
6
  require "mongoid/commands/save"
7
+ require "mongoid/commands/validate"
7
8
 
8
9
  module Mongoid #:nodoc:
9
10
 
@@ -52,7 +53,7 @@ module Mongoid #:nodoc:
52
53
  # Save the +Document+. Delegates to the Save command. If the command
53
54
  # returns false then a +ValidationError+ will be raised.
54
55
  def save!
55
- Save.execute(self) || (raise ValidationsError)
56
+ Save.execute(self) || (raise ValidationsError.new(self.errors.full_messages))
56
57
  end
57
58
 
58
59
  # Update the attributes of the +Document+. Will call save after the
@@ -83,7 +84,7 @@ module Mongoid #:nodoc:
83
84
  # validation.
84
85
  def create!(attributes = {})
85
86
  document = Create.execute(new(attributes))
86
- raise ValidationsError unless document.errors.empty?
87
+ raise ValidationsError.new(self.errors.full_messages) unless document.errors.empty?
87
88
  return document
88
89
  end
89
90
 
@@ -10,7 +10,7 @@ module Mongoid #:nodoc:
10
10
  #
11
11
  # Returns: +Document+ if validation passes, +false+ if not.
12
12
  def self.execute(doc)
13
- return false unless doc.valid?
13
+ return false unless Validate.execute(doc)
14
14
  doc.run_callbacks :before_save
15
15
  parent = doc.parent
16
16
  parent ? Save.execute(parent) : doc.collection.save(doc.attributes)
@@ -0,0 +1,20 @@
1
+ module Mongoid #:nodoc:
2
+ module Commands
3
+ class Validate
4
+ # Performs validation of the supplied +Document+, handling all associated
5
+ # callbacks.
6
+ #
7
+ # Options:
8
+ #
9
+ # doc: A +Document+ that is going to be persisted.
10
+ #
11
+ # Returns: +true+ if validation passes, +false+ if not.
12
+ def self.execute(doc)
13
+ doc.run_callbacks(:before_validation)
14
+ validated = doc.valid?
15
+ doc.run_callbacks(:after_validation)
16
+ return validated
17
+ end
18
+ end
19
+ end
20
+ end
@@ -11,10 +11,12 @@ module Mongoid #:nodoc:
11
11
  :after_destroy,
12
12
  :after_save,
13
13
  :after_update,
14
+ :after_validation,
14
15
  :before_create,
15
16
  :before_destroy,
16
17
  :before_save,
17
- :before_update
18
+ :before_update,
19
+ :before_validation
18
20
 
19
21
  class << self
20
22
 
@@ -89,6 +91,11 @@ module Mongoid #:nodoc:
89
91
  Criteria.translate(*args).execute(self)
90
92
  end
91
93
 
94
+ # Find a +Document+ by its id.
95
+ def find_by_id(id)
96
+ find(id)
97
+ end
98
+
92
99
  # Find the first +Document+ given the conditions.
93
100
  #
94
101
  # Options:
@@ -251,7 +258,7 @@ module Mongoid #:nodoc:
251
258
  # Writes all the attributes of this Document, and delegate up to
252
259
  # the parent.
253
260
  def write_attributes(attrs)
254
- @attributes = attrs
261
+ @attributes = process(fields, attrs)
255
262
  notify
256
263
  end
257
264
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.6.5"
8
+ s.version = "0.6.6"
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-11-02}
12
+ s.date = %q{2009-11-09}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
36
36
  "lib/mongoid/commands/destroy.rb",
37
37
  "lib/mongoid/commands/destroy_all.rb",
38
38
  "lib/mongoid/commands/save.rb",
39
+ "lib/mongoid/commands/validate.rb",
39
40
  "lib/mongoid/criteria.rb",
40
41
  "lib/mongoid/document.rb",
41
42
  "lib/mongoid/extensions.rb",
@@ -74,6 +75,7 @@ Gem::Specification.new do |s|
74
75
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
75
76
  "spec/unit/mongoid/commands/destroy_spec.rb",
76
77
  "spec/unit/mongoid/commands/save_spec.rb",
78
+ "spec/unit/mongoid/commands/validate_spec.rb",
77
79
  "spec/unit/mongoid/commands_spec.rb",
78
80
  "spec/unit/mongoid/criteria_spec.rb",
79
81
  "spec/unit/mongoid/document_spec.rb",
@@ -116,6 +118,7 @@ Gem::Specification.new do |s|
116
118
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
117
119
  "spec/unit/mongoid/commands/destroy_spec.rb",
118
120
  "spec/unit/mongoid/commands/save_spec.rb",
121
+ "spec/unit/mongoid/commands/validate_spec.rb",
119
122
  "spec/unit/mongoid/commands_spec.rb",
120
123
  "spec/unit/mongoid/criteria_spec.rb",
121
124
  "spec/unit/mongoid/document_spec.rb",
@@ -72,6 +72,18 @@ describe Mongoid::Document do
72
72
 
73
73
  end
74
74
 
75
+ describe "#find_by_id" do
76
+
77
+ before do
78
+ @person = Person.create
79
+ end
80
+
81
+ it "returns the document with the matching id" do
82
+ Person.find_by_id(@person.id).should == @person
83
+ end
84
+
85
+ end
86
+
75
87
  describe "#group" do
76
88
 
77
89
  before do
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))
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
@@ -234,6 +234,19 @@ describe Mongoid::Document do
234
234
 
235
235
  end
236
236
 
237
+ describe "#find_by_id" do
238
+
239
+ before do
240
+ @criteria = stub_everything
241
+ end
242
+
243
+ it "delegates to find with an id parameter" do
244
+ Mongoid::Criteria.expects(:translate).with("1").returns(@criteria)
245
+ Person.find_by_id("1")
246
+ end
247
+
248
+ end
249
+
237
250
  describe "#first" do
238
251
 
239
252
  before do
@@ -561,6 +574,20 @@ describe Mongoid::Document do
561
574
 
562
575
  describe "#write_attributes" do
563
576
 
577
+ context "typecasting" do
578
+
579
+ before do
580
+ @person = Person.new
581
+ @attributes = { :age => "50" }
582
+ end
583
+
584
+ it "properly casts values" do
585
+ @person.write_attributes(@attributes)
586
+ @person.age.should == 50
587
+ end
588
+
589
+ end
590
+
564
591
  context "on a child document" do
565
592
 
566
593
  context "when child is part of a has one" do
@@ -3,7 +3,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_he
3
3
  describe Mongoid::Extensions::Date::Conversions do
4
4
 
5
5
  before do
6
- @time = Date.today.to_time.utc
6
+ @time = Date.today.to_time
7
7
  end
8
8
 
9
9
  describe "#set" do
@@ -11,7 +11,7 @@ describe Mongoid::Extensions::Date::Conversions do
11
11
  context "when string provided" do
12
12
 
13
13
  it "returns a time from the string" do
14
- Date.set(@time.to_s).should == @time
14
+ Date.set(@time.utc.to_s).should == @time
15
15
  end
16
16
 
17
17
  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.6.5
4
+ version: 0.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-02 00:00:00 -05:00
12
+ date: 2009-11-09 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -92,6 +92,7 @@ files:
92
92
  - lib/mongoid/commands/destroy.rb
93
93
  - lib/mongoid/commands/destroy_all.rb
94
94
  - lib/mongoid/commands/save.rb
95
+ - lib/mongoid/commands/validate.rb
95
96
  - lib/mongoid/criteria.rb
96
97
  - lib/mongoid/document.rb
97
98
  - lib/mongoid/extensions.rb
@@ -130,6 +131,7 @@ files:
130
131
  - spec/unit/mongoid/commands/destroy_all_spec.rb
131
132
  - spec/unit/mongoid/commands/destroy_spec.rb
132
133
  - spec/unit/mongoid/commands/save_spec.rb
134
+ - spec/unit/mongoid/commands/validate_spec.rb
133
135
  - spec/unit/mongoid/commands_spec.rb
134
136
  - spec/unit/mongoid/criteria_spec.rb
135
137
  - spec/unit/mongoid/document_spec.rb
@@ -194,6 +196,7 @@ test_files:
194
196
  - spec/unit/mongoid/commands/destroy_all_spec.rb
195
197
  - spec/unit/mongoid/commands/destroy_spec.rb
196
198
  - spec/unit/mongoid/commands/save_spec.rb
199
+ - spec/unit/mongoid/commands/validate_spec.rb
197
200
  - spec/unit/mongoid/commands_spec.rb
198
201
  - spec/unit/mongoid/criteria_spec.rb
199
202
  - spec/unit/mongoid/document_spec.rb