mongoid_embedded_helper 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/embedded_helper.rb +23 -10
- data/mongoid_embedded_helper.gemspec +1 -1
- data/spec/mongoid/embedded_helper_spec.rb +37 -3
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -46,39 +46,52 @@ module Mongoid::Extensions::Array
|
|
46
46
|
def adjust!(attrs = {})
|
47
47
|
attrs.each_pair do |key, value|
|
48
48
|
self.each do |doc|
|
49
|
-
doc.
|
49
|
+
doc.adjust!(attrs)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
self
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
56
|
+
|
57
|
+
module Mongoid
|
58
|
+
class Criteria
|
59
|
+
def adjust!(attrs = {})
|
60
|
+
to_a.adjust!(attrs)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
56
64
|
|
57
65
|
module Mongoid::Document
|
58
|
-
def
|
66
|
+
def present? key
|
67
|
+
respond_to? key
|
68
|
+
end
|
69
|
+
|
70
|
+
def adjust!(attrs = {})
|
59
71
|
run_callbacks(:before_update)
|
60
72
|
(attrs || {}).each_pair do |key, value|
|
61
|
-
next if !value.integer?
|
62
|
-
|
73
|
+
next if !value.integer? # only add integer values
|
74
|
+
next if !present? key # only add to properties already present!
|
75
|
+
|
63
76
|
if set_allowed?(key)
|
64
|
-
current_val = @attributes[key.to_s]
|
77
|
+
current_val = @attributes[key.to_s] || 0
|
65
78
|
|
66
79
|
if current_val.integer?
|
67
80
|
@attributes[key.to_s] = current_val + value
|
68
81
|
end
|
69
82
|
|
70
83
|
elsif write_allowed?(key)
|
71
|
-
current_val = send("#{key}")
|
84
|
+
current_val = send("#{key}") || 0
|
72
85
|
|
73
86
|
if current_val.integer?
|
74
87
|
send("#{key}=", current_val + value)
|
75
88
|
end
|
76
89
|
end
|
77
|
-
identify if id.blank?
|
78
|
-
notify
|
79
|
-
run_callbacks(:after_update)
|
80
|
-
self
|
81
90
|
end
|
91
|
+
identify if id.blank?
|
92
|
+
notify
|
93
|
+
run_callbacks(:after_update)
|
94
|
+
self
|
82
95
|
end
|
83
96
|
end
|
84
97
|
|
@@ -9,6 +9,14 @@ class Person
|
|
9
9
|
embeds_many :lists
|
10
10
|
end
|
11
11
|
|
12
|
+
class PosPerson
|
13
|
+
include Mongoid::Document
|
14
|
+
include Mongoid::EmbeddedHelper
|
15
|
+
field :pos, :type => Integer
|
16
|
+
field :name, :type => String
|
17
|
+
embeds_many :lists
|
18
|
+
end
|
19
|
+
|
12
20
|
|
13
21
|
class List
|
14
22
|
include Mongoid::Document
|
@@ -35,6 +43,7 @@ describe 'Mongoid Embedded Helper' do
|
|
35
43
|
|
36
44
|
before :each do
|
37
45
|
@person = Person.create! :name => 'Kristian'
|
46
|
+
@pos_person = PosPerson.create! :name => 'Kristian'
|
38
47
|
# person.lists = []
|
39
48
|
list = List.new :pos => 1, :name => 'My list'
|
40
49
|
(1..3).each do |counter|
|
@@ -67,10 +76,35 @@ describe 'Mongoid Embedded Helper' do
|
|
67
76
|
result = @person.lists[0].items[0].in_collection.where(:number.gt => 1).to_a
|
68
77
|
result.size.should == 2
|
69
78
|
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#adjust!' do
|
82
|
+
context 'on an array' do
|
83
|
+
it "should add 1 to all positions greater than 1" do
|
84
|
+
result = @person.lists[0].items.where(:pos.gt => 1).to_a.adjust!(:pos => 1)
|
85
|
+
result.map(&:pos).should == [3, 4]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'on a criteria' do
|
90
|
+
it "should add 1 to all positions greater than 1" do
|
91
|
+
result = @person.lists[0].items.where(:pos.gt => 1).adjust!(:pos => 1)
|
92
|
+
result.map(&:pos).should == [3, 4]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'on a document with a pos' do
|
97
|
+
it "should add 1 to the position" do
|
98
|
+
result = @pos_person.adjust!(:pos => 1)
|
99
|
+
result.pos.should == 1
|
100
|
+
end
|
101
|
+
end
|
70
102
|
|
71
|
-
|
72
|
-
|
73
|
-
|
103
|
+
context 'on a document without a pos field' do
|
104
|
+
it "should NOT add 1 to the position" do
|
105
|
+
result = @person.adjust!(:pos => 1)
|
106
|
+
lambda {result.pos}.should raise_error
|
107
|
+
end
|
74
108
|
end
|
75
109
|
end
|
76
110
|
end
|