mongoid-list 0.1.8 → 0.2.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.
- data/Guardfile +4 -8
- data/lib/mongoid/list.rb +5 -1
- data/lib/mongoid/list/collection.rb +3 -5
- data/lib/mongoid/list/embedded.rb +2 -3
- data/lib/mongoid/list/version.rb +1 -1
- data/mongoid-list.gemspec +11 -11
- data/mongoid.yml +12 -0
- data/spec/mongoid/list/collection_spec.rb +147 -0
- data/spec/mongoid/list/embedded_spec.rb +181 -0
- data/spec/mongoid/list_spec.rb +844 -0
- data/spec/spec_helper.rb +66 -0
- data/spec/support/models/container.rb +8 -0
- data/{test → spec}/support/models/embedded.rb +0 -0
- data/{test → spec}/support/models/embedded_deeply.rb +0 -0
- data/{test → spec}/support/models/scoped.rb +0 -0
- data/{test → spec}/support/models/scoped_embedded.rb +0 -0
- data/{test → spec}/support/models/simple.rb +0 -0
- metadata +47 -62
- data/test/mongoid/list/collection_test.rb +0 -141
- data/test/mongoid/list/embedded_test.rb +0 -174
- data/test/mongoid/list_test.rb +0 -869
- data/test/support/models/container.rb +0 -8
- data/test/test_helper.rb +0 -27
data/Guardfile
CHANGED
@@ -1,12 +1,8 @@
|
|
1
|
-
guard 'spork' do
|
1
|
+
guard 'spork', wait: 20 do
|
2
2
|
watch('Gemfile')
|
3
|
-
watch('Gemfile.lock')
|
4
|
-
watch('test/test_helper.rb')
|
5
3
|
end
|
6
4
|
|
7
|
-
guard
|
8
|
-
watch(
|
9
|
-
watch(%r
|
10
|
-
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
11
|
-
watch(%r|^test/test_helper\.rb|) { "test" }
|
5
|
+
guard :rspec, version: 2 do
|
6
|
+
watch('spec/spec_helper.rb') { "spec" }
|
7
|
+
watch(%r{^spec/.+_spec\.rb})
|
12
8
|
end
|
data/lib/mongoid/list.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module Mongoid
|
2
2
|
|
3
|
+
Fields.option :scope do |model, field, value|
|
4
|
+
|
5
|
+
end
|
6
|
+
|
3
7
|
module List
|
4
8
|
|
5
9
|
extend ActiveSupport::Concern
|
@@ -18,7 +22,7 @@ module Mongoid
|
|
18
22
|
before_destroy :mark_for_removal_processing_from_list
|
19
23
|
after_destroy :update_positions_in_list!, if: :_process_list_change
|
20
24
|
|
21
|
-
scope :ordered,
|
25
|
+
scope :ordered, asc(:position)
|
22
26
|
end
|
23
27
|
|
24
28
|
|
@@ -9,7 +9,7 @@ module Mongoid
|
|
9
9
|
def update_positions!(klass, elements)
|
10
10
|
elements.each_with_index do |element, idx|
|
11
11
|
id = element.kind_of?(Hash) ? element['id'] : element
|
12
|
-
klass.collection.
|
12
|
+
klass.collection.find({ _id: id }).update({ '$set' => { position: (idx + 1) } })
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -17,10 +17,8 @@ module Mongoid
|
|
17
17
|
|
18
18
|
|
19
19
|
def update_positions!
|
20
|
-
obj.class.collection.
|
21
|
-
|
22
|
-
{ '$inc' => { position: changes[:by] } },
|
23
|
-
multi: true
|
20
|
+
obj.class.collection.find(criteria).update_all(
|
21
|
+
{ '$inc' => { position: changes[:by] } }
|
24
22
|
)
|
25
23
|
end
|
26
24
|
|
@@ -8,8 +8,7 @@ module Mongoid
|
|
8
8
|
|
9
9
|
def update_positions!(binding, elements)
|
10
10
|
load_list_elements(binding, elements).each_with_index do |element, idx|
|
11
|
-
(binding.base._parent || binding.base).collection.update(
|
12
|
-
element.atomic_selector,
|
11
|
+
(binding.base._parent || binding.base).collection.find(element.atomic_selector).update(
|
13
12
|
{ "$set" => { "#{element.atomic_path}.$.position" => (idx+1) } }
|
14
13
|
)
|
15
14
|
end
|
@@ -32,7 +31,7 @@ module Mongoid
|
|
32
31
|
next unless should_operate_on_item?(item)
|
33
32
|
criteria = item.atomic_selector
|
34
33
|
updates = { '$inc' => { "#{item.atomic_path}.$.position" => changes[:by] } }
|
35
|
-
item._root.class.collection.update(
|
34
|
+
item._root.class.collection.find(criteria).update(updates)
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
data/lib/mongoid/list/version.rb
CHANGED
data/mongoid-list.gemspec
CHANGED
@@ -13,20 +13,20 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.summary = 'Simple list behavior for Mongoid'
|
14
14
|
s.homepage = 'https://github.com/davekrupinski/mongoid-list'
|
15
15
|
|
16
|
-
s.add_dependency('mongoid', [ '>= 2.4.0' ])
|
17
|
-
|
18
|
-
s.add_development_dependency('bson_ext')
|
19
|
-
s.add_development_dependency('minitest', [ '>= 2.11.0' ])
|
20
|
-
s.add_development_dependency('mini_shoulda', [ '>= 0.4.0' ])
|
21
|
-
s.add_development_dependency('spork', [ '>= 1.0.0.rc' ])
|
22
|
-
s.add_development_dependency('spork-testunit', [ '>= 0.0.8' ])
|
23
|
-
s.add_development_dependency('guard-minitest', [ '>= 0.5.0' ])
|
24
|
-
s.add_development_dependency('guard-spork', [ '>= 0.5.2' ])
|
25
|
-
s.add_development_dependency('turn', [ '>= 0.9.4' ])
|
26
|
-
|
27
16
|
s.files = `git ls-files`.split("\n")
|
28
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
29
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
30
19
|
s.require_paths = ["lib"]
|
31
20
|
|
21
|
+
s.add_dependency('mongoid', [ '>= 3.0.0' ])
|
22
|
+
|
23
|
+
s.add_development_dependency('rspec', [ '>= 2.13.0' ])
|
24
|
+
s.add_development_dependency('guard', [ '>= 1.8.0' ])
|
25
|
+
s.add_development_dependency('guard-rspec', [ '>= 2.5.0' ])
|
26
|
+
s.add_development_dependency('guard-spork', [ '>= 1.5.0' ])
|
27
|
+
s.add_development_dependency('listen', [ '>= 1.0.0' ])
|
28
|
+
s.add_development_dependency('database_cleaner', [ '~> 0.9.0 ' ])
|
29
|
+
|
30
|
+
s.add_development_dependency('rspecify')
|
31
|
+
|
32
32
|
end
|
data/mongoid.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
test:
|
2
|
+
sessions:
|
3
|
+
default:
|
4
|
+
database: mongoid-list_test
|
5
|
+
hosts:
|
6
|
+
- localhost:27017
|
7
|
+
options:
|
8
|
+
consistency: :strong
|
9
|
+
# In the test environment we lower the retries and retry interval to
|
10
|
+
# low amounts for fast failures.
|
11
|
+
max_retries: 1
|
12
|
+
retry_interval: 0
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::List::Collection do
|
4
|
+
|
5
|
+
describe "#initialization" do
|
6
|
+
|
7
|
+
let(:simple) do
|
8
|
+
Simple.create
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:collection) do
|
12
|
+
Mongoid::List::Collection.new(simple)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should assign simple to :obj" do
|
16
|
+
collection.obj.should eq simple
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
describe "#count" do
|
23
|
+
|
24
|
+
context "when unscoped" do
|
25
|
+
let(:collection) do
|
26
|
+
Mongoid::List::Collection.new(Simple.new)
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
5.times do
|
31
|
+
Simple.create
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be 5" do
|
36
|
+
collection.count.should eq 5
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when scoped" do
|
42
|
+
|
43
|
+
before do
|
44
|
+
4.times do
|
45
|
+
Scoped.create(group: "airplane 1")
|
46
|
+
end
|
47
|
+
|
48
|
+
3.times do
|
49
|
+
Scoped.create(group: "airplane 2")
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
context "group 1" do
|
55
|
+
|
56
|
+
let(:collection) do
|
57
|
+
Mongoid::List::Collection.new(Scoped.new(group: "airplane 1"))
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should be 4" do
|
61
|
+
collection.count.should eq 4
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
context "group 2" do
|
67
|
+
|
68
|
+
let(:collection) do
|
69
|
+
Mongoid::List::Collection.new(Scoped.new(group: "airplane 2"))
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should be 3" do
|
73
|
+
collection.count.should eq 3
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
describe "#update_positions_in_list!" do
|
84
|
+
|
85
|
+
context "unscoped" do
|
86
|
+
|
87
|
+
let!(:obj1) { Simple.create }
|
88
|
+
let!(:obj2) { Simple.create }
|
89
|
+
let!(:obj3) { Simple.create }
|
90
|
+
|
91
|
+
before do
|
92
|
+
Simple.update_positions_in_list!([obj2.id, obj1.id, obj3.id])
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should change obj1 from :position of 1 to 2" do
|
96
|
+
obj1.position.should eq 1
|
97
|
+
obj1.reload.position.should eq 2
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should change obj2 from :position of 2 to 1" do
|
101
|
+
obj2.position.should eq 2
|
102
|
+
obj2.reload.position.should eq 1
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should not change obj3 from :position of 3" do
|
106
|
+
obj3.position.should eq 3
|
107
|
+
obj3.reload.position.should eq 3
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
context "scoped" do
|
113
|
+
|
114
|
+
let!(:obj1) { Scoped.create(group: "hell's angels") }
|
115
|
+
let!(:obj2) { Scoped.create(group: "hell's angels") }
|
116
|
+
let!(:obj3) { Scoped.create(group: "hell's angels") }
|
117
|
+
let!(:other) { Scoped.create(group: "charlie's angels") }
|
118
|
+
|
119
|
+
before do
|
120
|
+
Scoped.update_positions_in_list!([obj3.id, obj2.id, obj1.id])
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should change obj1 from :position of 1 to 3" do
|
124
|
+
obj1.position.should eq 1
|
125
|
+
obj1.reload.position.should eq 3
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should not change obj2 from :position of 2" do
|
129
|
+
obj2.position.should eq 2
|
130
|
+
obj2.reload.position.should eq 2
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should change obj3 from :position of 3 to 1" do
|
134
|
+
obj3.position.should eq 3
|
135
|
+
obj3.reload.position.should eq 1
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should not have touched other scoped" do
|
139
|
+
other.position.should eq 1
|
140
|
+
other.reload.position.should eq 1
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::List::Embedded do
|
4
|
+
|
5
|
+
describe "#initialization" do
|
6
|
+
|
7
|
+
let(:container) do
|
8
|
+
Container.create
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:item) do
|
12
|
+
container.items.create
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:embedded) do
|
16
|
+
Mongoid::List::Embedded.new(item)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should assign item to :obj" do
|
20
|
+
embedded.obj.should eq item
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
describe "#count" do
|
27
|
+
|
28
|
+
context "when unscoped" do
|
29
|
+
|
30
|
+
let(:container) do
|
31
|
+
Container.create!
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:container2) do
|
35
|
+
Container.create!
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:item) do
|
39
|
+
container.items.build
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:embedded) do
|
43
|
+
Mongoid::List::Embedded.new(item)
|
44
|
+
end
|
45
|
+
|
46
|
+
before do
|
47
|
+
3.times do
|
48
|
+
container.items.create!
|
49
|
+
end
|
50
|
+
2.times do
|
51
|
+
container2.items.create!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should be 3" do
|
56
|
+
embedded.count.should eq 3
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when scoped" do
|
62
|
+
|
63
|
+
let(:container) do
|
64
|
+
Container.create!
|
65
|
+
end
|
66
|
+
|
67
|
+
before do
|
68
|
+
3.times do
|
69
|
+
container.scoped_items.create!(group: "alien")
|
70
|
+
end
|
71
|
+
2.times do
|
72
|
+
container.scoped_items.create!(group: "aliens")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "group 1" do
|
77
|
+
|
78
|
+
let(:item) do
|
79
|
+
container.scoped_items.build(group: "alien")
|
80
|
+
end
|
81
|
+
|
82
|
+
let(:embedded) do
|
83
|
+
Mongoid::List::Embedded.new(item)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be 3" do
|
87
|
+
embedded.count.should eq 3
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
context "group 2" do
|
93
|
+
|
94
|
+
let(:item) do
|
95
|
+
container.scoped_items.build(group: "aliens")
|
96
|
+
end
|
97
|
+
|
98
|
+
let(:embedded) do
|
99
|
+
Mongoid::List::Embedded.new(item)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should be 2" do
|
103
|
+
embedded.count.should eq 2
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
describe "#update_positions_in_list!" do
|
114
|
+
|
115
|
+
let(:container) do
|
116
|
+
Container.create!
|
117
|
+
end
|
118
|
+
|
119
|
+
context "unscoped" do
|
120
|
+
|
121
|
+
let!(:obj1) { container.items.create! }
|
122
|
+
let!(:obj2) { container.items.create! }
|
123
|
+
let!(:obj3) { container.items.create! }
|
124
|
+
|
125
|
+
before do
|
126
|
+
container.items.update_positions_in_list!([obj2.id, obj1.id, obj3.id])
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should change obj1 from :position of 1 to 2" do
|
130
|
+
obj1.position.should eq 1
|
131
|
+
obj1.reload.position.should eq 2
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should change obj2 from :position of 2 to 1" do
|
135
|
+
obj2.position.should eq 2
|
136
|
+
obj2.reload.position.should eq 1
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should not change obj3 from :position of 3" do
|
140
|
+
obj3.position.should eq 3
|
141
|
+
obj3.reload.position.should eq 3
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
context "scoped" do
|
147
|
+
|
148
|
+
let!(:obj1) { container.scoped_items.create!(group: "hell's angels") }
|
149
|
+
let!(:obj2) { container.scoped_items.create!(group: "hell's angels") }
|
150
|
+
let!(:obj3) { container.scoped_items.create!(group: "hell's angels") }
|
151
|
+
let!(:other) { container.scoped_items.create!(group: "charlie's angels") }
|
152
|
+
|
153
|
+
before do
|
154
|
+
container.scoped_items.update_positions_in_list!([obj3.id, obj2.id, obj1.id])
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should change obj1 from :position of 1 to 3" do
|
158
|
+
obj1.position.should eq 1
|
159
|
+
obj1.reload.position.should eq 3
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should not change obj2 from :position of 2" do
|
163
|
+
obj2.position.should eq 2
|
164
|
+
obj2.reload.position.should eq 2
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should change obj3 from :position of 3 to 1" do
|
168
|
+
obj3.position.should eq 3
|
169
|
+
obj3.reload.position.should eq 1
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should not have touched other scoped" do
|
173
|
+
other.position.should eq 1
|
174
|
+
other.reload.position.should eq 1
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|