mongoid_paranoia 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +117 -117
- data/lib/mongoid/paranoia/configuration.rb +13 -11
- data/lib/mongoid/paranoia/monkey_patches.rb +114 -113
- data/lib/mongoid/paranoia/version.rb +7 -5
- data/lib/mongoid/paranoia.rb +196 -203
- data/lib/mongoid-paranoia.rb +3 -1
- data/lib/mongoid_paranoia.rb +3 -1
- data/perf/scope.rb +65 -64
- data/spec/app/models/address.rb +71 -71
- data/spec/app/models/appointment.rb +7 -7
- data/spec/app/models/author.rb +6 -6
- data/spec/app/models/fish.rb +8 -8
- data/spec/app/models/paranoid_phone.rb +25 -25
- data/spec/app/models/paranoid_post.rb +65 -65
- data/spec/app/models/person.rb +21 -21
- data/spec/app/models/phone.rb +11 -11
- data/spec/app/models/relations.rb +247 -247
- data/spec/app/models/tag.rb +6 -6
- data/spec/app/models/title.rb +4 -4
- data/spec/mongoid/configuration_spec.rb +19 -19
- data/spec/mongoid/document_spec.rb +21 -21
- data/spec/mongoid/nested_attributes_spec.rb +164 -164
- data/spec/mongoid/paranoia_spec.rb +887 -887
- data/spec/mongoid/scoping_spec.rb +55 -55
- data/spec/mongoid/validatable/uniqueness_spec.rb +74 -74
- data/spec/spec_helper.rb +43 -75
- metadata +36 -25
@@ -1,164 +1,164 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Attributes::Nested do
|
4
|
-
describe "##{name}_attributes=" do
|
5
|
-
context "when the parent document is new" do
|
6
|
-
context "when the relation is an embeds many" do
|
7
|
-
context "when ids are passed" do
|
8
|
-
|
9
|
-
let(:person) do
|
10
|
-
Person.create
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:address_one) do
|
14
|
-
Address.new(street: "Unter den Linden")
|
15
|
-
end
|
16
|
-
|
17
|
-
let(:address_two) do
|
18
|
-
Address.new(street: "Kurfeurstendamm")
|
19
|
-
end
|
20
|
-
|
21
|
-
let(:phone_one) do
|
22
|
-
ParanoidPhone.new(number: "1")
|
23
|
-
end
|
24
|
-
|
25
|
-
let(:phone_two) do
|
26
|
-
ParanoidPhone.new(number: "2")
|
27
|
-
end
|
28
|
-
|
29
|
-
before do
|
30
|
-
person.addresses << [ address_one, address_two ]
|
31
|
-
end
|
32
|
-
|
33
|
-
context "when destroy attributes are passed" do
|
34
|
-
context "when the ids match" do
|
35
|
-
context "when allow_destroy is true" do
|
36
|
-
context "when the child is paranoid" do
|
37
|
-
|
38
|
-
before(:all) do
|
39
|
-
Person.send(:undef_method, :paranoid_phones_attributes=)
|
40
|
-
Person.accepts_nested_attributes_for :paranoid_phones,
|
41
|
-
allow_destroy: true
|
42
|
-
end
|
43
|
-
|
44
|
-
after(:all) do
|
45
|
-
Person.send(:undef_method, :paranoid_phones_attributes=)
|
46
|
-
Person.accepts_nested_attributes_for :paranoid_phones
|
47
|
-
end
|
48
|
-
|
49
|
-
[ 1, "1", true, "true" ].each do |truth|
|
50
|
-
|
51
|
-
context "when passed a #{truth} with destroy" do
|
52
|
-
context "when the parent is persisted" do
|
53
|
-
|
54
|
-
let!(:persisted) do
|
55
|
-
Person.create do |p|
|
56
|
-
p.paranoid_phones << [ phone_one, phone_two ]
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context "when setting, pulling, and pushing in one op" do
|
61
|
-
|
62
|
-
before do
|
63
|
-
persisted.paranoid_phones_attributes =
|
64
|
-
{
|
65
|
-
"bar" => { "id" => phone_one.id, "_destroy" => truth },
|
66
|
-
"foo" => { "id" => phone_two.id, "number" => "3" },
|
67
|
-
"baz" => { "number" => "4" }
|
68
|
-
}
|
69
|
-
end
|
70
|
-
|
71
|
-
it "removes the first document from the relation" do
|
72
|
-
expect(persisted.paranoid_phones.size).to eq(2)
|
73
|
-
end
|
74
|
-
|
75
|
-
it "does not delete the unmarked document" do
|
76
|
-
expect(persisted.paranoid_phones.first.number).to eq("3")
|
77
|
-
end
|
78
|
-
|
79
|
-
it "adds the new document to the relation" do
|
80
|
-
expect(persisted.paranoid_phones.last.number).to eq("4")
|
81
|
-
end
|
82
|
-
|
83
|
-
it "has the proper persisted count" do
|
84
|
-
expect(persisted.paranoid_phones.count).to eq(1)
|
85
|
-
end
|
86
|
-
|
87
|
-
it "soft deletes the removed document" do
|
88
|
-
expect(phone_one).to be_destroyed
|
89
|
-
end
|
90
|
-
|
91
|
-
context "when saving the parent" do
|
92
|
-
|
93
|
-
before do
|
94
|
-
persisted.save
|
95
|
-
end
|
96
|
-
|
97
|
-
it "deletes the marked document from the relation" do
|
98
|
-
expect(persisted.reload.paranoid_phones.count).to eq(2)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "does not delete the unmarked document" do
|
102
|
-
expect(persisted.reload.paranoid_phones.first.number).to eq("3")
|
103
|
-
end
|
104
|
-
|
105
|
-
it "persists the new document to the relation" do
|
106
|
-
expect(persisted.reload.paranoid_phones.last.number).to eq("4")
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "when the child has defaults" do
|
116
|
-
|
117
|
-
before(:all) do
|
118
|
-
Person.accepts_nested_attributes_for :appointments, allow_destroy: true
|
119
|
-
end
|
120
|
-
|
121
|
-
after(:all) do
|
122
|
-
Person.send(:undef_method, :appointments_attributes=)
|
123
|
-
end
|
124
|
-
context "when the parent is persisted" do
|
125
|
-
context "when the child returns false in a before callback" do
|
126
|
-
context "when the child is paranoid" do
|
127
|
-
|
128
|
-
before(:all) do
|
129
|
-
Person.accepts_nested_attributes_for :paranoid_phones, allow_destroy: true
|
130
|
-
end
|
131
|
-
|
132
|
-
after(:all) do
|
133
|
-
Person.send(:undef_method, :paranoid_phones=)
|
134
|
-
Person.accepts_nested_attributes_for :paranoid_phones
|
135
|
-
end
|
136
|
-
|
137
|
-
let!(:persisted) do
|
138
|
-
Person.create(age: 42)
|
139
|
-
end
|
140
|
-
|
141
|
-
let!(:phone) do
|
142
|
-
persisted.paranoid_phones.create
|
143
|
-
end
|
144
|
-
|
145
|
-
before do
|
146
|
-
persisted.paranoid_phones_attributes =
|
147
|
-
{ "foo" => { "id" => phone.id, "number" => 42, "_destroy" => true }}
|
148
|
-
end
|
149
|
-
|
150
|
-
it "does not destroy the child" do
|
151
|
-
expect(persisted.reload.paranoid_phones).not_to be_empty
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Attributes::Nested do
|
4
|
+
describe "##{name}_attributes=" do
|
5
|
+
context "when the parent document is new" do
|
6
|
+
context "when the relation is an embeds many" do
|
7
|
+
context "when ids are passed" do
|
8
|
+
|
9
|
+
let(:person) do
|
10
|
+
Person.create
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:address_one) do
|
14
|
+
Address.new(street: "Unter den Linden")
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:address_two) do
|
18
|
+
Address.new(street: "Kurfeurstendamm")
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:phone_one) do
|
22
|
+
ParanoidPhone.new(number: "1")
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:phone_two) do
|
26
|
+
ParanoidPhone.new(number: "2")
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
person.addresses << [ address_one, address_two ]
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when destroy attributes are passed" do
|
34
|
+
context "when the ids match" do
|
35
|
+
context "when allow_destroy is true" do
|
36
|
+
context "when the child is paranoid" do
|
37
|
+
|
38
|
+
before(:all) do
|
39
|
+
Person.send(:undef_method, :paranoid_phones_attributes=)
|
40
|
+
Person.accepts_nested_attributes_for :paranoid_phones,
|
41
|
+
allow_destroy: true
|
42
|
+
end
|
43
|
+
|
44
|
+
after(:all) do
|
45
|
+
Person.send(:undef_method, :paranoid_phones_attributes=)
|
46
|
+
Person.accepts_nested_attributes_for :paranoid_phones
|
47
|
+
end
|
48
|
+
|
49
|
+
[ 1, "1", true, "true" ].each do |truth|
|
50
|
+
|
51
|
+
context "when passed a #{truth} with destroy" do
|
52
|
+
context "when the parent is persisted" do
|
53
|
+
|
54
|
+
let!(:persisted) do
|
55
|
+
Person.create do |p|
|
56
|
+
p.paranoid_phones << [ phone_one, phone_two ]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when setting, pulling, and pushing in one op" do
|
61
|
+
|
62
|
+
before do
|
63
|
+
persisted.paranoid_phones_attributes =
|
64
|
+
{
|
65
|
+
"bar" => { "id" => phone_one.id, "_destroy" => truth },
|
66
|
+
"foo" => { "id" => phone_two.id, "number" => "3" },
|
67
|
+
"baz" => { "number" => "4" }
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
it "removes the first document from the relation" do
|
72
|
+
expect(persisted.paranoid_phones.size).to eq(2)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "does not delete the unmarked document" do
|
76
|
+
expect(persisted.paranoid_phones.first.number).to eq("3")
|
77
|
+
end
|
78
|
+
|
79
|
+
it "adds the new document to the relation" do
|
80
|
+
expect(persisted.paranoid_phones.last.number).to eq("4")
|
81
|
+
end
|
82
|
+
|
83
|
+
it "has the proper persisted count" do
|
84
|
+
expect(persisted.paranoid_phones.count).to eq(1)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "soft deletes the removed document" do
|
88
|
+
expect(phone_one).to be_destroyed
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when saving the parent" do
|
92
|
+
|
93
|
+
before do
|
94
|
+
persisted.save
|
95
|
+
end
|
96
|
+
|
97
|
+
it "deletes the marked document from the relation" do
|
98
|
+
expect(persisted.reload.paranoid_phones.count).to eq(2)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "does not delete the unmarked document" do
|
102
|
+
expect(persisted.reload.paranoid_phones.first.number).to eq("3")
|
103
|
+
end
|
104
|
+
|
105
|
+
it "persists the new document to the relation" do
|
106
|
+
expect(persisted.reload.paranoid_phones.last.number).to eq("4")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when the child has defaults" do
|
116
|
+
|
117
|
+
before(:all) do
|
118
|
+
Person.accepts_nested_attributes_for :appointments, allow_destroy: true
|
119
|
+
end
|
120
|
+
|
121
|
+
after(:all) do
|
122
|
+
Person.send(:undef_method, :appointments_attributes=)
|
123
|
+
end
|
124
|
+
context "when the parent is persisted" do
|
125
|
+
context "when the child returns false in a before callback" do
|
126
|
+
context "when the child is paranoid" do
|
127
|
+
|
128
|
+
before(:all) do
|
129
|
+
Person.accepts_nested_attributes_for :paranoid_phones, allow_destroy: true
|
130
|
+
end
|
131
|
+
|
132
|
+
after(:all) do
|
133
|
+
Person.send(:undef_method, :paranoid_phones=)
|
134
|
+
Person.accepts_nested_attributes_for :paranoid_phones
|
135
|
+
end
|
136
|
+
|
137
|
+
let!(:persisted) do
|
138
|
+
Person.create(age: 42)
|
139
|
+
end
|
140
|
+
|
141
|
+
let!(:phone) do
|
142
|
+
persisted.paranoid_phones.create
|
143
|
+
end
|
144
|
+
|
145
|
+
before do
|
146
|
+
persisted.paranoid_phones_attributes =
|
147
|
+
{ "foo" => { "id" => phone.id, "number" => 42, "_destroy" => true }}
|
148
|
+
end
|
149
|
+
|
150
|
+
it "does not destroy the child" do
|
151
|
+
expect(persisted.reload.paranoid_phones).not_to be_empty
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|