mongodb_model 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ module Mongo::Model::Conversion
28
28
  profile_options = self.class.profiles[profile] || raise("profile :#{profile} not defined for #{self.class}!")
29
29
  to_rson profile_options.merge(_profile: profile)
30
30
  else
31
- options.validate_options! :only, :except, :methods, :_profile
31
+ options.validate_options! :only, :except, :methods, :errors, :_profile
32
32
  child_options = options[:_profile] ? {profile: options[:_profile]} : {}
33
33
 
34
34
  instance_variables = Mongo::Object.instance_variables(self)
@@ -47,13 +47,19 @@ module Mongo::Model::Conversion
47
47
  end
48
48
 
49
49
  methods = options[:methods] ? Array(options[:methods]) : []
50
-
51
50
  methods.each do |method|
52
51
  value = send method
53
52
  value = Mongo::Object.convert value, :to_rson, child_options
54
53
  result[method.to_s] = value
55
54
  end
56
55
 
56
+ with_errors = options.include?(:errors) ? options[:errors] : true
57
+ if with_errors and !(errors = self.errors).empty?
58
+ stringified_errors = {}
59
+ errors.each{|k, v| stringified_errors[k.to_s] = v}
60
+ result['errors'] = stringified_errors
61
+ end
62
+
57
63
  result
58
64
  end
59
65
  end
@@ -4,7 +4,8 @@ module Mongo::Model
4
4
  attr_accessor :_id, :_class
5
5
 
6
6
  def _id?; !!_id end
7
- def new_record?; !_id end
7
+ def new?; !_id end
8
+ alias_method :new_record?, :new?
8
9
 
9
10
  inherited do
10
11
  unless is?(Array) or is?(Hash)
@@ -82,6 +82,24 @@ describe 'Model callbacks' do
82
82
  ]
83
83
  }
84
84
  end
85
+
86
+ it "should include errors" do
87
+ Post.class_eval do
88
+ validates_presence_of :token
89
+ end
90
+
91
+ post = Post.new text: 'StarCraft releasing soon!'
92
+ post.valid?.should be_false
93
+
94
+ post.to_rson.should == {
95
+ 'text' => 'StarCraft releasing soon!',
96
+ 'errors' => {"token" => ["can't be empty"]}
97
+ }
98
+
99
+ post.to_rson(errors: false).should == {
100
+ 'text' => 'StarCraft releasing soon!'
101
+ }
102
+ end
85
103
 
86
104
  it "to_json" do
87
105
  post = build_post_with_comment
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-22 00:00:00.000000000Z
12
+ date: 2011-10-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongodb
16
- requirement: &2832660 !ruby/object:Gem::Requirement
16
+ requirement: &2829180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2832660
24
+ version_requirements: *2829180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: validatable2
27
- requirement: &2832420 !ruby/object:Gem::Requirement
27
+ requirement: &2828940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2832420
35
+ version_requirements: *2828940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ruby_ext
38
- requirement: &2832180 !ruby/object:Gem::Requirement
38
+ requirement: &2828700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2832180
46
+ version_requirements: *2828700
47
47
  description:
48
48
  email:
49
49
  executables: []