mongoid_max_denormalize 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -50,6 +50,8 @@ module Mongoid
50
50
  obj.denormalize_from_#{relation}(true)
51
51
  obj.save!
52
52
  end
53
+
54
+ nil
53
55
  end
54
56
  EOM
55
57
  klass.class_eval callback_code
@@ -102,7 +104,7 @@ EOM
102
104
  end
103
105
 
104
106
  yield if block_given?
105
- return if changed_fields.empty?
107
+ return if (changed_fields.empty? && !fields.empty?)
106
108
 
107
109
  to_update = { "$set" => {}, "$inc" => {}, "$push" => {} }
108
110
  to_get = {}
@@ -60,6 +60,8 @@ EOM
60
60
  each do |obj|
61
61
  obj.denormalize_to_#{inverse_relation}(true)
62
62
  end
63
+
64
+ nil
63
65
  end
64
66
 
65
67
  around_destroy :denormalize_to_#{inverse_relation}_destroy
@@ -4,7 +4,7 @@ module Mongoid
4
4
  module Denormalize
5
5
  module Version
6
6
 
7
- STRING = '0.0.3'
7
+ STRING = '0.0.4'
8
8
 
9
9
  end
10
10
  end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ #
5
+ # The case
6
+ #
7
+ # This is to demonstrate when we denormalize a Many to One
8
+ # with no fields and only an option :count => true
9
+ #
10
+ class Inhabitant
11
+ include Mongoid::Document
12
+
13
+ belongs_to :city
14
+ end
15
+
16
+ class City
17
+ include Mongoid::Document
18
+ include Mongoid::Max::Denormalize
19
+
20
+ has_many :inhabitants
21
+
22
+ denormalize :inhabitants, count: true
23
+ end
24
+
25
+
26
+ #
27
+ # The specs
28
+ #
29
+ describe "Case: a city and his inhabitants" do
30
+
31
+ before do
32
+ @city = City.create!
33
+ end
34
+
35
+ context "when nothing" do
36
+ context "considering the city" do
37
+ subject { @city }
38
+
39
+ it "should not have inhabitants" do
40
+ @city.inhabitants.should be_empty
41
+ @city.inhabitants_count.should eq 0
42
+ end
43
+ end
44
+ end
45
+
46
+ context "when adding 20 inhabitants" do
47
+ before do
48
+ 5.times do
49
+ @city.inhabitants.create!
50
+ end
51
+ @city.reload
52
+ end
53
+
54
+ context "considering the city" do
55
+ subject { @city }
56
+
57
+ its(:inhabitants) { should have(5).inhabitants }
58
+ its(:inhabitants_count) { should eq 5 }
59
+
60
+ context "when destroying 2 inhabitants" do
61
+ before do
62
+ 2.times do
63
+ Inhabitant.first.destroy
64
+ end
65
+ @city.reload
66
+ end
67
+
68
+ its(:inhabitants) { should have(3).inhabitants }
69
+ its(:inhabitants_count) { should eq 3 }
70
+ end
71
+
72
+ context "when destroying all inhabitants" do
73
+ before do
74
+ Inhabitant.destroy_all
75
+ @city.reload
76
+ end
77
+
78
+ its(:inhabitants) { should have(0).inhabitant }
79
+ its(:inhabitants_count) { should eq 0 }
80
+ end
81
+ end
82
+ end
83
+
84
+ end
85
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_max_denormalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -94,6 +94,7 @@ files:
94
94
  - lib/mongoid/max/denormalize/base.rb
95
95
  - lib/mongoid/max/denormalize.rb
96
96
  - spec/lib/mongoid_max_denormalize_spec.rb
97
+ - spec/cases/city_and_inhabitants_spec.rb
97
98
  - spec/cases/song_and_ratings_spec.rb
98
99
  - spec/cases/existing_models_spec.rb
99
100
  - spec/cases/contact_and_addresses_spec.rb
@@ -125,6 +126,7 @@ specification_version: 3
125
126
  summary: MaxMapper, polyvalent ORM for Rails
126
127
  test_files:
127
128
  - spec/lib/mongoid_max_denormalize_spec.rb
129
+ - spec/cases/city_and_inhabitants_spec.rb
128
130
  - spec/cases/song_and_ratings_spec.rb
129
131
  - spec/cases/existing_models_spec.rb
130
132
  - spec/cases/contact_and_addresses_spec.rb