sequel_orderable 0.0.1 → 0.0.2
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/CHANGELOG +2 -0
- data/Rakefile +1 -1
- data/lib/sequel_orderable.rb +44 -36
- metadata +2 -2
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
data/lib/sequel_orderable.rb
CHANGED
@@ -1,20 +1,50 @@
|
|
1
1
|
module Sequel
|
2
2
|
module Plugins
|
3
3
|
module Orderable
|
4
|
-
|
5
4
|
def self.apply(model, opts = {})
|
6
|
-
|
5
|
+
# defaults
|
6
|
+
opts[:field] ||= :position
|
7
|
+
|
8
|
+
# custom behavior
|
9
|
+
position_field = opts[:field]
|
7
10
|
scope_field = opts[:scope]
|
11
|
+
if scope_field
|
12
|
+
model.dataset.order!(scope_field, position_field)
|
13
|
+
else
|
14
|
+
model.dataset.order!(position_field)
|
15
|
+
end
|
16
|
+
end
|
8
17
|
|
9
|
-
|
10
|
-
|
18
|
+
module InstanceMethods
|
19
|
+
def position
|
20
|
+
@values[orderable_opts[:field]]
|
21
|
+
end
|
22
|
+
|
23
|
+
def at_position(p)
|
24
|
+
position_field = orderable_opts[:field]
|
25
|
+
if scope_field = orderable_opts[:scope]
|
11
26
|
dataset.first(scope_field => @values[scope_field], position_field => p)
|
12
27
|
else
|
13
28
|
dataset.first(position_field => p)
|
14
29
|
end
|
15
30
|
end
|
16
31
|
|
17
|
-
|
32
|
+
def prev(n = 1)
|
33
|
+
target = position - n
|
34
|
+
# XXX: error checking, negative target?
|
35
|
+
return self if position == target
|
36
|
+
at_position(target)
|
37
|
+
end
|
38
|
+
|
39
|
+
def next(n = 1)
|
40
|
+
target = position + n
|
41
|
+
at_position(target)
|
42
|
+
end
|
43
|
+
|
44
|
+
def move_to(pos)
|
45
|
+
position_field = orderable_opts[:field]
|
46
|
+
scope_field = orderable_opts[:scope]
|
47
|
+
|
18
48
|
# XXX: error checking, negative pos?
|
19
49
|
cur_pos = position
|
20
50
|
return self if pos == cur_pos
|
@@ -33,37 +63,6 @@ module Sequel
|
|
33
63
|
end
|
34
64
|
end
|
35
65
|
|
36
|
-
model.class_def(:move_to_bottom) do
|
37
|
-
ds = dataset
|
38
|
-
ds = ds.filter(scope_field => @values[scope_field]) if scope_field
|
39
|
-
last = ds.select(:max[position_field] => :max).first.values[:max].to_i
|
40
|
-
self.move_to(last)
|
41
|
-
end
|
42
|
-
|
43
|
-
model.class_def(:position) {self[position_field]}
|
44
|
-
|
45
|
-
if scope_field
|
46
|
-
model.dataset.order!(scope_field, position_field)
|
47
|
-
else
|
48
|
-
model.dataset.order!(position_field)
|
49
|
-
end
|
50
|
-
|
51
|
-
model.send(:include, InstanceMethods)
|
52
|
-
end
|
53
|
-
|
54
|
-
module InstanceMethods
|
55
|
-
def prev(n = 1)
|
56
|
-
target = position - n
|
57
|
-
# XXX: error checking, negative target?
|
58
|
-
return self if position == target
|
59
|
-
at_position(target)
|
60
|
-
end
|
61
|
-
|
62
|
-
def next(n = 1)
|
63
|
-
target = position + n
|
64
|
-
at_position(target)
|
65
|
-
end
|
66
|
-
|
67
66
|
def move_up(n = 1)
|
68
67
|
# XXX: position == 1 already?
|
69
68
|
self.move_to(position-n)
|
@@ -77,6 +76,15 @@ module Sequel
|
|
77
76
|
def move_to_top
|
78
77
|
self.move_to(1)
|
79
78
|
end
|
79
|
+
|
80
|
+
def move_to_bottom
|
81
|
+
position_field = orderable_opts[:field]
|
82
|
+
scope_field = orderable_opts[:scope]
|
83
|
+
ds = dataset
|
84
|
+
ds = ds.filter(scope_field => @values[scope_field]) if scope_field
|
85
|
+
last = ds.select(:max[position_field] => :max).first.values[:max].to_i
|
86
|
+
self.move_to(last)
|
87
|
+
end
|
80
88
|
end
|
81
89
|
|
82
90
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel_orderable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wayne E. Seguin & Aman Gupta
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2007-12-
|
12
|
+
date: 2007-12-05 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|