mongo_mapper 0.11.1 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,7 +6,7 @@ A Ruby Object Mapper for Mongo.
6
6
 
7
7
  $ gem install mongo_mapper
8
8
 
9
- == Note on Patches/Pull Requests
9
+ == Contributing
10
10
 
11
11
  * Fork the project.
12
12
  * Make your feature addition or bug fix.
@@ -30,4 +30,4 @@ irc://chat.freenode.net/#mongomapper
30
30
 
31
31
  == Copyright
32
32
 
33
- See LICENSE for details.
33
+ See LICENSE for details.
@@ -14,7 +14,10 @@ module MongoMapper
14
14
 
15
15
  # raised when document not valid and using !
16
16
  class DocumentNotValid < Error
17
+ attr_reader :document
18
+
17
19
  def initialize(document)
20
+ @document = document
18
21
  super("Validation failed: #{document.errors.full_messages.join(", ")}")
19
22
  end
20
23
  end
@@ -4,13 +4,21 @@ module MongoMapper
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- class_attribute :accessible_attributes
7
+ class_attribute :_accessible_attributes
8
8
  end
9
9
 
10
10
  module ClassMethods
11
11
  def attr_accessible(*attrs)
12
12
  raise AccessibleOrProtected.new(name) if try(:protected_attributes?)
13
- self.accessible_attributes = Set.new(attrs) + (accessible_attributes || [])
13
+ self._accessible_attributes = Set.new(attrs) + (_accessible_attributes || [])
14
+ end
15
+
16
+ def accessible_attributes(*)
17
+ _accessible_attributes
18
+ end
19
+
20
+ def accessible_attributes?
21
+ _accessible_attributes?
14
22
  end
15
23
  end
16
24
 
@@ -26,6 +34,14 @@ module MongoMapper
26
34
  super(filter_inaccessible_attrs(attrs))
27
35
  end
28
36
 
37
+ def accessible_attributes(*args)
38
+ self.class.accessible_attributes(*args)
39
+ end
40
+
41
+ def accessible_attributes?
42
+ self.class.accessible_attributes?
43
+ end
44
+
29
45
  protected
30
46
  def filter_inaccessible_attrs(attrs)
31
47
  return attrs if !accessible_attributes? || attrs.blank?
@@ -17,6 +17,7 @@ module MongoMapper
17
17
 
18
18
  def setup(model)
19
19
  model.key foreign_key, ObjectId unless model.key?(foreign_key)
20
+ model.key type_key_name, String unless model.key?(type_key_name) if polymorphic?
20
21
  super
21
22
  add_touch_callbacks if touch?
22
23
  end
@@ -4,7 +4,8 @@ module MongoMapper
4
4
  module Associations
5
5
  class ManyDocumentsProxy < Collection
6
6
  include DynamicQuerying::ClassMethods
7
- include Querying::PluckyMethods
7
+
8
+ def_delegators :query, *(Querying::Methods - [:to_a, :size, :empty?])
8
9
 
9
10
  def replace(docs)
10
11
  load_target
@@ -121,7 +121,11 @@ module MongoMapper
121
121
  attribute = key.name.to_sym
122
122
 
123
123
  if key.options[:required]
124
- validates_presence_of(attribute)
124
+ if key.type == Boolean
125
+ validates_inclusion_of attribute, :in => [true, false]
126
+ else
127
+ validates_presence_of(attribute)
128
+ end
125
129
  end
126
130
 
127
131
  if key.options[:unique]
@@ -21,20 +21,22 @@ module MongoMapper
21
21
  updates.each do |key, value|
22
22
  updates[key] = keys[key.to_s].set(value) if key?(key)
23
23
  end
24
- collection.update(criteria, {'$set' => updates}, :multi => true)
24
+ modifier_update('$set', [criteria, updates, options])
25
25
  end
26
26
 
27
27
  def unset(*args)
28
28
  if args[0].is_a?(Hash)
29
29
  criteria, keys = args.shift, args
30
+ options = keys.last.is_a?(Hash) ? keys.pop : {}
30
31
  else
31
32
  keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
33
+ options = ids.last.is_a?(Hash) ? ids.pop : {}
32
34
  criteria = {:id => ids}
33
35
  end
34
36
 
35
- criteria = criteria_hash(criteria).to_hash
36
- modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
37
- collection.update(criteria, {'$unset' => modifiers}, :multi => true)
37
+ criteria = criteria_hash(criteria).to_hash
38
+ updates = keys.inject({}) { |hash, key| hash[key] = 1; hash }
39
+ modifier_update('$unset', [criteria, updates, options])
38
40
  end
39
41
 
40
42
  def push(*args)
@@ -87,8 +89,8 @@ module MongoMapper
87
89
  end
88
90
  end
89
91
 
90
- def unset(*keys)
91
- self.class.unset(id, *keys)
92
+ def unset(*args)
93
+ self.class.unset(id, *args)
92
94
  end
93
95
 
94
96
  def increment(hash, options=nil)
@@ -1,6 +1,5 @@
1
1
  # encoding: UTF-8
2
2
  require 'mongo_mapper/plugins/querying/decorator'
3
- require 'mongo_mapper/plugins/querying/plucky_methods'
4
3
 
5
4
  module MongoMapper
6
5
  module Plugins
@@ -9,13 +8,8 @@ module MongoMapper
9
8
 
10
9
  module ClassMethods
11
10
  extend Forwardable
12
- include PluckyMethods
13
11
 
14
- def_delegators :query, :to_a, :size, :empty?
15
-
16
- def find_each(opts={})
17
- super(opts).each { |doc| yield(doc) }
18
- end
12
+ def_delegators :query, *Querying::Methods
19
13
 
20
14
  def find_by_id(id)
21
15
  find_one(:_id => id)
@@ -2,6 +2,8 @@
2
2
  module MongoMapper
3
3
  module Plugins
4
4
  module Querying
5
+ Methods = Plucky::Methods + [:find!]
6
+
5
7
  module Decorator
6
8
  include DynamicQuerying::ClassMethods
7
9
 
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapper
3
- Version = '0.11.1'
4
- end
3
+ Version = '0.11.2'
4
+ end
@@ -13,7 +13,9 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
13
13
 
14
14
  should "add type and id key to polymorphic class base" do
15
15
  PostComment.keys.keys.should include('commentable_type')
16
+ PostComment.keys['commentable_type'].type.should == String
16
17
  PostComment.keys.keys.should include('commentable_id')
18
+ PostComment.keys['commentable_id'].type.should == ObjectId
17
19
  end
18
20
 
19
21
  should "allow adding to association like it was an array" do
@@ -226,4 +228,4 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
226
228
  end
227
229
  end
228
230
  end
229
- end
231
+ end
@@ -43,6 +43,33 @@ class ModifierTest < Test::Unit::TestCase
43
43
  assert_keys_removed @page, :title, :tags
44
44
  assert_keys_removed @page2, :title, :tags
45
45
  end
46
+
47
+ context "additional options (upsert & safe)" do
48
+ should "be able to pass upsert option" do
49
+ new_key_value = DateTime.now.to_s
50
+ @page_class.unset({:title => new_key_value, :tags => %w(foo bar)}, :tags, {:upsert => true})
51
+ @page_class.count(:title => new_key_value).should == 1
52
+ @page_class.first(:title => new_key_value).tags.should == []
53
+ end
54
+
55
+ should "be able to pass safe option" do
56
+ @page_class.create(:title => "Better Be Safe than Sorry")
57
+
58
+ Mongo::Collection.any_instance.expects(:update).with(
59
+ {:title => "Better Be Safe than Sorry"},
60
+ {'$unset' => {:tags => 1}},
61
+ {:safe => true, :multi => true}
62
+ )
63
+ @page_class.unset({:title => "Better Be Safe than Sorry"}, :tags, {:safe => true})
64
+ end
65
+
66
+ should "be able to pass both safe and upsert options" do
67
+ new_key_value = DateTime.now.to_s
68
+ @page_class.unset({:title => new_key_value, :tags => %w(foo bar)}, :tags, {:upsert => true, :safe => true})
69
+ @page_class.count(:title => new_key_value).should == 1
70
+ @page_class.first(:title => new_key_value).tags.should == []
71
+ end
72
+ end
46
73
  end
47
74
 
48
75
  context "increment" do
@@ -141,6 +168,33 @@ class ModifierTest < Test::Unit::TestCase
141
168
  @page.reload
142
169
  @page[:colors].should == %w[red green]
143
170
  end
171
+
172
+ context "additional options (upsert & safe)" do
173
+ should "be able to pass upsert option" do
174
+ new_key_value = DateTime.now.to_s
175
+ @page_class.set({:title => new_key_value}, {:day_count => 1}, {:upsert => true})
176
+ @page_class.count(:title => new_key_value).should == 1
177
+ @page_class.first(:title => new_key_value).day_count.should == 1
178
+ end
179
+
180
+ should "be able to pass safe option" do
181
+ @page_class.create(:title => "Better Be Safe than Sorry")
182
+
183
+ Mongo::Collection.any_instance.expects(:update).with(
184
+ {:title => "Better Be Safe than Sorry"},
185
+ {'$set' => {:title => "I like safety."}},
186
+ {:safe => true, :multi => true}
187
+ )
188
+ @page_class.set({:title => "Better Be Safe than Sorry"}, {:title => "I like safety."}, {:safe => true})
189
+ end
190
+
191
+ should "be able to pass both safe and upsert options" do
192
+ new_key_value = DateTime.now.to_s
193
+ @page_class.set({:title => new_key_value}, {:day_count => 1}, {:upsert => true, :safe => true})
194
+ @page_class.count(:title => new_key_value).should == 1
195
+ @page_class.first(:title => new_key_value).day_count.should == 1
196
+ end
197
+ end
144
198
  end
145
199
 
146
200
  context "push" do
@@ -33,6 +33,15 @@ class ValidationsTest < Test::Unit::TestCase
33
33
  doc = @document.new
34
34
  lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
35
35
  end
36
+
37
+ should "set document on exception" do
38
+ doc = @document.new
39
+ begin
40
+ doc.save!
41
+ rescue MongoMapper::DocumentNotValid => e
42
+ e.document.should == doc
43
+ end
44
+ end
36
45
  end
37
46
 
38
47
  context "Creating a document that is invalid (destructive)" do
data/test/test_helper.rb CHANGED
@@ -6,7 +6,7 @@ require 'mongo_mapper'
6
6
  require 'fileutils'
7
7
  require 'ostruct'
8
8
 
9
- Bundler.require(:development)
9
+ Bundler.require(:default, :test)
10
10
 
11
11
  class Test::Unit::TestCase
12
12
  def Doc(name='Class', &block)
@@ -88,7 +88,6 @@ log_dir = File.expand_path('../../log', __FILE__)
88
88
  FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
89
89
  logger = Logger.new(log_dir + '/test.log')
90
90
 
91
- LogBuddy.init(:logger => logger)
92
91
  MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
93
92
  MongoMapper.database = "test"
94
93
  MongoMapper.database.collections.each { |c| c.drop_indexes }
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class ExtensionsTest < Test::Unit::TestCase
4
+ context "DocumentNotValid" do
5
+ should "have document reader method" do
6
+ doc_class = Doc()
7
+ instance = doc_class.new
8
+ exception = MongoMapper::DocumentNotValid.new(instance)
9
+ exception.document.should == instance
10
+ end
11
+ end
12
+ end
@@ -13,6 +13,11 @@ class TestRailtie < Test::Unit::TestCase
13
13
  initializer = mod.initializers.detect do |i|
14
14
  i.name == name
15
15
  end
16
+
17
+ if initializer.nil?
18
+ raise 'Initializer not found'
19
+ end
20
+
16
21
  initializer.block.arity == -1 ? initializer.run : initializer.run(FakeRails)
17
22
  # mongo_mapper.prepare_dispatcher takes a Rails app as its one arg,
18
23
  # set_clear_dependencies_hook takes no args
@@ -245,6 +245,19 @@ class ValidationsTest < Test::Unit::TestCase
245
245
  doc.should_not have_error_on(:action)
246
246
  end
247
247
 
248
+ should "work with :required shortcut on Boolean type" do
249
+ @document.key :flag, Boolean, :required => true
250
+
251
+ doc = @document.new
252
+ doc.should have_error_on(:flag, 'is not included in the list')
253
+
254
+ doc.flag = true
255
+ doc.should_not have_error_on(:action)
256
+
257
+ doc.flag = false
258
+ doc.should_not have_error_on(:action)
259
+ end
260
+
248
261
  should "not have error if allow nil is true and value is nil" do
249
262
  @document.key :action, String
250
263
  @document.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
metadata CHANGED
@@ -1,78 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
- version: !ruby/object:Gem::Version
4
- hash: 49
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.11.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 11
9
- - 1
10
- version: 0.11.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - John Nunemaker
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-03-30 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
- none: false
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- hash: 7
27
- segments:
28
- - 3
29
- - 0
30
- version: "3.0"
31
- prerelease: false
32
- requirement: *id001
12
+ date: 2012-07-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
33
15
  name: activemodel
34
- type: :runtime
35
- - !ruby/object:Gem::Dependency
36
- version_requirements: &id002 !ruby/object:Gem::Requirement
16
+ requirement: &70319139905420 !ruby/object:Gem::Requirement
37
17
  none: false
38
- requirements:
18
+ requirements:
39
19
  - - ~>
40
- - !ruby/object:Gem::Version
41
- hash: 7
42
- segments:
43
- - 3
44
- - 0
45
- version: "3.0"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
46
23
  prerelease: false
47
- requirement: *id002
24
+ version_requirements: *70319139905420
25
+ - !ruby/object:Gem::Dependency
48
26
  name: activesupport
49
- type: :runtime
50
- - !ruby/object:Gem::Dependency
51
- version_requirements: &id003 !ruby/object:Gem::Requirement
27
+ requirement: &70319139904940 !ruby/object:Gem::Requirement
52
28
  none: false
53
- requirements:
29
+ requirements:
54
30
  - - ~>
55
- - !ruby/object:Gem::Version
56
- hash: 15
57
- segments:
58
- - 0
59
- - 4
60
- - 0
61
- version: 0.4.0
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
62
34
  prerelease: false
63
- requirement: *id003
35
+ version_requirements: *70319139904940
36
+ - !ruby/object:Gem::Dependency
64
37
  name: plucky
38
+ requirement: &70319139904480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.5.1
65
44
  type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70319139904480
66
47
  description:
67
- email:
48
+ email:
68
49
  - nunemaker@gmail.com
69
- executables:
50
+ executables:
70
51
  - mmconsole
71
52
  extensions: []
72
-
73
53
  extra_rdoc_files: []
74
-
75
- files:
54
+ files:
76
55
  - bin/mmconsole
77
56
  - examples/attr_accessible.rb
78
57
  - examples/attr_protected.rb
@@ -151,7 +130,6 @@ files:
151
130
  - lib/mongo_mapper/plugins/persistence.rb
152
131
  - lib/mongo_mapper/plugins/protected.rb
153
132
  - lib/mongo_mapper/plugins/querying/decorator.rb
154
- - lib/mongo_mapper/plugins/querying/plucky_methods.rb
155
133
  - lib/mongo_mapper/plugins/querying.rb
156
134
  - lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb
157
135
  - lib/mongo_mapper/plugins/rails.rb
@@ -229,6 +207,7 @@ files:
229
207
  - test/unit/test_dynamic_finder.rb
230
208
  - test/unit/test_embedded_document.rb
231
209
  - test/unit/test_equality.rb
210
+ - test/unit/test_exceptions.rb
232
211
  - test/unit/test_extensions.rb
233
212
  - test/unit/test_identity_map_middleware.rb
234
213
  - test/unit/test_inspect.rb
@@ -250,36 +229,32 @@ files:
250
229
  - README.rdoc
251
230
  homepage: http://github.com/jnunemaker/mongomapper
252
231
  licenses: []
253
-
254
232
  post_install_message:
255
233
  rdoc_options: []
256
-
257
- require_paths:
234
+ require_paths:
258
235
  - lib
259
- required_ruby_version: !ruby/object:Gem::Requirement
236
+ required_ruby_version: !ruby/object:Gem::Requirement
260
237
  none: false
261
- requirements:
262
- - - ">="
263
- - !ruby/object:Gem::Version
264
- hash: 3
265
- segments:
238
+ requirements:
239
+ - - ! '>='
240
+ - !ruby/object:Gem::Version
241
+ version: '0'
242
+ segments:
266
243
  - 0
267
- version: "0"
268
- required_rubygems_version: !ruby/object:Gem::Requirement
244
+ hash: -1944766579498551313
245
+ required_rubygems_version: !ruby/object:Gem::Requirement
269
246
  none: false
270
- requirements:
271
- - - ">="
272
- - !ruby/object:Gem::Version
273
- hash: 3
274
- segments:
247
+ requirements:
248
+ - - ! '>='
249
+ - !ruby/object:Gem::Version
250
+ version: '0'
251
+ segments:
275
252
  - 0
276
- version: "0"
253
+ hash: -1944766579498551313
277
254
  requirements: []
278
-
279
255
  rubyforge_project:
280
- rubygems_version: 1.8.15
256
+ rubygems_version: 1.8.10
281
257
  signing_key:
282
258
  specification_version: 3
283
259
  summary: A Ruby Object Mapper for Mongo
284
260
  test_files: []
285
-
@@ -1,21 +0,0 @@
1
- # encoding: UTF-8
2
- require 'forwardable'
3
-
4
- module MongoMapper
5
- module Plugins
6
- module Querying
7
- module PluckyMethods
8
- extend Forwardable
9
- def_delegators :query, :where, :filter,
10
- :fields, :ignore, :only,
11
- :limit, :paginate, :per_page, :skip, :offset,
12
- :sort, :order, :reverse,
13
- :count,
14
- :distinct,
15
- :last, :first, :find_one, :all, :find_each,
16
- :find, :find!,
17
- :exists?, :exist?
18
- end
19
- end
20
- end
21
- end