aemadrid-sequel_orderable 0.0.4
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/.document +5 -0
- data/.gitignore +6 -0
- data/CHANGELOG +15 -0
- data/LICENSE +18 -0
- data/README.rdoc +175 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/sequel_orderable.rb +113 -0
- data/sequel_orderable.gemspec +48 -0
- data/spec/orderable_spec.rb +133 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +31 -0
- metadata +67 -0
data/.document
ADDED
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
== 0.0.4 (2009-08-26)
|
2
|
+
|
3
|
+
* Updated gem to latest plugin conventions.
|
4
|
+
|
5
|
+
* Added exceptions for out-of-range situations.
|
6
|
+
|
7
|
+
* Packed gem with Jeweler.
|
8
|
+
|
9
|
+
== 0.0.3 (2007-12-30)
|
10
|
+
|
11
|
+
* Updated gem dependency on sequel_model.
|
12
|
+
|
13
|
+
* 2007.12.05 Modified for better readability and new plugin conventions.
|
14
|
+
|
15
|
+
* 2007.12.02 Initial working revision.
|
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2007 Sharon Rosner, Wayne E. Seguin, Aman Gupta, Adrian Madrid
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
= Sequel Orderable Plugin
|
2
|
+
|
3
|
+
Allows for model instances to be part of an ordered list,
|
4
|
+
based on a 'position' field in the database.
|
5
|
+
|
6
|
+
== Basic Usage
|
7
|
+
|
8
|
+
Load the plugin into the model:
|
9
|
+
|
10
|
+
plugin :orderable
|
11
|
+
|
12
|
+
Given:
|
13
|
+
|
14
|
+
DB.create_table :items do
|
15
|
+
primary_key :id
|
16
|
+
varchar :name
|
17
|
+
int :position
|
18
|
+
end
|
19
|
+
|
20
|
+
class Item < Sequel::Model(:items)
|
21
|
+
plugin :orderable
|
22
|
+
end
|
23
|
+
|
24
|
+
item = Item[1]
|
25
|
+
|
26
|
+
The plugin provides access to the previous and next item in the list
|
27
|
+
|
28
|
+
item.next
|
29
|
+
item.prev
|
30
|
+
|
31
|
+
And methods to change the position of an item (and update affected items accordingly)
|
32
|
+
|
33
|
+
item.move_to(new_position)
|
34
|
+
item.move_to_top
|
35
|
+
item.move_to_bottom
|
36
|
+
item.move_up
|
37
|
+
item.move_down
|
38
|
+
|
39
|
+
== Scoping
|
40
|
+
|
41
|
+
You can scope the position field by another field.
|
42
|
+
|
43
|
+
For example, to allow each user to have their own a distinct orderable list:
|
44
|
+
|
45
|
+
DB.create_table :items do
|
46
|
+
primary_key :id
|
47
|
+
varchar :name
|
48
|
+
int :user_id
|
49
|
+
int :pos
|
50
|
+
end
|
51
|
+
|
52
|
+
class Item < Sequel::Model(:items)
|
53
|
+
plugin :orderable, :field => :pos, :scope => :user_id
|
54
|
+
end
|
55
|
+
|
56
|
+
All the defined methods will operate within the 'user_id' field's scope.
|
57
|
+
|
58
|
+
== Examples
|
59
|
+
|
60
|
+
# Input: irb
|
61
|
+
require 'sequel'
|
62
|
+
require 'sequel_orderable'
|
63
|
+
require 'sequel/extensions/pretty_table' # for pretty prints
|
64
|
+
|
65
|
+
DB = Sequel.sqlite
|
66
|
+
|
67
|
+
DB.create_table :items do
|
68
|
+
primary_key :id
|
69
|
+
varchar :name
|
70
|
+
int :position
|
71
|
+
end
|
72
|
+
|
73
|
+
class Item < Sequel::Model(:items)
|
74
|
+
plugin :orderable
|
75
|
+
end
|
76
|
+
|
77
|
+
Item.create :name => "alice", :position => 2
|
78
|
+
Item.create :name => "bob", :position => 1
|
79
|
+
Item.create :name => "charlie", :position => 4
|
80
|
+
Item.create :name => "darwin", :position => 3
|
81
|
+
|
82
|
+
Item.print
|
83
|
+
|
84
|
+
Item[:name => "alice"].move_down
|
85
|
+
Item.print
|
86
|
+
Item[:name => "darwin"].move_to_top
|
87
|
+
Item.print
|
88
|
+
Item[:name => "alice"].next
|
89
|
+
Item.print
|
90
|
+
Item[:name => "bob"].prev
|
91
|
+
Item.print
|
92
|
+
Item[:name => "darwin"].move_to(3)
|
93
|
+
Item.print
|
94
|
+
Item[:name => "bob"].move_to_bottom
|
95
|
+
Item.print
|
96
|
+
|
97
|
+
|
98
|
+
# Output
|
99
|
+
>> Item.print
|
100
|
+
+--+-------+--------+
|
101
|
+
|id|name |position|
|
102
|
+
+--+-------+--------+
|
103
|
+
| 2|bob | 1|
|
104
|
+
| 1|alice | 2|
|
105
|
+
| 4|darwin | 3|
|
106
|
+
| 3|charlie| 4|
|
107
|
+
+--+-------+--------+
|
108
|
+
=> nil
|
109
|
+
|
110
|
+
>> Item[:name => "alice"].move_down
|
111
|
+
=> #<Item @values={:name=>"alice", :position=>3, :id=>1}>
|
112
|
+
|
113
|
+
>> Item.print
|
114
|
+
+--+-------+--------+
|
115
|
+
|id|name |position|
|
116
|
+
+--+-------+--------+
|
117
|
+
| 2|bob | 1|
|
118
|
+
| 4|darwin | 2|
|
119
|
+
| 1|alice | 3|
|
120
|
+
| 3|charlie| 4|
|
121
|
+
+--+-------+--------+
|
122
|
+
=> nil
|
123
|
+
|
124
|
+
>> Item[:name => "darwin"].move_to_top
|
125
|
+
=> #<Item @values={:name=>"darwin", :position=>1, :id=>4}>
|
126
|
+
|
127
|
+
>> Item.print
|
128
|
+
+--+-------+--------+
|
129
|
+
|id|name |position|
|
130
|
+
+--+-------+--------+
|
131
|
+
| 4|darwin | 1|
|
132
|
+
| 2|bob | 2|
|
133
|
+
| 1|alice | 3|
|
134
|
+
| 3|charlie| 4|
|
135
|
+
+--+-------+--------+
|
136
|
+
=> nil
|
137
|
+
|
138
|
+
>> Item[:name => "alice"].next
|
139
|
+
=> #<Item @values={:name=>"charlie", :position=>4, :id=>3}>
|
140
|
+
|
141
|
+
>> Item[:name => "bob"].prev
|
142
|
+
=> #<Item @values={:name=>"darwin", :position=>1, :id=>4}>
|
143
|
+
|
144
|
+
>> Item.print
|
145
|
+
+--+-------+--------+
|
146
|
+
|id|name |position|
|
147
|
+
+--+-------+--------+
|
148
|
+
| 4|darwin | 1|
|
149
|
+
| 2|bob | 2|
|
150
|
+
| 1|alice | 3|
|
151
|
+
| 3|charlie| 4|
|
152
|
+
+--+-------+--------+
|
153
|
+
=> nil
|
154
|
+
|
155
|
+
|
156
|
+
== Copyright
|
157
|
+
|
158
|
+
Copyright (c) 2007 Sharon Rosner, Wayne E. Seguin, Aman Gupta, Adrian Madrid
|
159
|
+
|
160
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
161
|
+
of this software and associated documentation files (the "Software"), to
|
162
|
+
deal in the Software without restriction, including without limitation the
|
163
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
164
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
165
|
+
furnished to do so, subject to the following conditions:
|
166
|
+
|
167
|
+
The above copyright notice and this permission notice shall be included in
|
168
|
+
all copies or substantial portions of the Software.
|
169
|
+
|
170
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
171
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
172
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
173
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
174
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
175
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "sequel_orderable"
|
8
|
+
gem.summary = %Q{Update of Aman Gupta's Sequel Orderable Plugin}
|
9
|
+
gem.email = "aemadrid@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/aemadrid/sequel_orderable"
|
11
|
+
gem.authors = ["Adrian Madrid"]
|
12
|
+
end
|
13
|
+
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'spec/rake/spectask'
|
19
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
20
|
+
spec.libs << 'lib'
|
21
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
22
|
+
end
|
23
|
+
|
24
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
25
|
+
spec.libs << 'lib'
|
26
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
27
|
+
spec.rcov = true
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :spec
|
31
|
+
|
32
|
+
require 'rake/rdoctask'
|
33
|
+
Rake::RDocTask.new do |rdoc|
|
34
|
+
if File.exist?('VERSION.yml')
|
35
|
+
config = YAML.load(File.read('VERSION.yml'))
|
36
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
37
|
+
else
|
38
|
+
version = ""
|
39
|
+
end
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "orderable #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
46
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.4
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
# The orderable plugin allows for model instances to be part of an ordered list,
|
4
|
+
# based on a 'position' field in the database.
|
5
|
+
#
|
6
|
+
# Page.plugin :orderable, :field => :position, :scope => :parent_id
|
7
|
+
#
|
8
|
+
module Orderable
|
9
|
+
|
10
|
+
def self.apply(model, opts = {})
|
11
|
+
opts[:field] ||= :position
|
12
|
+
|
13
|
+
position_field = opts[:field]
|
14
|
+
scope_field = opts[:scope]
|
15
|
+
if scope_field
|
16
|
+
model.dataset.order!(scope_field, position_field)
|
17
|
+
else
|
18
|
+
model.dataset.order!(position_field)
|
19
|
+
end
|
20
|
+
|
21
|
+
model.instance_eval <<-CODE
|
22
|
+
def orderable_options
|
23
|
+
#{opts.inspect}
|
24
|
+
end
|
25
|
+
CODE
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
def orderable_position_field
|
30
|
+
self.class.orderable_options[:field]
|
31
|
+
end
|
32
|
+
|
33
|
+
def orderable_position_value
|
34
|
+
@values[orderable_position_field]
|
35
|
+
end
|
36
|
+
|
37
|
+
def orderable_scope_field
|
38
|
+
self.class.orderable_options[:scope]
|
39
|
+
end
|
40
|
+
|
41
|
+
def orderable_scope_value
|
42
|
+
orderable_scope_field && @values[orderable_scope_field]
|
43
|
+
end
|
44
|
+
|
45
|
+
def at_position(p)
|
46
|
+
if orderable_scope_field
|
47
|
+
self.class.dataset.first orderable_scope_field => orderable_scope_value, orderable_position_field => p
|
48
|
+
else
|
49
|
+
self.class.dataset.first orderable_position_field => p
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def prev(n = 1)
|
54
|
+
target = orderable_position_value - n
|
55
|
+
return self if orderable_position_value == target
|
56
|
+
at_position target
|
57
|
+
end
|
58
|
+
|
59
|
+
def next(n = 1)
|
60
|
+
target = orderable_position_value + n
|
61
|
+
at_position target
|
62
|
+
end
|
63
|
+
|
64
|
+
def move_to(target)
|
65
|
+
raise "Moving too far up" if target < 0
|
66
|
+
raise "Moving too far down" if target > last_orderable_position
|
67
|
+
current = orderable_position_value
|
68
|
+
return self if target == current
|
69
|
+
|
70
|
+
db.transaction do
|
71
|
+
if target < current
|
72
|
+
ds = self.class.dataset.filter "? >= ? AND ? < ?", orderable_position_field, target, orderable_position_field, current
|
73
|
+
ds.filter! orderable_scope_field => orderable_scope_value if orderable_scope_field
|
74
|
+
ds.update orderable_position_field => "#{orderable_position_field} + 1".lit
|
75
|
+
else
|
76
|
+
ds = self.class.dataset.filter "? > ? AND ? <= ?", orderable_position_field, current, orderable_position_field, target
|
77
|
+
ds.filter! orderable_scope_field => orderable_scope_value if orderable_scope_field
|
78
|
+
ds.update orderable_position_field => "#{orderable_position_field} - 1".lit
|
79
|
+
end
|
80
|
+
update orderable_position_field => target
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def move_up(n = 1)
|
85
|
+
target = orderable_position_value - n
|
86
|
+
raise "Moving too far up" if target < 0
|
87
|
+
self.move_to target
|
88
|
+
end
|
89
|
+
|
90
|
+
def move_down(n = 1)
|
91
|
+
target = orderable_position_value + n
|
92
|
+
raise "Moving too far down" if target > last_orderable_position
|
93
|
+
self.move_to target
|
94
|
+
end
|
95
|
+
|
96
|
+
def move_to_top
|
97
|
+
self.move_to 1
|
98
|
+
end
|
99
|
+
|
100
|
+
def move_to_bottom
|
101
|
+
self.move_to last_orderable_position
|
102
|
+
end
|
103
|
+
|
104
|
+
def last_orderable_position
|
105
|
+
ds = self.class.dataset
|
106
|
+
ds = ds.filter orderable_scope_field => orderable_scope_value if orderable_scope_field
|
107
|
+
ds.max(orderable_position_field).to_i
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{sequel_orderable}
|
5
|
+
s.version = "0.0.4"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Adrian Madrid"]
|
9
|
+
s.date = %q{2009-08-26}
|
10
|
+
s.email = %q{aemadrid@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"CHANGELOG",
|
19
|
+
"LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/sequel_orderable.rb",
|
24
|
+
"sequel_orderable.gemspec",
|
25
|
+
"spec/orderable_spec.rb",
|
26
|
+
"spec/spec.opts",
|
27
|
+
"spec/spec_helper.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/aemadrid/sequel_orderable}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.5}
|
33
|
+
s.summary = %q{Update of Aman Gupta's Sequel Orderable Plugin}
|
34
|
+
s.test_files = [
|
35
|
+
"spec/spec_helper.rb",
|
36
|
+
"spec/orderable_spec.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Orderable" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "without a scope" do
|
9
|
+
before :all do
|
10
|
+
@c = Class.new(Sequel::Model(:sites)) do
|
11
|
+
plugin :orderable
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
@c.delete
|
17
|
+
@c.create :name => "hig", :position => 3
|
18
|
+
@c.create :name => "def", :position => 2
|
19
|
+
@c.create :name => "abc", :position => 1
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return rows in order of position" do
|
23
|
+
@c.map(&:position).should == [1,2,3]
|
24
|
+
@c.map(&:name).should == %w[ abc def hig ]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should define prev and next" do
|
28
|
+
i = @c[:name => "abc"]
|
29
|
+
i.prev.should == nil
|
30
|
+
i = @c[:name => "def"]
|
31
|
+
i.prev.should == @c[:name => "abc"]
|
32
|
+
i.next.should == @c[:name => "hig"]
|
33
|
+
i = @c[:name => "hig"]
|
34
|
+
i.next.should == nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should define move_to" do
|
38
|
+
@c[:name => "def"].move_to(1)
|
39
|
+
@c.map(&:name).should == %w[ def abc hig ]
|
40
|
+
|
41
|
+
@c[:name => "abc"].move_to(3)
|
42
|
+
@c.map(&:name).should == %w[ def hig abc ]
|
43
|
+
|
44
|
+
proc { @c[:name => "abc"].move_to(-1) }.should raise_error(RuntimeError)
|
45
|
+
proc { @c[:name => "abc"].move_to(10) }.should raise_error(RuntimeError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should define move_to_top and move_to_bottom" do
|
49
|
+
@c[:name => "def"].move_to_top
|
50
|
+
@c.map(&:name).should == %w[ def abc hig ]
|
51
|
+
|
52
|
+
@c[:name => "def"].move_to_bottom
|
53
|
+
@c.map(&:name).should == %w[ abc hig def ]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should define move_up and move_down" do
|
57
|
+
@c[:name => "def"].move_up
|
58
|
+
@c.map(&:name).should == %w[ def abc hig ]
|
59
|
+
|
60
|
+
@c[:name => "abc"].move_down
|
61
|
+
@c.map(&:name).should == %w[ def hig abc ]
|
62
|
+
|
63
|
+
proc { @c[:name => "def"].move_up(10) }.should raise_error(RuntimeError)
|
64
|
+
proc { @c[:name => "def"].move_down(10) }.should raise_error(RuntimeError)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "with a scope" do
|
70
|
+
before :all do
|
71
|
+
@c = Class.new(Sequel::Model(:pages)) do
|
72
|
+
plugin :orderable, :field => :pos, :scope => :parent_id
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
before :each do
|
77
|
+
@c.delete
|
78
|
+
p1 = @c.create :name => "Hm", :pos => 1, :parent_id => nil
|
79
|
+
p2 = @c.create :name => "Ps", :pos => 1, :parent_id => p1.id
|
80
|
+
@c.create :name => "P1", :pos => 1, :parent_id => p2.id
|
81
|
+
@c.create :name => "P2", :pos => 2, :parent_id => p2.id
|
82
|
+
@c.create :name => "P3", :pos => 3, :parent_id => p2.id
|
83
|
+
@c.create :name => "Au", :pos => 2, :parent_id => p1.id
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should return rows in order of position" do
|
87
|
+
@c.map(&:name).should == %w[ Hm Ps Au P1 P2 P3 ]
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should define prev and next" do
|
91
|
+
@c[:name => "Ps"].next.name.should == 'Au'
|
92
|
+
@c[:name => "Au"].prev.name.should == 'Ps'
|
93
|
+
@c[:name => "P1"].next.name.should == 'P2'
|
94
|
+
@c[:name => "P2"].prev.name.should == 'P1'
|
95
|
+
|
96
|
+
@c[:name => "Ps"].prev.should == nil
|
97
|
+
@c[:name => "Au"].next.should == nil
|
98
|
+
@c[:name => "P1"].prev.should == nil
|
99
|
+
@c[:name => "P3"].next.should == nil
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should define move_to" do
|
103
|
+
@c[:name => "P2"].move_to(1)
|
104
|
+
@c.map(&:name).should == %w[ Hm Ps Au P2 P1 P3 ]
|
105
|
+
|
106
|
+
@c[:name => "P2"].move_to(3)
|
107
|
+
@c.map(&:name).should == %w[ Hm Ps Au P1 P3 P2 ]
|
108
|
+
|
109
|
+
proc { @c[:name => "P2"].move_to(-1) }.should raise_error(RuntimeError)
|
110
|
+
proc { @c[:name => "P2"].move_to(10) }.should raise_error(RuntimeError)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should define move_to_top and move_to_bottom" do
|
114
|
+
@c[:name => "Au"].move_to_top
|
115
|
+
@c.map(&:name).should == %w[ Hm Au Ps P1 P2 P3 ]
|
116
|
+
|
117
|
+
@c[:name => "Au"].move_to_bottom
|
118
|
+
@c.map(&:name).should == %w[ Hm Ps Au P1 P2 P3 ]
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should define move_up and move_down" do
|
122
|
+
@c[:name => "P2"].move_up
|
123
|
+
@c.map(&:name).should == %w[ Hm Ps Au P2 P1 P3 ]
|
124
|
+
|
125
|
+
@c[:name => "P1"].move_down
|
126
|
+
@c.map(&:name).should == %w[ Hm Ps Au P2 P3 P1 ]
|
127
|
+
|
128
|
+
proc { @c[:name => "P1"].move_up(10) }.should raise_error(RuntimeError)
|
129
|
+
proc { @c[:name => "P1"].move_down(10) }.should raise_error(RuntimeError)
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'sequel'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
|
7
|
+
require 'sequel_orderable'
|
8
|
+
|
9
|
+
Spec::Runner.configure do |config|
|
10
|
+
end
|
11
|
+
|
12
|
+
class Symbol
|
13
|
+
def to_proc() lambda{ |object, *args| object.send(self, *args) } end
|
14
|
+
end
|
15
|
+
|
16
|
+
DB = Sequel.connect ENV['SEQUEL_ORDERABLE_CONN'] || 'sqlite:/'
|
17
|
+
|
18
|
+
DB.create_table :sites do
|
19
|
+
primary_key :id
|
20
|
+
varchar :name
|
21
|
+
int :position
|
22
|
+
end
|
23
|
+
|
24
|
+
DB.create_table :pages do
|
25
|
+
primary_key :id
|
26
|
+
varchar :name
|
27
|
+
int :pos
|
28
|
+
int :parent_id
|
29
|
+
end
|
30
|
+
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aemadrid-sequel_orderable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adrian Madrid
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-26 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: aemadrid@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- CHANGELOG
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
- Rakefile
|
32
|
+
- VERSION
|
33
|
+
- lib/sequel_orderable.rb
|
34
|
+
- sequel_orderable.gemspec
|
35
|
+
- spec/orderable_spec.rb
|
36
|
+
- spec/spec.opts
|
37
|
+
- spec/spec_helper.rb
|
38
|
+
has_rdoc: false
|
39
|
+
homepage: http://github.com/aemadrid/sequel_orderable
|
40
|
+
licenses:
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Update of Aman Gupta's Sequel Orderable Plugin
|
65
|
+
test_files:
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/orderable_spec.rb
|