mongoid_listable 0.1.3 → 0.2.1
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 +8 -8
- data/lib/mongoid/listable.rb +2 -1
- data/lib/mongoid/listable/accessors.rb +1 -0
- data/lib/mongoid/listable/callbacks.rb +22 -27
- data/lib/mongoid/listable/macros.rb +9 -9
- data/lib/mongoid/listable/version.rb +1 -1
- data/spec/models/article.rb +10 -0
- data/spec/models/item.rb +8 -0
- data/spec/models/photo.rb +0 -2
- data/spec/models/section.rb +7 -0
- data/spec/mongoid_listable_spec.rb +258 -106
- data/spec/spec_helper.rb +4 -0
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
N2IzMjdhOTU3NzdkZTA2OWVkOTkzYTkzNjA2NmEwYmM3Y2MyN2E1OQ==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
Mzc3MGVmOWRjNmQ4Yzc3YmI0YmE3Y2I2ODU5MzcxNDliMDE2N2NiZA==
|
|
7
7
|
!binary "U0hBNTEy":
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
NjgxM2UzY2RiMDMyYWQ5ZTIzMTllZTQ3NmU4MjIzYzdlNDUwZDU2ZjJkNzFm
|
|
10
|
+
ODgwNmE4NTk4YzNjNWRkZWRmZjhlODU1YmIyMjkyMzAyYmY0MjZiMGMxOGY0
|
|
11
|
+
Zjk1Y2U2ZGFhMmFkYzg0MmEzODY2N2U3MDVlMWYxNGIyMGJmMjk=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
MTEzMjQyNmY1MmVkZWQ3ZWZmNjQ3ZDkzZjNkMmQ2NTg5ZTNkMGY4MGM3MGIz
|
|
14
|
+
NjI4MzBmMTczOGEzYmYxOTQyZTQzN2EyNTJhZWY4ODZjMWVhODAzOTVlMTU2
|
|
15
|
+
NDI1ZjRjOWYyZjg1YzcyMjkyNmMzMTEwOGY4YmFmMzY1NGM3OWM=
|
data/lib/mongoid/listable.rb
CHANGED
|
@@ -17,10 +17,10 @@ module Mongoid
|
|
|
17
17
|
def created name
|
|
18
18
|
callback = "#{name}_#{__method__}"
|
|
19
19
|
define_method callback do
|
|
20
|
-
position = send
|
|
20
|
+
position = send(name)
|
|
21
21
|
if position.present?
|
|
22
|
-
siblings = siblings
|
|
23
|
-
reposition siblings, name, position + 1
|
|
22
|
+
siblings = siblings name
|
|
23
|
+
reposition siblings.gte(name => position), name, position + 1
|
|
24
24
|
else
|
|
25
25
|
set name, siblings(name).count + 1
|
|
26
26
|
end
|
|
@@ -45,7 +45,6 @@ module Mongoid
|
|
|
45
45
|
define_method callback do
|
|
46
46
|
apply_change_on name if send("#{name}_changed?")
|
|
47
47
|
end
|
|
48
|
-
|
|
49
48
|
before_update callback
|
|
50
49
|
self
|
|
51
50
|
end
|
|
@@ -82,11 +81,16 @@ module Mongoid
|
|
|
82
81
|
def added name, meta
|
|
83
82
|
callback = "#{name.to_s.singularize}_added"
|
|
84
83
|
define_method callback do |object|
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
return unless object.new_record?
|
|
85
|
+
field_name = field_name(meta)
|
|
86
|
+
if position = object.send(field_name)
|
|
87
|
+
objects = object.siblings(field_name).gte(field_name => position)
|
|
88
|
+
reposition objects, field_name, position + 1
|
|
89
|
+
else
|
|
90
|
+
object.set field_name, has_many_count(name)
|
|
87
91
|
end
|
|
88
92
|
end
|
|
89
|
-
meta[:
|
|
93
|
+
meta[:after_add] = callback
|
|
90
94
|
self
|
|
91
95
|
end
|
|
92
96
|
|
|
@@ -109,7 +113,7 @@ module Mongoid
|
|
|
109
113
|
object.unset field_name
|
|
110
114
|
end
|
|
111
115
|
|
|
112
|
-
meta[:
|
|
116
|
+
meta[:after_remove] = callback
|
|
113
117
|
self
|
|
114
118
|
end
|
|
115
119
|
|
|
@@ -140,28 +144,19 @@ module Mongoid
|
|
|
140
144
|
# @return [Array] [from, to]
|
|
141
145
|
#
|
|
142
146
|
# @since 0.1.0
|
|
143
|
-
def change_on
|
|
144
|
-
from, to = send "#{
|
|
145
|
-
|
|
147
|
+
def change_on name
|
|
148
|
+
from, to = send "#{name}_change"
|
|
149
|
+
from ||= 0
|
|
150
|
+
to = if to > siblings(name).count + 1
|
|
151
|
+
siblings(name).count + 1
|
|
152
|
+
elsif to < 1
|
|
153
|
+
1
|
|
154
|
+
else
|
|
155
|
+
to
|
|
156
|
+
end
|
|
146
157
|
[from, to]
|
|
147
158
|
end
|
|
148
159
|
|
|
149
|
-
# Ensures the 'to' value is within acceptable bounds
|
|
150
|
-
#
|
|
151
|
-
# @param [ Integer ] to The supplied position value
|
|
152
|
-
# @return [ Integer ] The acceptable position value
|
|
153
|
-
#
|
|
154
|
-
# @since 0.1.0
|
|
155
|
-
def safe_to to
|
|
156
|
-
if to > self.class.count
|
|
157
|
-
self.class.count
|
|
158
|
-
elsif to < 1
|
|
159
|
-
1
|
|
160
|
-
else
|
|
161
|
-
to
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
|
|
165
160
|
end
|
|
166
161
|
end
|
|
167
162
|
end
|
|
@@ -15,17 +15,14 @@ module Mongoid
|
|
|
15
15
|
# @return self
|
|
16
16
|
#
|
|
17
17
|
# @since 0.1.0
|
|
18
|
-
def listed
|
|
19
|
-
|
|
20
|
-
configuration.merge! options if options.is_a?(Hash)
|
|
18
|
+
def listed
|
|
19
|
+
field :position, type: Integer
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
created(:position)
|
|
22
|
+
.updated(:position)
|
|
23
|
+
.destroyed(:position)
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
created(field_name).updated(field_name).destroyed(field_name)
|
|
27
|
-
|
|
28
|
-
scope :list, order_by(field_name => :asc)
|
|
25
|
+
scope :list, order_by(:position => :asc)
|
|
29
26
|
self
|
|
30
27
|
end
|
|
31
28
|
|
|
@@ -39,11 +36,14 @@ module Mongoid
|
|
|
39
36
|
# @since 0.0.1
|
|
40
37
|
def lists name, options={}
|
|
41
38
|
meta = reflect_on_association name
|
|
39
|
+
|
|
42
40
|
field_name = field_name(meta)
|
|
43
41
|
meta.klass.send :field, field_name, type: Integer
|
|
42
|
+
|
|
44
43
|
ids_set(name, meta).set(name, meta)
|
|
45
44
|
.added(name, meta).removed(name, meta)
|
|
46
45
|
|
|
46
|
+
meta.klass.send :include, Mongoid::Listable
|
|
47
47
|
meta.klass.updated(field_name).destroyed(field_name)
|
|
48
48
|
|
|
49
49
|
meta[:order] ||= "#{field_name(meta)} asc"
|
data/spec/models/item.rb
ADDED
data/spec/models/photo.rb
CHANGED
|
@@ -10,154 +10,306 @@ describe Mongoid::Listable do
|
|
|
10
10
|
|
|
11
11
|
describe 'listed' do
|
|
12
12
|
|
|
13
|
-
before :
|
|
14
|
-
|
|
15
|
-
10.times {
|
|
13
|
+
before :each do
|
|
14
|
+
Item.destroy_all
|
|
15
|
+
10.times { Item.create! }
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
after :each do
|
|
19
|
-
ensure_order
|
|
19
|
+
ensure_order Item.list, :position
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
it '
|
|
23
|
-
|
|
24
|
-
expect(Photo.list.first.position).to eq(1)
|
|
25
|
-
expect(photo.id).to eq(Photo.list.first.id)
|
|
22
|
+
it 'should have a position field' do
|
|
23
|
+
expect(Item.fields.key?('position')).to be_true
|
|
26
24
|
end
|
|
27
25
|
|
|
28
|
-
it '
|
|
29
|
-
|
|
30
|
-
expect(Photo.list[4].position).to eq(5)
|
|
31
|
-
expect(photo.id).to eq(Photo.list[4].id)
|
|
26
|
+
it 'should have a list scope' do
|
|
27
|
+
expect(Item.scopes.key?(:list)).to be_true
|
|
32
28
|
end
|
|
33
29
|
|
|
34
|
-
it '
|
|
35
|
-
|
|
36
|
-
expect(
|
|
37
|
-
expect(photo.id).to eq(Photo.list.last.id)
|
|
30
|
+
it 'should append new object at position 1' do
|
|
31
|
+
item = Item.create position: 1
|
|
32
|
+
expect(item.position).to eq(1)
|
|
38
33
|
end
|
|
39
34
|
|
|
40
|
-
it '
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
expect(photo_id).to eq(Photo.list[3].id)
|
|
44
|
-
expect(Photo.list[3].position).to eq(4)
|
|
35
|
+
it 'should append new object at position 5' do
|
|
36
|
+
item = Item.create position: 5
|
|
37
|
+
expect(item.position).to eq(5)
|
|
45
38
|
end
|
|
46
39
|
|
|
47
|
-
it '
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
expect(photo_id).to eq(Photo.list[1].id)
|
|
51
|
-
expect(Photo.list[1].position).to eq(2)
|
|
40
|
+
it 'should append new object at end of list' do
|
|
41
|
+
item = Item.create
|
|
42
|
+
expect(item.position).to eq(11)
|
|
52
43
|
end
|
|
53
44
|
|
|
54
|
-
it '
|
|
55
|
-
|
|
56
|
-
Photo.list[4].update_attribute :position, 5
|
|
57
|
-
expect(photo_id).to eq(Photo.list[4].id)
|
|
58
|
-
expect(Photo.list[4].position).to eq(5)
|
|
45
|
+
it 'should maintain order when removing object at position 1' do
|
|
46
|
+
Item.list.first.destroy
|
|
59
47
|
end
|
|
60
48
|
|
|
61
|
-
it '
|
|
62
|
-
|
|
63
|
-
expect(Photo.list.first.position).to eq(1)
|
|
49
|
+
it 'should maintain order when removing object at position 5' do
|
|
50
|
+
Item.where(position: 5).destroy
|
|
64
51
|
end
|
|
65
52
|
|
|
66
|
-
it '
|
|
67
|
-
|
|
68
|
-
expect(Photo.list.first.position).to eq(1)
|
|
69
|
-
expect(Photo.list[6].position).to eq(7)
|
|
70
|
-
expect(Photo.list.last.position).to eq(11)
|
|
53
|
+
it 'should maintain order when removing object at position 10' do
|
|
54
|
+
Item.list.last.destroy
|
|
71
55
|
end
|
|
72
56
|
|
|
73
|
-
it '
|
|
74
|
-
|
|
75
|
-
|
|
57
|
+
it 'should maintain order when moving object from position 1 to 5' do
|
|
58
|
+
item = Item.list.first
|
|
59
|
+
item.update_attribute :position, 5
|
|
60
|
+
expect(Item.list.where(position: 5).first).to eq(item)
|
|
76
61
|
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
describe 'lists' do
|
|
80
|
-
|
|
81
|
-
before :all do
|
|
82
|
-
User.destroy_all
|
|
83
|
-
Photo.destroy_all
|
|
84
62
|
|
|
85
|
-
|
|
86
|
-
|
|
63
|
+
it 'should maintain order when moving object from position 10 to 5' do
|
|
64
|
+
item = Item.list.last
|
|
65
|
+
item.update_attribute :position, 5
|
|
66
|
+
expect(Item.list.where(position: 5).first).to eq(item)
|
|
87
67
|
end
|
|
88
68
|
|
|
89
|
-
|
|
90
|
-
|
|
69
|
+
it 'should maintain order when moving object from position 2 to 6' do
|
|
70
|
+
item = Item.list.where(position: 2).first
|
|
71
|
+
item.update_attribute :position, 6
|
|
72
|
+
expect(Item.list.where(position: 6).first).to eq(item)
|
|
91
73
|
end
|
|
92
74
|
|
|
93
|
-
it '
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
expect(photo.id).to eq(User.first.photos[index].id)
|
|
98
|
-
end
|
|
99
|
-
expect(User.first.photos.count).to eq(Photo.count)
|
|
75
|
+
it 'should maintain order when moving object from position 8 to 4' do
|
|
76
|
+
item = Item.list.where(position: 8).first
|
|
77
|
+
item.update_attribute :position, 4
|
|
78
|
+
expect(Item.list.where(position: 4).first).to eq(item)
|
|
100
79
|
end
|
|
101
80
|
|
|
102
|
-
it '
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
expect(id).to eq(User.first.photos[index].id)
|
|
107
|
-
end
|
|
108
|
-
expect(User.first.photos.count).to eq(6)
|
|
81
|
+
it 'should do nothing when assigning object to same position' do
|
|
82
|
+
item = Item.list.where(position: 5).first
|
|
83
|
+
item.update_attribute :position, 5
|
|
84
|
+
expect(Item.list.where(position: 5).first).to eq(item)
|
|
109
85
|
end
|
|
110
86
|
|
|
111
|
-
it '
|
|
112
|
-
|
|
113
|
-
|
|
87
|
+
it 'should compensate for updated positions that are higher than bounds' do
|
|
88
|
+
item = Item.list.where(position: 5).first
|
|
89
|
+
item.update_attribute :position, 100
|
|
90
|
+
expect(Item.list.last).to eq(item)
|
|
114
91
|
end
|
|
115
92
|
|
|
116
|
-
it '
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
expect(
|
|
120
|
-
expect(User.first.photos[5].user_position).to eq(6)
|
|
93
|
+
it 'should compensate for updated positions that are lower than bounds' do
|
|
94
|
+
item = Item.list.where(position: 5).first
|
|
95
|
+
item.update_attribute :position, -100
|
|
96
|
+
expect(Item.list.first).to eq(item)
|
|
121
97
|
end
|
|
122
98
|
|
|
123
|
-
|
|
124
|
-
photo_id = User.first.photos[4].id
|
|
125
|
-
Photo.find(photo_id).update_attribute :user_position, 1
|
|
126
|
-
expect(photo_id).to eq(User.first.photos[0].id)
|
|
127
|
-
expect(User.first.photos[0].user_position).to eq(1)
|
|
128
|
-
end
|
|
99
|
+
end
|
|
129
100
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
expect(photo_id).to eq(User.first.photos[4].id)
|
|
134
|
-
expect(User.first.photos[4].user_position).to eq(5)
|
|
135
|
-
end
|
|
101
|
+
describe 'lists' do
|
|
102
|
+
|
|
103
|
+
describe 'embedded' do
|
|
136
104
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
end
|
|
105
|
+
before :each do
|
|
106
|
+
Article.destroy_all
|
|
107
|
+
Article.create!
|
|
108
|
+
10.times { Article.first.sections.create! }
|
|
109
|
+
end
|
|
143
110
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
111
|
+
after :each do
|
|
112
|
+
ensure_order Article.first.sections, :article_position
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'should have a position field' do
|
|
116
|
+
expect(Section.fields.key?('article_position')).to be_true
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it 'should append new objects with the default setter' do
|
|
120
|
+
sections = 10.times.collect { Section.new }
|
|
121
|
+
sections.reverse!
|
|
122
|
+
Article.first.sections = sections
|
|
123
|
+
|
|
124
|
+
Article.first.sections.each_with_index do |section, index|
|
|
125
|
+
expect(section.id).to eq(sections[index].id)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it 'should append new object at position 1' do
|
|
130
|
+
section = Article.first.sections.create article_position: 1
|
|
131
|
+
expect(section.article_position).to eq(1)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
it 'should append new object at position 5' do
|
|
135
|
+
section = Article.first.sections.create article_position: 5
|
|
136
|
+
expect(section.article_position).to eq(5)
|
|
137
|
+
end
|
|
150
138
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
139
|
+
it 'should append new object at end of list' do
|
|
140
|
+
section = Article.first.sections.create!
|
|
141
|
+
expect(section.article_position).to eq(11)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it 'should maintain order when removing object at position 1' do
|
|
145
|
+
Article.first.sections.first.destroy
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it 'should maintain order when removing object at position 5' do
|
|
149
|
+
Article.first.sections.where(article_position: 5).first.destroy
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it 'should maintain order when removing object at position 10' do
|
|
153
|
+
Article.first.sections.where(article_position: 10).first.destroy
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it 'should maintain order when moving object from position 1 to 5' do
|
|
157
|
+
section = Article.first.sections.where(article_position: 1).first
|
|
158
|
+
section.update_attribute :article_position, 5
|
|
159
|
+
expect(Article.first.sections.where(article_position: 5).first).to eq(section)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it 'should maintain order when moving object from position 10 to 5' do
|
|
163
|
+
section = Article.first.sections.where(article_position: 10).first
|
|
164
|
+
section.update_attribute :article_position, 5
|
|
165
|
+
expect(Article.first.sections.where(article_position: 5).first).to eq(section)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it 'should maintain order when moving object from position 2 to 6' do
|
|
169
|
+
section = Article.first.sections.where(article_position: 2).first
|
|
170
|
+
section.update_attribute :article_position, 6
|
|
171
|
+
expect(Article.first.sections.where(article_position: 6).first).to eq(section)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
it 'should maintain order when moving object from position 8 to 4' do
|
|
175
|
+
section = Article.first.sections.where(article_position: 8).first
|
|
176
|
+
section.update_attribute :article_position, 4
|
|
177
|
+
expect(Article.first.sections.where(article_position: 4).first).to eq(section)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it 'should do nothing when assigning object to same position' do
|
|
181
|
+
section = Article.first.sections.where(article_position: 5).first
|
|
182
|
+
section.update_attribute :article_position, 5
|
|
183
|
+
expect(Article.first.sections.where(article_position: 5).first).to eq(section)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it 'should compensate for updated positions that are higher than bounds' do
|
|
187
|
+
section = Article.first.sections.where(article_position: 5).first
|
|
188
|
+
section.update_attribute :article_position, 100
|
|
189
|
+
expect(Article.first.sections.last).to eq(section)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it 'should compensate for updated positions that are lower than bounds' do
|
|
193
|
+
section = Article.first.sections.where(article_position: 5).first
|
|
194
|
+
section.update_attribute :article_position, -100
|
|
195
|
+
expect(Article.first.sections.first).to eq(section)
|
|
196
|
+
end
|
|
197
|
+
|
|
154
198
|
end
|
|
155
199
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
200
|
+
describe 'referenced' do
|
|
201
|
+
|
|
202
|
+
before :each do
|
|
203
|
+
User.destroy_all
|
|
204
|
+
Photo.destroy_all
|
|
205
|
+
User.create!
|
|
206
|
+
10.times { User.first.photos.create! }
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
after :each do
|
|
210
|
+
ensure_order User.first.photos, :user_position
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
it 'should have a position field' do
|
|
214
|
+
expect(Photo.fields.key?('user_position')).to be_true
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
it 'should append new object at position 1' do
|
|
218
|
+
photo = User.first.photos.create user_position: 1
|
|
219
|
+
expect(photo.user_position).to eq(1)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
it 'should append new object at position 5' do
|
|
223
|
+
photo = User.first.photos.create user_position: 5
|
|
224
|
+
expect(photo.user_position).to eq(5)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
it 'should append new object at end of list' do
|
|
228
|
+
user = User.first.photos.create!
|
|
229
|
+
expect(user.user_position).to eq(11)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
it 'should add new objects with the default setter' do
|
|
233
|
+
photos = 15.times.collect { Photo.create }
|
|
234
|
+
|
|
235
|
+
photos.reverse!
|
|
236
|
+
|
|
237
|
+
User.first.photos = photos
|
|
238
|
+
|
|
239
|
+
expect(User.first.photos.count).to eq(15)
|
|
240
|
+
|
|
241
|
+
User.first.photos.each_with_index do |photo, index|
|
|
242
|
+
expect(photos[index].id).to eq(photo.id)
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
it 'should add new objects with the default ids setter' do
|
|
247
|
+
ids = 15.times.collect { Photo.create.id }
|
|
248
|
+
|
|
249
|
+
User.first.photo_ids = ids
|
|
250
|
+
expect(User.first.photos.count).to eq(15)
|
|
251
|
+
|
|
252
|
+
User.first.photos.each_with_index do |photo, index|
|
|
253
|
+
expect(ids[index]).to eq(User.first.photos[index].id)
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
it 'should maintain order when removing object at position 1' do
|
|
258
|
+
User.first.photos.first.destroy
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
it 'should maintain order when removing object at position 5' do
|
|
262
|
+
User.first.photos.where(user_position: 5).first.destroy
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
it 'should maintain order when removing object at position 10' do
|
|
266
|
+
User.first.photos.where(user_position: 10).first.destroy
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
it 'should maintain order when moving object from position 1 to 5' do
|
|
270
|
+
photo = User.first.photos.where(user_position: 1).first
|
|
271
|
+
photo.update_attribute :user_position, 5
|
|
272
|
+
expect(User.first.photos.where(user_position: 5).first).to eq(photo)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
it 'should maintain order when moving object from position 10 to 5' do
|
|
276
|
+
photo = User.first.photos.where(user_position: 10).first
|
|
277
|
+
photo.update_attribute :user_position, 5
|
|
278
|
+
expect(User.first.photos.where(user_position: 5).first).to eq(photo)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
it 'should maintain order when moving object from position 2 to 6' do
|
|
282
|
+
photo = User.first.photos.where(user_position: 2).first
|
|
283
|
+
photo.update_attribute :user_position, 6
|
|
284
|
+
expect(User.first.photos.where(user_position: 6).first).to eq(photo)
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
it 'should maintain order when moving object from position 8 to 4' do
|
|
288
|
+
photo = User.first.photos.where(user_position: 8).first
|
|
289
|
+
photo.update_attribute :user_position, 4
|
|
290
|
+
expect(User.first.photos.where(user_position: 4).first).to eq(photo)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
it 'should do nothing when assigning object to same position' do
|
|
294
|
+
photo = User.first.photos.where(user_position: 5).first
|
|
295
|
+
photo.update_attribute :user_position, 5
|
|
296
|
+
expect(User.first.photos.where(user_position: 5).first).to eq(photo)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
it 'should compensate for updated positions that are higher than bounds' do
|
|
300
|
+
photo = User.first.photos.where(user_position: 5).first
|
|
301
|
+
photo.update_attribute :user_position, 100
|
|
302
|
+
expect(User.first.photos.last).to eq(photo)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
it 'should compensate for updated positions that are lower than bounds' do
|
|
306
|
+
photo = User.first.photos.where(user_position: 5).first
|
|
307
|
+
photo.update_attribute :user_position, -100
|
|
308
|
+
expect(User.first.photos.first).to eq(photo)
|
|
309
|
+
end
|
|
310
|
+
|
|
160
311
|
end
|
|
312
|
+
|
|
161
313
|
end
|
|
162
314
|
|
|
163
315
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -7,8 +7,12 @@ if ENV['CI']
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
require File.expand_path '../../lib/mongoid_listable', __FILE__
|
|
10
|
+
require File.expand_path '../../spec/models/item', __FILE__
|
|
10
11
|
require File.expand_path '../../spec/models/photo', __FILE__
|
|
11
12
|
require File.expand_path '../../spec/models/user', __FILE__
|
|
13
|
+
require File.expand_path '../../spec/models/section', __FILE__
|
|
14
|
+
require File.expand_path '../../spec/models/article', __FILE__
|
|
15
|
+
|
|
12
16
|
|
|
13
17
|
Mongoid.configure do |config|
|
|
14
18
|
config.connect_to 'mongoid_listable_test'
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mongoid_listable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- richardcalahan
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-11-
|
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: mongoid
|
|
@@ -77,7 +77,10 @@ files:
|
|
|
77
77
|
- lib/mongoid/listable/version.rb
|
|
78
78
|
- lib/mongoid_listable.rb
|
|
79
79
|
- mongoid_listable.gemspec
|
|
80
|
+
- spec/models/article.rb
|
|
81
|
+
- spec/models/item.rb
|
|
80
82
|
- spec/models/photo.rb
|
|
83
|
+
- spec/models/section.rb
|
|
81
84
|
- spec/models/user.rb
|
|
82
85
|
- spec/mongoid/listable/extensions/object_spec.rb
|
|
83
86
|
- spec/mongoid_listable_spec.rb
|
|
@@ -107,7 +110,10 @@ signing_key:
|
|
|
107
110
|
specification_version: 4
|
|
108
111
|
summary: ''
|
|
109
112
|
test_files:
|
|
113
|
+
- spec/models/article.rb
|
|
114
|
+
- spec/models/item.rb
|
|
110
115
|
- spec/models/photo.rb
|
|
116
|
+
- spec/models/section.rb
|
|
111
117
|
- spec/models/user.rb
|
|
112
118
|
- spec/mongoid/listable/extensions/object_spec.rb
|
|
113
119
|
- spec/mongoid_listable_spec.rb
|