mongoid_orderable 0.9.0 → 0.9.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.
- data/lib/mongoid/orderable.rb +89 -93
- data/lib/mongoid_orderable/version.rb +1 -1
- data/spec/mongoid/orderable_spec.rb +16 -0
- metadata +7 -7
data/lib/mongoid/orderable.rb
CHANGED
@@ -13,7 +13,7 @@ module Mongoid::Orderable
|
|
13
13
|
}
|
14
14
|
|
15
15
|
configuration.update options if options.is_a?(Hash)
|
16
|
-
field configuration[:column], type
|
16
|
+
field configuration[:column], :type => Integer
|
17
17
|
index configuration[:column]
|
18
18
|
|
19
19
|
configuration[:scope] = "#{configuration[:scope]}_id".to_sym if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
|
@@ -36,128 +36,124 @@ module Mongoid::Orderable
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
def move_to! target_position
|
40
|
+
@move_to = target_position
|
41
|
+
save
|
42
|
+
end
|
43
|
+
alias_method :insert_at!, :move_to!
|
40
44
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
alias_method :insert_at!, :move_to!
|
45
|
+
def move_to target_position
|
46
|
+
@move_to = target_position
|
47
|
+
end
|
48
|
+
alias_method :insert_at, :move_to
|
46
49
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
50
|
+
def move_to= target_position
|
51
|
+
@move_to = target_position
|
52
|
+
end
|
53
|
+
alias_method :insert_at=, :move_to=
|
51
54
|
|
52
|
-
|
53
|
-
|
55
|
+
[:top, :bottom].each do |symbol|
|
56
|
+
define_method "move_to_#{symbol}" do
|
57
|
+
move_to symbol
|
54
58
|
end
|
55
|
-
alias_method :insert_at=, :move_to=
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
move_to symbol
|
60
|
-
end
|
61
|
-
|
62
|
-
define_method "move_to_#{symbol}!" do
|
63
|
-
move_to! symbol
|
64
|
-
end
|
60
|
+
define_method "move_to_#{symbol}!" do
|
61
|
+
move_to! symbol
|
65
62
|
end
|
63
|
+
end
|
66
64
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
define_method "move_#{symbol}!" do
|
73
|
-
move_to! symbol
|
74
|
-
end
|
65
|
+
[:higher, :lower].each do |symbol|
|
66
|
+
define_method "move_#{symbol}" do
|
67
|
+
move_to symbol
|
75
68
|
end
|
76
69
|
|
77
|
-
|
78
|
-
|
70
|
+
define_method "move_#{symbol}!" do
|
71
|
+
move_to! symbol
|
79
72
|
end
|
73
|
+
end
|
80
74
|
|
81
|
-
|
82
|
-
|
83
|
-
|
75
|
+
def first?
|
76
|
+
in_list? && orderable_position == 1
|
77
|
+
end
|
84
78
|
|
85
|
-
|
86
|
-
|
87
|
-
|
79
|
+
def last?
|
80
|
+
in_list? && orderable_position == bottom_orderable_position
|
81
|
+
end
|
88
82
|
|
89
|
-
|
90
|
-
|
91
|
-
|
83
|
+
def in_list?
|
84
|
+
!orderable_position.nil?
|
85
|
+
end
|
92
86
|
|
93
|
-
|
94
|
-
|
95
|
-
|
87
|
+
def add_to_list
|
88
|
+
apply_position @move_to
|
89
|
+
end
|
96
90
|
|
97
|
-
|
91
|
+
def remove_from_list
|
92
|
+
orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column => -1)
|
93
|
+
end
|
98
94
|
|
99
|
-
|
100
|
-
send orderable_column
|
101
|
-
end
|
95
|
+
private
|
102
96
|
|
103
|
-
|
104
|
-
|
105
|
-
|
97
|
+
def orderable_position
|
98
|
+
send orderable_column
|
99
|
+
end
|
106
100
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
else
|
111
|
-
self.class.orderable_scope(self)
|
112
|
-
end
|
113
|
-
end
|
101
|
+
def orderable_position= value
|
102
|
+
send "#{orderable_column}=", value
|
103
|
+
end
|
114
104
|
|
115
|
-
|
116
|
-
|
105
|
+
def orderable_scoped
|
106
|
+
if embedded?
|
107
|
+
send(metadata.inverse).send(metadata.name).orderable_scope(self)
|
108
|
+
else
|
109
|
+
self.class.orderable_scope(self)
|
117
110
|
end
|
111
|
+
end
|
118
112
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
self.orderable_position = nil
|
123
|
-
end
|
124
|
-
|
125
|
-
return if !target_position && in_list?
|
113
|
+
def orderable_scope_changed?
|
114
|
+
!orderable_scoped.where(:_id => _id).exists?
|
115
|
+
end
|
126
116
|
|
127
|
-
|
117
|
+
def apply_position target_position
|
118
|
+
if persisted? && !embedded? && orderable_scope_changed?
|
119
|
+
self.class.find(_id).remove_from_list
|
120
|
+
self.orderable_position = nil
|
121
|
+
end
|
128
122
|
|
129
|
-
|
130
|
-
orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column => 1)
|
131
|
-
else
|
132
|
-
orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column => 1) if target_position < orderable_position
|
133
|
-
orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column => -1) if target_position > orderable_position
|
134
|
-
end
|
123
|
+
return if !target_position && in_list?
|
135
124
|
|
136
|
-
|
125
|
+
target_position = target_position_to_position target_position
|
126
|
+
|
127
|
+
unless in_list?
|
128
|
+
orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column => 1)
|
129
|
+
else
|
130
|
+
orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column => 1) if target_position < orderable_position
|
131
|
+
orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column => -1) if target_position > orderable_position
|
137
132
|
end
|
138
133
|
|
139
|
-
|
140
|
-
|
134
|
+
self.orderable_position = target_position
|
135
|
+
end
|
136
|
+
|
137
|
+
def target_position_to_position target_position
|
138
|
+
target_position = :bottom unless target_position
|
141
139
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
140
|
+
target_position = case target_position.to_sym
|
141
|
+
when :top then 1
|
142
|
+
when :bottom then bottom_orderable_position
|
143
|
+
when :higher then orderable_position.pred
|
144
|
+
when :lower then orderable_position.next
|
145
|
+
end unless target_position.is_a? Numeric
|
148
146
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
147
|
+
target_position = 1 if target_position < 1
|
148
|
+
target_position = bottom_orderable_position if target_position > bottom_orderable_position
|
149
|
+
target_position
|
150
|
+
end
|
153
151
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
end
|
152
|
+
def bottom_orderable_position
|
153
|
+
@bottom_orderable_position = begin
|
154
|
+
max = orderable_scoped.max(orderable_column).to_i
|
155
|
+
in_list? ? max : max.next
|
159
156
|
end
|
160
|
-
|
161
157
|
end
|
162
158
|
|
163
159
|
end
|
@@ -36,6 +36,12 @@ describe Mongoid::Orderable do
|
|
36
36
|
embedded_in :embeds_orderable
|
37
37
|
end
|
38
38
|
|
39
|
+
class CustomizedOrderable
|
40
|
+
include Mongoid::Document
|
41
|
+
include Mongoid::Orderable
|
42
|
+
|
43
|
+
orderable :column => :pos
|
44
|
+
end
|
39
45
|
|
40
46
|
describe SimpleOrderable do
|
41
47
|
before :each do
|
@@ -251,4 +257,14 @@ describe Mongoid::Orderable do
|
|
251
257
|
|
252
258
|
end
|
253
259
|
|
260
|
+
describe CustomizedOrderable do
|
261
|
+
it 'does not have default position field' do
|
262
|
+
CustomizedOrderable.fields.should_not have_key('position')
|
263
|
+
end
|
264
|
+
|
265
|
+
it 'should have custom pos field' do
|
266
|
+
CustomizedOrderable.fields.should have_key('pos')
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
254
270
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_orderable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-03 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &69828211577180 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *69828211577180
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mongoid
|
27
|
-
requirement: &
|
27
|
+
requirement: &69828211576700 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *69828211576700
|
36
36
|
description: Gem allows mongoid model behave as orderable list
|
37
37
|
email:
|
38
38
|
- kinwizard@gmail.com
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
version: '0'
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project: mongoid_orderable
|
78
|
-
rubygems_version: 1.8.
|
78
|
+
rubygems_version: 1.8.10
|
79
79
|
signing_key:
|
80
80
|
specification_version: 3
|
81
81
|
summary: Acts as list mongoid implementation
|