mongomodel 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +23 -11
  3. data/gemfiles/rails-3.2.gemfile +2 -2
  4. data/gemfiles/rails-4-observers.gemfile +1 -1
  5. data/gemfiles/rails-4.1.gemfile +2 -2
  6. data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
  7. data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
  8. data/gemfiles/rails-5.1.gemfile +10 -0
  9. data/gemfiles/rails-5.2.gemfile +10 -0
  10. data/lib/mongomodel.rb +15 -15
  11. data/lib/mongomodel/attributes/mongo.rb +7 -7
  12. data/lib/mongomodel/attributes/store.rb +4 -4
  13. data/lib/mongomodel/attributes/typecasting.rb +7 -7
  14. data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
  15. data/lib/mongomodel/compatibility/mongoid.rb +3 -3
  16. data/lib/mongomodel/concerns/abstract_class.rb +3 -3
  17. data/lib/mongomodel/concerns/activemodel.rb +4 -4
  18. data/lib/mongomodel/concerns/associations.rb +8 -8
  19. data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
  20. data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
  21. data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
  22. data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
  23. data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
  24. data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
  25. data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
  26. data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
  27. data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
  28. data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
  29. data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
  30. data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
  31. data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
  32. data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
  33. data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
  34. data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
  35. data/lib/mongomodel/concerns/attributes.rb +18 -18
  36. data/lib/mongomodel/concerns/callbacks.rb +7 -7
  37. data/lib/mongomodel/concerns/document_parent.rb +2 -2
  38. data/lib/mongomodel/concerns/logging.rb +2 -2
  39. data/lib/mongomodel/concerns/map_reduce.rb +11 -11
  40. data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
  41. data/lib/mongomodel/concerns/properties.rb +18 -18
  42. data/lib/mongomodel/concerns/record_status.rb +9 -13
  43. data/lib/mongomodel/concerns/serialization.rb +4 -4
  44. data/lib/mongomodel/concerns/timestamps.rb +4 -4
  45. data/lib/mongomodel/concerns/translation.rb +2 -2
  46. data/lib/mongomodel/concerns/validations.rb +5 -5
  47. data/lib/mongomodel/concerns/validations/associated.rb +1 -1
  48. data/lib/mongomodel/document.rb +6 -6
  49. data/lib/mongomodel/document/callbacks.rb +15 -21
  50. data/lib/mongomodel/document/collection_modifiers.rb +5 -5
  51. data/lib/mongomodel/document/dynamic_finders.rb +1 -1
  52. data/lib/mongomodel/document/indexes.rb +19 -19
  53. data/lib/mongomodel/document/optimistic_locking.rb +7 -7
  54. data/lib/mongomodel/document/persistence.rb +23 -23
  55. data/lib/mongomodel/document/scopes.rb +20 -20
  56. data/lib/mongomodel/document/validations.rb +6 -6
  57. data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
  58. data/lib/mongomodel/embedded_document.rb +11 -11
  59. data/lib/mongomodel/locale/en.yml +0 -1
  60. data/lib/mongomodel/log_subscriber.rb +5 -5
  61. data/lib/mongomodel/railtie.rb +13 -13
  62. data/lib/mongomodel/support/collection.rb +31 -31
  63. data/lib/mongomodel/support/configuration.rb +11 -11
  64. data/lib/mongomodel/support/core_extensions.rb +1 -1
  65. data/lib/mongomodel/support/dynamic_finder.rb +12 -12
  66. data/lib/mongomodel/support/exceptions.rb +6 -6
  67. data/lib/mongomodel/support/instrumented_collection.rb +20 -20
  68. data/lib/mongomodel/support/map.rb +33 -33
  69. data/lib/mongomodel/support/mongo_operator.rb +6 -6
  70. data/lib/mongomodel/support/mongo_options.rb +18 -18
  71. data/lib/mongomodel/support/mongo_order.rb +16 -16
  72. data/lib/mongomodel/support/paginator.rb +8 -8
  73. data/lib/mongomodel/support/reference.rb +10 -10
  74. data/lib/mongomodel/support/scope.rb +37 -37
  75. data/lib/mongomodel/support/scope/array_methods.rb +1 -1
  76. data/lib/mongomodel/support/scope/batches.rb +1 -1
  77. data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
  78. data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
  79. data/lib/mongomodel/support/scope/load_methods.rb +1 -1
  80. data/lib/mongomodel/support/scope/pagination.rb +1 -1
  81. data/lib/mongomodel/support/scope/query_methods.rb +6 -6
  82. data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
  83. data/lib/mongomodel/support/types.rb +2 -2
  84. data/lib/mongomodel/support/types/array.rb +1 -1
  85. data/lib/mongomodel/support/types/boolean.rb +3 -3
  86. data/lib/mongomodel/support/types/custom.rb +3 -3
  87. data/lib/mongomodel/support/types/date.rb +2 -2
  88. data/lib/mongomodel/support/types/date_time.rb +6 -16
  89. data/lib/mongomodel/support/types/float.rb +1 -1
  90. data/lib/mongomodel/support/types/hash.rb +1 -1
  91. data/lib/mongomodel/support/types/integer.rb +13 -10
  92. data/lib/mongomodel/support/types/object.rb +5 -5
  93. data/lib/mongomodel/support/types/rational.rb +3 -3
  94. data/lib/mongomodel/support/types/time.rb +2 -2
  95. data/lib/mongomodel/version.rb +1 -1
  96. data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
  97. data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
  98. data/mongomodel.gemspec +5 -4
  99. data/spec/mongomodel/attributes/store_spec.rb +21 -21
  100. data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
  101. data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
  102. data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
  103. data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
  104. data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
  105. data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
  106. data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
  107. data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
  108. data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
  109. data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
  110. data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
  111. data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
  112. data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
  113. data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
  114. data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
  115. data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
  116. data/spec/mongomodel/concerns/logging_spec.rb +5 -3
  117. data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
  118. data/spec/mongomodel/concerns/observing_spec.rb +3 -3
  119. data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
  120. data/spec/mongomodel/concerns/properties_spec.rb +11 -11
  121. data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
  122. data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
  123. data/spec/mongomodel/concerns/validations_spec.rb +41 -38
  124. data/spec/mongomodel/document/callbacks_spec.rb +20 -16
  125. data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
  126. data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
  127. data/spec/mongomodel/document/finders_spec.rb +15 -15
  128. data/spec/mongomodel/document/indexes_spec.rb +29 -29
  129. data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
  130. data/spec/mongomodel/document/persistence_spec.rb +39 -39
  131. data/spec/mongomodel/document/scopes_spec.rb +17 -17
  132. data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
  133. data/spec/mongomodel/document/validations_spec.rb +35 -35
  134. data/spec/mongomodel/document_spec.rb +19 -19
  135. data/spec/mongomodel/embedded_document_spec.rb +19 -19
  136. data/spec/mongomodel/mongomodel_spec.rb +7 -6
  137. data/spec/mongomodel/support/collection_spec.rb +54 -54
  138. data/spec/mongomodel/support/configuration_spec.rb +1 -1
  139. data/spec/mongomodel/support/map_spec.rb +66 -66
  140. data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
  141. data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
  142. data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
  143. data/spec/mongomodel/support/paginator_spec.rb +15 -15
  144. data/spec/mongomodel/support/property_spec.rb +29 -23
  145. data/spec/mongomodel/support/scope_spec.rb +205 -204
  146. data/spec/spec_helper.rb +13 -2
  147. data/spec/support/callbacks.rb +3 -8
  148. data/spec/support/helpers/define_class.rb +7 -7
  149. data/spec/support/helpers/document_finder_stubs.rb +6 -6
  150. data/spec/support/helpers/specs_for.rb +1 -1
  151. data/spec/support/helpers/validations.rb +1 -1
  152. data/spec/support/matchers/find_with.rb +8 -8
  153. data/spec/support/matchers/respond_to_boolean.rb +3 -3
  154. data/spec/support/matchers/run_callbacks.rb +6 -9
  155. data/spec/support/models.rb +5 -5
  156. metadata +23 -7
@@ -6,69 +6,73 @@ module MongoModel
6
6
  define_class(:CallbackTestDocument, Document) do
7
7
  include MongoModel::CallbackHelpers
8
8
  end
9
-
9
+
10
10
  let(:doc) { CallbackTestDocument.create! }
11
-
11
+
12
12
  it "runs each type of callback when initializing" do
13
13
  instance = CallbackTestDocument.new
14
14
  instance.should run_callbacks(:after_initialize)
15
15
  end
16
-
16
+
17
17
  it "runs each type of callback on find" do
18
18
  instance = CallbackTestDocument.find(doc.id)
19
19
  instance.should run_callbacks(:after_initialize, :after_find)
20
20
  end
21
-
21
+
22
22
  it "runs each type of callback when validating a new document" do
23
23
  instance = CallbackTestDocument.new
24
24
  instance.valid?
25
25
  instance.should run_callbacks(:after_initialize, :before_validation, :after_validation)
26
26
  end
27
-
27
+
28
28
  it "runs each type of callback when validating an existing document" do
29
29
  instance = CallbackTestDocument.find(doc.id)
30
30
  instance.valid?
31
31
  instance.should run_callbacks(:after_initialize, :after_find, :before_validation, :after_validation)
32
32
  end
33
-
33
+
34
34
  it "runs each type of callback when creating a document" do
35
35
  instance = CallbackTestDocument.create!
36
36
  instance.should run_callbacks(:after_initialize, :before_validation, :after_validation, :before_save, :before_create, :after_create, :after_save)
37
37
  end
38
-
38
+
39
39
  it "runs each type of callback when saving an existing document" do
40
40
  instance = CallbackTestDocument.find(doc.id)
41
41
  instance.save
42
42
  instance.should run_callbacks(:after_initialize, :after_find, :before_validation, :after_validation, :before_save, :before_update, :after_update, :after_save)
43
43
  end
44
-
44
+
45
45
  it "runs each type of callback when destroying a document" do
46
46
  instance = CallbackTestDocument.find(doc.id)
47
47
  instance.destroy
48
48
  instance.should run_callbacks(:after_initialize, :after_find, :before_destroy, :after_destroy)
49
49
  end
50
-
50
+
51
51
  it "does not run destroy callbacks when deleting a document" do
52
52
  instance = CallbackTestDocument.find(doc.id)
53
53
  instance.delete
54
54
  instance.should run_callbacks(:after_initialize, :after_find)
55
55
  end
56
56
  end
57
-
57
+
58
58
  [ :before_save, :before_create ].each do |callback|
59
- context "#{callback} callback return false" do
59
+ context "#{callback} callback halts" do
60
60
  define_class(:CallbackTestDocument, Document) do
61
- send(callback) { false }
61
+ if Gem::Version.new(ActiveSupport::VERSION::STRING) >= Gem::Version.new('5.0.0')
62
+ send(callback) { throw :abort }
63
+ else
64
+ send(callback) { false }
65
+ end
62
66
  end
63
-
67
+
64
68
  subject { CallbackTestDocument.new }
65
-
69
+
66
70
  describe "#save" do
67
71
  it "returns false" do
68
- subject.save.should be_false
72
+ subject.save.should be false
69
73
  end
70
74
  end
71
-
75
+
72
76
  describe "#save!" do
73
77
  it "raises a MongoModel::DocumentNotSaved exception" do
74
78
  lambda { subject.save! }.should raise_error(MongoModel::DocumentNotSaved)
@@ -8,96 +8,96 @@ module MongoModel
8
8
  property :available, Integer
9
9
  property :tags, Array
10
10
  end
11
-
11
+
12
12
  let(:collection) { Post.collection }
13
13
  subject { Post }
14
-
14
+
15
15
  def self.should_update_collection(expression, &block)
16
16
  it "updates the collection" do
17
17
  collection.should_receive(:update).with(selector, expression, :multi => true)
18
18
  instance_eval(&block)
19
19
  end
20
20
  end
21
-
21
+
22
22
  shared_examples_for "collection modifiers" do
23
23
  describe "increment!" do
24
24
  should_update_collection('$inc' => { 'hits' => 1, 'available' => -1 }) do
25
25
  subject.increment!(:hits => 1, :available => -1)
26
26
  end
27
27
  end
28
-
28
+
29
29
  describe "increase! (alias of increment!)" do
30
30
  should_update_collection('$inc' => { 'hits' => 1, 'available' => -1 }) do
31
31
  subject.increase!(:hits => 1, :available => -1)
32
32
  end
33
33
  end
34
-
34
+
35
35
  describe "set!" do
36
36
  should_update_collection('$set' => { 'hits' => 20, 'available' => 100 }) do
37
37
  subject.set!(:hits => 20, :available => 100)
38
38
  end
39
39
  end
40
-
40
+
41
41
  describe "unset!" do
42
42
  should_update_collection('$unset' => { 'hits' => 1, 'available' => 1 }) do
43
43
  subject.unset!(:hits, :available)
44
44
  end
45
45
  end
46
-
46
+
47
47
  describe "push!" do
48
48
  should_update_collection('$push' => { 'tags' => 'abc' }) do
49
49
  subject.push!(:tags => 'abc')
50
50
  end
51
51
  end
52
-
52
+
53
53
  describe "push_all!" do
54
54
  should_update_collection('$pushAll' => { 'tags' => ['xxx', 'yyy', 'zzz'] }) do
55
55
  subject.push_all!(:tags => ['xxx', 'yyy', 'zzz'])
56
56
  end
57
57
  end
58
-
58
+
59
59
  describe "add_to_set!" do
60
60
  should_update_collection('$addToSet' => { 'tags' => 'xxx' }) do
61
61
  subject.add_to_set!(:tags => 'xxx')
62
62
  end
63
63
  end
64
-
64
+
65
65
  describe "pull!" do
66
66
  should_update_collection('$pull' => { 'tags' => 'abc' }) do
67
67
  subject.pull!(:tags => 'abc')
68
68
  end
69
69
  end
70
-
70
+
71
71
  describe "pull_all!" do
72
72
  should_update_collection('$pullAll' => { 'tags' => ['xxx', 'yyy', 'zzz'] }) do
73
73
  subject.pull_all!(:tags => ['xxx', 'yyy', 'zzz'])
74
74
  end
75
75
  end
76
-
76
+
77
77
  describe "pop!" do
78
78
  should_update_collection('$pop' => { 'tags' => 1 }) do
79
79
  subject.pop!(:tags)
80
80
  end
81
81
  end
82
-
82
+
83
83
  describe "shift!" do
84
84
  should_update_collection('$pop' => { 'tags' => -1 }) do
85
85
  subject.shift!(:tags)
86
86
  end
87
87
  end
88
-
88
+
89
89
  describe "rename!" do
90
90
  should_update_collection('$rename' => { 'tags' => :tag_collection }) do
91
91
  subject.rename!(:tags => :tag_collection)
92
92
  end
93
93
  end
94
94
  end
95
-
95
+
96
96
  describe "class methods" do
97
97
  let(:selector) { {} }
98
98
  it_should_behave_like "collection modifiers"
99
99
  end
100
-
100
+
101
101
  describe "instance methods" do
102
102
  subject { Post.new }
103
103
  before { subject.save }
@@ -7,9 +7,9 @@ module MongoModel
7
7
  property :name, String
8
8
  property :age, Integer
9
9
  end
10
-
10
+
11
11
  subject { Person }
12
-
12
+
13
13
  before(:each) do
14
14
  @john = Person.create!(:name => 'John', :age => 42, :id => '1')
15
15
  @young_john = Person.create!(:name => 'John', :age => 12, :id => '2')
@@ -17,163 +17,163 @@ module MongoModel
17
17
  @tom2 = Person.create!(:name => 'Tom', :age => 23, :id => '4')
18
18
  @mary = Person.create!(:name => 'Mary', :age => 33, :id => '5')
19
19
  end
20
-
20
+
21
21
  def self.should_find(*args, &block)
22
22
  it "returns correct results when called with #{args.inspect}" do
23
23
  expected = instance_eval(&block)
24
24
  subject.send(valid_finder, *args).should == expected
25
25
  end
26
26
  end
27
-
27
+
28
28
  def self.should_raise(*args, &block)
29
29
  it "raises DocumentNotFound exception if results not found" do
30
30
  message = instance_eval(&block)
31
31
  lambda { subject.send(valid_finder, *args) }.should raise_error(DocumentNotFound, message)
32
32
  end
33
33
  end
34
-
34
+
35
35
  def self.should_initialize(*args, &block)
36
36
  it "initializes new instance" do
37
37
  result = subject.send(valid_finder, *args)
38
38
  result.should be_a_new_record
39
39
  result.should be_an_instance_of(Person)
40
- yield(result).should be_true
40
+ yield(result).should be true
41
41
  end
42
42
  end
43
-
43
+
44
44
  def self.should_create(*args, &block)
45
45
  it "creates new instance" do
46
46
  result = subject.send(valid_finder, *args)
47
47
  result.should_not be_a_new_record
48
48
  result.should be_an_instance_of(Person)
49
- yield(result).should be_true
49
+ yield(result).should be true
50
50
  end
51
51
  end
52
-
52
+
53
53
  shared_examples_for "a dynamic finder" do
54
54
  it { should respond_to(valid_finder) }
55
55
  it { should_not respond_to(invalid_finder) }
56
-
56
+
57
57
  it "raises NoMethodError calling an invalid finder" do
58
58
  lambda { subject.send(invalid_finder, "Foo") }.should raise_error(NoMethodError)
59
59
  end
60
60
  end
61
-
61
+
62
62
  describe "find first by single property" do
63
63
  let(:valid_finder) { :find_by_name }
64
64
  let(:invalid_finder) { :find_by_something_else }
65
-
65
+
66
66
  it_should_behave_like "a dynamic finder"
67
-
67
+
68
68
  should_find("John") { @john }
69
69
  should_find("Jane") { nil }
70
70
  end
71
-
71
+
72
72
  describe "find first by single property (bang method)" do
73
73
  let(:valid_finder) { :find_by_name! }
74
74
  let(:invalid_finder) { :find_by_something_else! }
75
-
75
+
76
76
  it_should_behave_like "a dynamic finder"
77
-
77
+
78
78
  should_find("John") { @john }
79
79
  should_raise("Jane") { 'Couldn\'t find Person with {:name=>"Jane"}' }
80
80
  end
81
-
81
+
82
82
  describe "find last by single property" do
83
83
  let(:valid_finder) { :find_last_by_name }
84
84
  let(:invalid_finder) { :find_last_by_something_else }
85
-
85
+
86
86
  it_should_behave_like "a dynamic finder"
87
-
87
+
88
88
  should_find("John") { @young_john }
89
89
  should_find("Jane") { nil }
90
90
  end
91
-
91
+
92
92
  describe "find all by single property" do
93
93
  let(:valid_finder) { :find_all_by_name }
94
94
  let(:invalid_finder) { :find_all_by_something_else }
95
-
95
+
96
96
  it_should_behave_like "a dynamic finder"
97
-
97
+
98
98
  should_find("John") { [@john, @young_john] }
99
99
  end
100
-
100
+
101
101
  describe "find first by multiple properties" do
102
102
  let(:valid_finder) { :find_by_name_and_age }
103
103
  let(:invalid_finder) { :find_by_age_and_something_else }
104
-
104
+
105
105
  it_should_behave_like "a dynamic finder"
106
-
106
+
107
107
  should_find("Tom", 23) { @tom1 }
108
108
  should_find("Tom", 5) { nil }
109
109
  end
110
-
110
+
111
111
  describe "find first by multiple properties (bang method)" do
112
112
  let(:valid_finder) { :find_by_name_and_age! }
113
113
  let(:invalid_finder) { :find_by_age_and_something_else! }
114
-
114
+
115
115
  it_should_behave_like "a dynamic finder"
116
-
116
+
117
117
  should_find("Tom", 23) { @tom1 }
118
118
  should_raise("Tom", 5) { "Couldn\'t find Person with #{{:name=>"Tom", :age=>5}.inspect}" }
119
119
  end
120
-
120
+
121
121
  describe "find all by multiple properties" do
122
122
  let(:valid_finder) { :find_all_by_name_and_age }
123
123
  let(:invalid_finder) { :find_all_by_age_and_something_else }
124
-
124
+
125
125
  it_should_behave_like "a dynamic finder"
126
-
126
+
127
127
  should_find("Tom", 23) { [@tom1, @tom2] }
128
128
  should_find("Tom", 5) { [] }
129
129
  end
130
-
130
+
131
131
  describe "find last by multiple properties" do
132
132
  let(:valid_finder) { :find_last_by_name_and_age }
133
133
  let(:invalid_finder) { :find_last_by_age_and_something_else }
134
-
134
+
135
135
  it_should_behave_like "a dynamic finder"
136
-
136
+
137
137
  should_find("Tom", 23) { @tom2 }
138
138
  should_find("Tom", 5) { nil }
139
139
  end
140
-
140
+
141
141
  describe "find or initialize by single property" do
142
142
  let(:valid_finder) { :find_or_initialize_by_name }
143
143
  let(:invalid_finder) { :find_or_initialize_by_something_else }
144
-
144
+
145
145
  it_should_behave_like "a dynamic finder"
146
-
146
+
147
147
  should_find("John") { @john }
148
148
  should_initialize("Jane") { |p| p.name == "Jane" }
149
149
  end
150
-
150
+
151
151
  describe "find or initialize by multiple properties" do
152
152
  let(:valid_finder) { :find_or_initialize_by_name_and_age }
153
153
  let(:invalid_finder) { :find_or_initialize_by_name_and_something_else }
154
-
154
+
155
155
  it_should_behave_like "a dynamic finder"
156
-
156
+
157
157
  should_find("John", 42) { @john }
158
158
  should_initialize("John", 1) { |p| p.name == "John" && p.age == 1 }
159
159
  end
160
-
160
+
161
161
  describe "find or create by single property" do
162
162
  let(:valid_finder) { :find_or_create_by_name }
163
163
  let(:invalid_finder) { :find_or_create_by_something_else }
164
-
164
+
165
165
  it_should_behave_like "a dynamic finder"
166
-
166
+
167
167
  should_find("John") { @john }
168
168
  should_create("Jane") { |p| p.name == "Jane" }
169
169
  end
170
-
170
+
171
171
  describe "find or create by multiple properties" do
172
172
  let(:valid_finder) { :find_or_create_by_name_and_age }
173
173
  let(:invalid_finder) { :find_or_create_by_name_and_something_else }
174
-
174
+
175
175
  it_should_behave_like "a dynamic finder"
176
-
176
+
177
177
  should_find("John", 42) { @john }
178
178
  should_create("John", 1) { |p| p.name == "John" && p.age == 1 }
179
179
  end
@@ -6,36 +6,36 @@ module MongoModel
6
6
  property :name, String
7
7
  property :age, Integer
8
8
  end
9
-
9
+
10
10
  define_class(:NonUser, Document)
11
-
11
+
12
12
  describe "#find" do
13
13
  before(:each) do
14
14
  User.collection.save({ '_id' => '1', 'name' => 'Fred', :age => 45 })
15
15
  User.collection.save({ '_id' => '2', 'name' => 'Alistair', :age => 18 })
16
16
  User.collection.save({ '_id' => '3', 'name' => 'Barney', :age => 10 })
17
17
  end
18
-
18
+
19
19
  describe "by id" do
20
20
  context "document exists" do
21
21
  subject { User.find('2') }
22
-
22
+
23
23
  it "returns a User" do
24
24
  subject.should be_a(User)
25
25
  end
26
-
26
+
27
27
  it "loads the document attributes" do
28
28
  subject.id.should == '2'
29
29
  subject.name.should == 'Alistair'
30
30
  end
31
-
31
+
32
32
  it { should_not be_a_new_record }
33
-
33
+
34
34
  it "stringifies ids" do
35
35
  User.find(2).id.should == '2'
36
36
  end
37
37
  end
38
-
38
+
39
39
  context "document does not exist" do
40
40
  it "raises a DocumentNotFound exception" do
41
41
  lambda {
@@ -43,37 +43,37 @@ module MongoModel
43
43
  }.should raise_error(MongoModel::DocumentNotFound)
44
44
  end
45
45
  end
46
-
46
+
47
47
  context "no id specified" do
48
48
  it "raises an ArgumentError" do
49
- lambda {
49
+ lambda {
50
50
  User.find
51
51
  }.should raise_error(ArgumentError)
52
52
  end
53
53
  end
54
54
  end
55
-
55
+
56
56
  describe "by multiple ids" do
57
57
  context "all documents exist" do
58
58
  subject { User.find('1', '2') }
59
-
59
+
60
60
  it "returns an array of Users" do
61
61
  subject[0].should be_a(User)
62
62
  subject[1].should be_a(User)
63
63
  end
64
-
64
+
65
65
  it "loads document attributes" do
66
66
  subject[0].name.should == 'Fred'
67
67
  subject[1].name.should == 'Alistair'
68
68
  end
69
-
69
+
70
70
  it "loads documents in correct order" do
71
71
  result = User.find('2', '1')
72
72
  result[0].id.should == '2'
73
73
  result[1].id.should == '1'
74
74
  end
75
75
  end
76
-
76
+
77
77
  context "some documents missing" do
78
78
  it "raises a DocumentNotFound exception" do
79
79
  lambda {