ar-orderable 1.0.2 → 1.0.3
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/.rspec +2 -2
- data/MIT-LICENSE +20 -20
- data/README.md +46 -46
- data/VERSION +1 -1
- data/ar-orderable.gemspec +6 -6
- data/lib/ar-orderable.rb +21 -17
- data/spec/ar-orderable_spec.rb +140 -129
- data/spec/spec.opts +2 -2
- data/spec/spec_helper.rb +61 -53
- metadata +18 -20
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--colour
|
2
|
-
--format d
|
1
|
+
--colour
|
2
|
+
--format d
|
data/MIT-LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2009 IT House, Gatis Tomsons
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2009 IT House, Gatis Tomsons
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,47 +1,47 @@
|
|
1
|
-
## Activerecord::Orderable
|
2
|
-
|
3
|
-
Rails 3 plugin for simple ordering.
|
4
|
-
|
5
|
-
### Install
|
6
|
-
|
7
|
-
Insert into your `Gemfile`:
|
8
|
-
|
9
|
-
gem 'ar-orderable'
|
10
|
-
|
11
|
-
then `bundle install`
|
12
|
-
|
13
|
-
### Setup
|
14
|
-
|
15
|
-
1. Add order field, like "order_nr" as integer
|
16
|
-
2. In model add line "acts_as_orderable" and if needed add :column => "my_orderfield_name".
|
17
|
-
3. If your table already has some rows of data then use the 'order_unordered' after adding new column:
|
18
|
-
|
19
|
-
Example migration:
|
20
|
-
|
21
|
-
add_column :categories, :order_nr, :integer
|
22
|
-
Category.order_unordered # remove this for new table
|
23
|
-
add_index :categories, :order_nr
|
24
|
-
|
25
|
-
### Examples
|
26
|
-
|
27
|
-
To reorder items use the `move_to(<integer>)`, `move_up` and `move_down` methods, for example:
|
28
|
-
|
29
|
-
item.move_to 3 # moved to 3rd position
|
30
|
-
item.move_up # moved to 2rd position
|
31
|
-
item.move_down # moved to 3d position
|
32
|
-
|
33
|
-
To skip model callbacks and just update order information you can specify `:skip_callbacks => true` option:
|
34
|
-
|
35
|
-
# in your model
|
36
|
-
acts_as_orderable :skip_callbacks => true
|
37
|
-
|
38
|
-
# or whenever you call one of the ordering methods
|
39
|
-
item.move_to 3, :skip_callbacks => true
|
40
|
-
item.move_up :skip_callbacks => true
|
41
|
-
item.move_down :skip_callbacks => true
|
42
|
-
|
43
|
-
### Tests
|
44
|
-
|
45
|
-
rspec spec # all examples should be green
|
46
|
-
|
1
|
+
## Activerecord::Orderable
|
2
|
+
|
3
|
+
Rails 3 plugin for simple ordering.
|
4
|
+
|
5
|
+
### Install
|
6
|
+
|
7
|
+
Insert into your `Gemfile`:
|
8
|
+
|
9
|
+
gem 'ar-orderable'
|
10
|
+
|
11
|
+
then `bundle install`
|
12
|
+
|
13
|
+
### Setup
|
14
|
+
|
15
|
+
1. Add order field, like "order_nr" as integer
|
16
|
+
2. In model add line "acts_as_orderable" and if needed add :column => "my_orderfield_name".
|
17
|
+
3. If your table already has some rows of data then use the 'order_unordered' after adding new column:
|
18
|
+
|
19
|
+
Example migration:
|
20
|
+
|
21
|
+
add_column :categories, :order_nr, :integer
|
22
|
+
Category.order_unordered # remove this for new table
|
23
|
+
add_index :categories, :order_nr
|
24
|
+
|
25
|
+
### Examples
|
26
|
+
|
27
|
+
To reorder items use the `move_to(<integer>)`, `move_up` and `move_down` methods, for example:
|
28
|
+
|
29
|
+
item.move_to 3 # moved to 3rd position
|
30
|
+
item.move_up # moved to 2rd position
|
31
|
+
item.move_down # moved to 3d position
|
32
|
+
|
33
|
+
To skip model callbacks and just update order information you can specify `:skip_callbacks => true` option:
|
34
|
+
|
35
|
+
# in your model
|
36
|
+
acts_as_orderable :skip_callbacks => true
|
37
|
+
|
38
|
+
# or whenever you call one of the ordering methods
|
39
|
+
item.move_to 3, :skip_callbacks => true
|
40
|
+
item.move_up :skip_callbacks => true
|
41
|
+
item.move_down :skip_callbacks => true
|
42
|
+
|
43
|
+
### Tests
|
44
|
+
|
45
|
+
rspec spec # all examples should be green
|
46
|
+
|
47
47
|
Copyright (c) 2009 IT House, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.3
|
data/ar-orderable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-orderable}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date = %q{2011-08
|
11
|
+
s.authors = [%q{Gatis Tomsons}, %q{IT House}]
|
12
|
+
s.date = %q{2011-11-08}
|
13
13
|
s.description = %q{You can order AR records and skip callbacks}
|
14
14
|
s.email = %q{gatis@ithouse.cc}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -31,9 +31,9 @@ Gem::Specification.new do |s|
|
|
31
31
|
"spec/spec_helper.rb"
|
32
32
|
]
|
33
33
|
s.homepage = %q{http://github.com/ithouse/ar-orderable}
|
34
|
-
s.licenses = [
|
35
|
-
s.require_paths = [
|
36
|
-
s.rubygems_version = %q{1.
|
34
|
+
s.licenses = [%q{MIT}]
|
35
|
+
s.require_paths = [%q{lib}]
|
36
|
+
s.rubygems_version = %q{1.8.8}
|
37
37
|
s.summary = %q{Rails 3 plugin for simple ordering.}
|
38
38
|
|
39
39
|
if s.respond_to? :specification_version then
|
data/lib/ar-orderable.rb
CHANGED
@@ -78,37 +78,41 @@ module ActiveRecord # :nodoc:
|
|
78
78
|
private
|
79
79
|
|
80
80
|
def pre_save_ordering
|
81
|
-
|
81
|
+
column_name = self.class.orderable_column
|
82
|
+
|
83
|
+
self[column_name] = 0 if self[column_name].nil?
|
82
84
|
if self.id
|
83
|
-
if self[
|
84
|
-
self[
|
85
|
+
if self[column_name] == 0
|
86
|
+
self[column_name] = 1
|
85
87
|
end
|
86
|
-
if self[
|
87
|
-
self[
|
88
|
+
if self[column_name] > self.all_orderable.count
|
89
|
+
self[column_name] = self[column_name] -1
|
88
90
|
end
|
89
91
|
else
|
90
|
-
self[
|
92
|
+
self[column_name] = self.all_orderable.count + 1 if self[column_name].to_i == 0
|
91
93
|
end
|
94
|
+
|
95
|
+
return unless self.all_orderable.where("id != ? and #{column_name} = ?", self.id, self[column_name]).count > 0
|
92
96
|
self.all_orderable.where("#{self.class.table_name}.id != ?",self.id || 0).each do |item|
|
93
|
-
item[
|
97
|
+
item[column_name] = 0 if item[column_name].nil?
|
94
98
|
if self.id
|
95
|
-
if item[
|
96
|
-
if item[
|
97
|
-
item[
|
99
|
+
if item[column_name] > (self.send("#{column_name}_was") || 0 )
|
100
|
+
if item[column_name] <= self[column_name]
|
101
|
+
item[column_name] -= 1
|
98
102
|
end
|
99
103
|
else
|
100
|
-
if item[
|
101
|
-
item[
|
104
|
+
if item[column_name] >= self[column_name]
|
105
|
+
item[column_name] += 1
|
102
106
|
end
|
103
107
|
end
|
104
108
|
|
105
|
-
if item[
|
106
|
-
self.class.raw_orderable_update(item.id, item[
|
109
|
+
if item[column_name] != item.send("#{column_name}_was")
|
110
|
+
self.class.raw_orderable_update(item.id, item[column_name])
|
107
111
|
end
|
108
112
|
else
|
109
|
-
if item[
|
110
|
-
item[
|
111
|
-
self.class.raw_orderable_update(item.id, item[
|
113
|
+
if item[column_name] >= self[column_name]
|
114
|
+
item[column_name] += 1
|
115
|
+
self.class.raw_orderable_update(item.id, item[column_name])
|
112
116
|
end
|
113
117
|
end
|
114
118
|
end
|
data/spec/ar-orderable_spec.rb
CHANGED
@@ -1,130 +1,141 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
describe ActiveRecord::Orderable do
|
4
|
-
it "should order simple categories correctly" do
|
5
|
-
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
6
|
-
Category.count.should == 5
|
7
|
-
Category.first.order_nr.should == 1
|
8
|
-
Category.last.order_nr.should == 5
|
9
|
-
c = Category.first
|
10
|
-
c.update_attribute(:order_nr, 4)
|
11
|
-
Category.find_by_name("Cat 1").order_nr.should == 4
|
12
|
-
Category.first.name.should == "Cat 2"
|
13
|
-
c = Category.first
|
14
|
-
Category.find_by_name("Cat 4").move_to 1
|
15
|
-
Category.find_by_name("Cat 4").order_nr.should == 1
|
16
|
-
Category.find(c.id).order_nr.should == 2
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should order categories correct by scope" do
|
20
|
-
cat_types = [
|
21
|
-
CatType.create(:name => "Type 1"),
|
22
|
-
CatType.create(:name => "Type 2")
|
23
|
-
]
|
24
|
-
|
25
|
-
5.times{|i| Category.create(:name => "Cat #{i+1}", :cat_type => cat_types.first)}
|
26
|
-
5.times{|i| Category.create(:name => "Cat #{i+6}", :cat_type => cat_types.last)}
|
27
|
-
|
28
|
-
Category.find_by_name("Cat 9").move_to 1
|
29
|
-
Category.find_by_name("Cat 9").order_nr.should == 1
|
30
|
-
Category.find_by_name("Cat 6").order_nr.should == 2
|
31
|
-
Category.find_by_name("Cat 7").order_nr.should == 3
|
32
|
-
Category.find_by_name("Cat 8").order_nr.should == 4
|
33
|
-
Category.find_by_name("Cat 10").order_nr.should == 5
|
34
|
-
|
35
|
-
Category.find_by_name("Cat 7").move_to 5
|
36
|
-
|
37
|
-
Category.find_by_name("Cat 9").order_nr.should == 1
|
38
|
-
Category.find_by_name("Cat 6").order_nr.should == 2
|
39
|
-
Category.find_by_name("Cat 8").order_nr.should == 3
|
40
|
-
Category.find_by_name("Cat 10").order_nr.should == 4
|
41
|
-
Category.find_by_name("Cat 7").order_nr.should == 5
|
42
|
-
|
43
|
-
Category.find_by_name("Cat 7").move_to 4
|
44
|
-
|
45
|
-
Category.find_by_name("Cat 9").order_nr.should == 1
|
46
|
-
Category.find_by_name("Cat 6").order_nr.should == 2
|
47
|
-
Category.find_by_name("Cat 8").order_nr.should == 3
|
48
|
-
Category.find_by_name("Cat 7").order_nr.should == 4
|
49
|
-
Category.find_by_name("Cat 10").order_nr.should == 5
|
50
|
-
|
51
|
-
Category.find_by_name("Cat 9").move_to 5
|
52
|
-
|
53
|
-
Category.find_by_name("Cat 6").order_nr.should == 1
|
54
|
-
Category.find_by_name("Cat 8").order_nr.should == 2
|
55
|
-
Category.find_by_name("Cat 7").order_nr.should == 3
|
56
|
-
Category.find_by_name("Cat 10").order_nr.should == 4
|
57
|
-
Category.find_by_name("Cat 9").order_nr.should == 5
|
58
|
-
|
59
|
-
Category.find_by_name("Cat 10").move_to 2
|
60
|
-
|
61
|
-
Category.find_by_name("Cat 6").order_nr.should == 1
|
62
|
-
Category.find_by_name("Cat 10").order_nr.should == 2
|
63
|
-
Category.find_by_name("Cat 8").order_nr.should == 3
|
64
|
-
Category.find_by_name("Cat 7").order_nr.should == 4
|
65
|
-
Category.find_by_name("Cat 9").order_nr.should == 5
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should create unordered list and order it with order_unordered method" do
|
69
|
-
1.upto(4){|i| Category.create(:name => "Cat #{i}")}
|
70
|
-
cat = Category.create(:name => "Cat 0")
|
71
|
-
Category.update_all("order_nr = NULL")
|
72
|
-
Category.order_unordered
|
73
|
-
cat.order_nr.should_not == 1
|
74
|
-
cat.move_to 1
|
75
|
-
cat.reload
|
76
|
-
cat.order_nr.should == 1
|
77
|
-
Category.find_by_name("Cat 4").order_nr.should == 5
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should create scoped unordered list and order it with order_unordered method" do
|
81
|
-
cat_types = [
|
82
|
-
CatType.create(:name => "Type 1"),
|
83
|
-
CatType.create(:name => "Type 2")
|
84
|
-
]
|
85
|
-
5.times{|i| Category.create(:name => "Cat #{i+1}", :cat_type => cat_types.first)}
|
86
|
-
5.times{|i| Category.create(:name => "Cat #{i+6}", :cat_type => cat_types.last)}
|
87
|
-
Category.update_all("order_nr = NULL")
|
88
|
-
Category.all.map(&:order_nr).should == [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
|
89
|
-
Category.order_unordered
|
90
|
-
Category.all.map(&:order_nr).should == [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should move_up" do
|
94
|
-
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
95
|
-
c = Category.find_by_name("Cat 2")
|
96
|
-
c.order_nr.should == 2
|
97
|
-
c.move_up
|
98
|
-
c.reload
|
99
|
-
c.order_nr.should == 1
|
100
|
-
c.move_up
|
101
|
-
c.reload
|
102
|
-
c.order_nr.should == 1
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should move_down" do
|
106
|
-
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
107
|
-
c = Category.find_by_name("Cat 2")
|
108
|
-
c.order_nr.should == 2
|
109
|
-
c.move_down
|
110
|
-
c.reload
|
111
|
-
c.order_nr.should == 3
|
112
|
-
c.move_to(5)
|
113
|
-
c.reload
|
114
|
-
c.order_nr.should == 5
|
115
|
-
c.move_down
|
116
|
-
c.reload
|
117
|
-
c.order_nr.should == 5
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should skip_callbacks" do
|
121
|
-
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
122
|
-
c = Category.find_by_name("Cat 2")
|
123
|
-
c.background_task.should be_nil
|
124
|
-
c.move_down
|
125
|
-
c.background_task.should == :done
|
126
|
-
c.background_task = nil
|
127
|
-
c.move_up(:skip_callbacks => true)
|
128
|
-
c.background_task.should be_nil
|
129
|
-
end
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Orderable do
|
4
|
+
it "should order simple categories correctly" do
|
5
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
6
|
+
Category.count.should == 5
|
7
|
+
Category.first.order_nr.should == 1
|
8
|
+
Category.last.order_nr.should == 5
|
9
|
+
c = Category.first
|
10
|
+
c.update_attribute(:order_nr, 4)
|
11
|
+
Category.find_by_name("Cat 1").order_nr.should == 4
|
12
|
+
Category.first.name.should == "Cat 2"
|
13
|
+
c = Category.first
|
14
|
+
Category.find_by_name("Cat 4").move_to 1
|
15
|
+
Category.find_by_name("Cat 4").order_nr.should == 1
|
16
|
+
Category.find(c.id).order_nr.should == 2
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should order categories correct by scope" do
|
20
|
+
cat_types = [
|
21
|
+
CatType.create(:name => "Type 1"),
|
22
|
+
CatType.create(:name => "Type 2")
|
23
|
+
]
|
24
|
+
|
25
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}", :cat_type => cat_types.first)}
|
26
|
+
5.times{|i| Category.create(:name => "Cat #{i+6}", :cat_type => cat_types.last)}
|
27
|
+
|
28
|
+
Category.find_by_name("Cat 9").move_to 1
|
29
|
+
Category.find_by_name("Cat 9").order_nr.should == 1
|
30
|
+
Category.find_by_name("Cat 6").order_nr.should == 2
|
31
|
+
Category.find_by_name("Cat 7").order_nr.should == 3
|
32
|
+
Category.find_by_name("Cat 8").order_nr.should == 4
|
33
|
+
Category.find_by_name("Cat 10").order_nr.should == 5
|
34
|
+
|
35
|
+
Category.find_by_name("Cat 7").move_to 5
|
36
|
+
|
37
|
+
Category.find_by_name("Cat 9").order_nr.should == 1
|
38
|
+
Category.find_by_name("Cat 6").order_nr.should == 2
|
39
|
+
Category.find_by_name("Cat 8").order_nr.should == 3
|
40
|
+
Category.find_by_name("Cat 10").order_nr.should == 4
|
41
|
+
Category.find_by_name("Cat 7").order_nr.should == 5
|
42
|
+
|
43
|
+
Category.find_by_name("Cat 7").move_to 4
|
44
|
+
|
45
|
+
Category.find_by_name("Cat 9").order_nr.should == 1
|
46
|
+
Category.find_by_name("Cat 6").order_nr.should == 2
|
47
|
+
Category.find_by_name("Cat 8").order_nr.should == 3
|
48
|
+
Category.find_by_name("Cat 7").order_nr.should == 4
|
49
|
+
Category.find_by_name("Cat 10").order_nr.should == 5
|
50
|
+
|
51
|
+
Category.find_by_name("Cat 9").move_to 5
|
52
|
+
|
53
|
+
Category.find_by_name("Cat 6").order_nr.should == 1
|
54
|
+
Category.find_by_name("Cat 8").order_nr.should == 2
|
55
|
+
Category.find_by_name("Cat 7").order_nr.should == 3
|
56
|
+
Category.find_by_name("Cat 10").order_nr.should == 4
|
57
|
+
Category.find_by_name("Cat 9").order_nr.should == 5
|
58
|
+
|
59
|
+
Category.find_by_name("Cat 10").move_to 2
|
60
|
+
|
61
|
+
Category.find_by_name("Cat 6").order_nr.should == 1
|
62
|
+
Category.find_by_name("Cat 10").order_nr.should == 2
|
63
|
+
Category.find_by_name("Cat 8").order_nr.should == 3
|
64
|
+
Category.find_by_name("Cat 7").order_nr.should == 4
|
65
|
+
Category.find_by_name("Cat 9").order_nr.should == 5
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should create unordered list and order it with order_unordered method" do
|
69
|
+
1.upto(4){|i| Category.create(:name => "Cat #{i}")}
|
70
|
+
cat = Category.create(:name => "Cat 0")
|
71
|
+
Category.update_all("order_nr = NULL")
|
72
|
+
Category.order_unordered
|
73
|
+
cat.order_nr.should_not == 1
|
74
|
+
cat.move_to 1
|
75
|
+
cat.reload
|
76
|
+
cat.order_nr.should == 1
|
77
|
+
Category.find_by_name("Cat 4").order_nr.should == 5
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should create scoped unordered list and order it with order_unordered method" do
|
81
|
+
cat_types = [
|
82
|
+
CatType.create(:name => "Type 1"),
|
83
|
+
CatType.create(:name => "Type 2")
|
84
|
+
]
|
85
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}", :cat_type => cat_types.first)}
|
86
|
+
5.times{|i| Category.create(:name => "Cat #{i+6}", :cat_type => cat_types.last)}
|
87
|
+
Category.update_all("order_nr = NULL")
|
88
|
+
Category.all.map(&:order_nr).should == [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
|
89
|
+
Category.order_unordered
|
90
|
+
Category.all.map(&:order_nr).should == [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should move_up" do
|
94
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
95
|
+
c = Category.find_by_name("Cat 2")
|
96
|
+
c.order_nr.should == 2
|
97
|
+
c.move_up
|
98
|
+
c.reload
|
99
|
+
c.order_nr.should == 1
|
100
|
+
c.move_up
|
101
|
+
c.reload
|
102
|
+
c.order_nr.should == 1
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should move_down" do
|
106
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
107
|
+
c = Category.find_by_name("Cat 2")
|
108
|
+
c.order_nr.should == 2
|
109
|
+
c.move_down
|
110
|
+
c.reload
|
111
|
+
c.order_nr.should == 3
|
112
|
+
c.move_to(5)
|
113
|
+
c.reload
|
114
|
+
c.order_nr.should == 5
|
115
|
+
c.move_down
|
116
|
+
c.reload
|
117
|
+
c.order_nr.should == 5
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should skip_callbacks" do
|
121
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
122
|
+
c = Category.find_by_name("Cat 2")
|
123
|
+
c.background_task.should be_nil
|
124
|
+
c.move_down
|
125
|
+
c.background_task.should == :done
|
126
|
+
c.background_task = nil
|
127
|
+
c.move_up(:skip_callbacks => true)
|
128
|
+
c.background_task.should be_nil
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should not reorder elements after repeated save" do
|
132
|
+
5.times{|i| Category.create(:name => "Cat #{i+1}")}
|
133
|
+
Category.first.order_nr.should == 1
|
134
|
+
Category.last.order_nr.should == 5
|
135
|
+
c = Category.new(:name => "Cat new")
|
136
|
+
c.must_resave = true
|
137
|
+
c.save
|
138
|
+
Category.first.order_nr.should == 1
|
139
|
+
Category.last.order_nr.should == 6
|
140
|
+
end
|
130
141
|
end
|
data/spec/spec.opts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
--colour
|
2
|
-
--reverse
|
1
|
+
--colour
|
2
|
+
--reverse
|
3
3
|
--backtrace
|
data/spec/spec_helper.rb
CHANGED
@@ -1,53 +1,61 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler'
|
3
|
-
begin
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
rescue Bundler::BundlerError => e
|
6
|
-
$stderr.puts e.message
|
7
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
-
exit e.status_code
|
9
|
-
end
|
10
|
-
|
11
|
-
require 'rails'
|
12
|
-
require 'active_record'
|
13
|
-
require 'rspec'
|
14
|
-
require 'logger'
|
15
|
-
require 'ruby-debug'
|
16
|
-
require 'ar-orderable'
|
17
|
-
ActiveRecord::Base.logger = Logger.new(File.open("#{File.dirname(__FILE__)}/database.log", 'w+'))
|
18
|
-
ActiveRecord::Base.establish_connection({ :database => ":memory:", :adapter => 'sqlite3', :timeout => 500 })
|
19
|
-
|
20
|
-
ActiveRecord::Schema.define do
|
21
|
-
create_table :categories, :force => true do |t|
|
22
|
-
t.string :name
|
23
|
-
t.integer :cat_type_id
|
24
|
-
t.integer :order_nr
|
25
|
-
end
|
26
|
-
create_table :cat_types, :force => true do |t|
|
27
|
-
t.string :name
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class CatType < ActiveRecord::Base
|
32
|
-
has_many :categories, :dependent => :destroy
|
33
|
-
end
|
34
|
-
|
35
|
-
class Category < ActiveRecord::Base
|
36
|
-
belongs_to :cat_type
|
37
|
-
acts_as_orderable :scope => :cat_type_id
|
38
|
-
after_save :do_background_task
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rails'
|
12
|
+
require 'active_record'
|
13
|
+
require 'rspec'
|
14
|
+
require 'logger'
|
15
|
+
require 'ruby-debug'
|
16
|
+
require 'ar-orderable'
|
17
|
+
ActiveRecord::Base.logger = Logger.new(File.open("#{File.dirname(__FILE__)}/database.log", 'w+'))
|
18
|
+
ActiveRecord::Base.establish_connection({ :database => ":memory:", :adapter => 'sqlite3', :timeout => 500 })
|
19
|
+
|
20
|
+
ActiveRecord::Schema.define do
|
21
|
+
create_table :categories, :force => true do |t|
|
22
|
+
t.string :name
|
23
|
+
t.integer :cat_type_id
|
24
|
+
t.integer :order_nr
|
25
|
+
end
|
26
|
+
create_table :cat_types, :force => true do |t|
|
27
|
+
t.string :name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class CatType < ActiveRecord::Base
|
32
|
+
has_many :categories, :dependent => :destroy
|
33
|
+
end
|
34
|
+
|
35
|
+
class Category < ActiveRecord::Base
|
36
|
+
belongs_to :cat_type
|
37
|
+
acts_as_orderable :scope => :cat_type_id
|
38
|
+
after_save :do_background_task
|
39
|
+
after_save :resave_record
|
40
|
+
attr_accessor :background_task, :must_resave
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def do_background_task
|
45
|
+
self.background_task = :done
|
46
|
+
end
|
47
|
+
|
48
|
+
def resave_record
|
49
|
+
if @must_resave
|
50
|
+
@must_resave = false
|
51
|
+
self.save!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
RSpec.configure do |config|
|
57
|
+
config.before(:each) do
|
58
|
+
Category.destroy_all
|
59
|
+
CatType.destroy_all
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-orderable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,12 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-08
|
14
|
-
default_executable:
|
13
|
+
date: 2011-11-08 00:00:00.000000000Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: rails
|
18
|
-
requirement: &
|
17
|
+
requirement: &17147740 !ruby/object:Gem::Requirement
|
19
18
|
none: false
|
20
19
|
requirements:
|
21
20
|
- - ! '>='
|
@@ -23,10 +22,10 @@ dependencies:
|
|
23
22
|
version: 3.0.0
|
24
23
|
type: :runtime
|
25
24
|
prerelease: false
|
26
|
-
version_requirements: *
|
25
|
+
version_requirements: *17147740
|
27
26
|
- !ruby/object:Gem::Dependency
|
28
27
|
name: sqlite3
|
29
|
-
requirement: &
|
28
|
+
requirement: &17146680 !ruby/object:Gem::Requirement
|
30
29
|
none: false
|
31
30
|
requirements:
|
32
31
|
- - ~>
|
@@ -34,10 +33,10 @@ dependencies:
|
|
34
33
|
version: 1.3.4
|
35
34
|
type: :development
|
36
35
|
prerelease: false
|
37
|
-
version_requirements: *
|
36
|
+
version_requirements: *17146680
|
38
37
|
- !ruby/object:Gem::Dependency
|
39
38
|
name: rspec
|
40
|
-
requirement: &
|
39
|
+
requirement: &17145080 !ruby/object:Gem::Requirement
|
41
40
|
none: false
|
42
41
|
requirements:
|
43
42
|
- - ~>
|
@@ -45,10 +44,10 @@ dependencies:
|
|
45
44
|
version: 2.6.0
|
46
45
|
type: :development
|
47
46
|
prerelease: false
|
48
|
-
version_requirements: *
|
47
|
+
version_requirements: *17145080
|
49
48
|
- !ruby/object:Gem::Dependency
|
50
49
|
name: ruby-debug19
|
51
|
-
requirement: &
|
50
|
+
requirement: &17144280 !ruby/object:Gem::Requirement
|
52
51
|
none: false
|
53
52
|
requirements:
|
54
53
|
- - ~>
|
@@ -56,10 +55,10 @@ dependencies:
|
|
56
55
|
version: 0.11.6
|
57
56
|
type: :development
|
58
57
|
prerelease: false
|
59
|
-
version_requirements: *
|
58
|
+
version_requirements: *17144280
|
60
59
|
- !ruby/object:Gem::Dependency
|
61
60
|
name: bundler
|
62
|
-
requirement: &
|
61
|
+
requirement: &17143240 !ruby/object:Gem::Requirement
|
63
62
|
none: false
|
64
63
|
requirements:
|
65
64
|
- - ~>
|
@@ -67,10 +66,10 @@ dependencies:
|
|
67
66
|
version: 1.0.0
|
68
67
|
type: :development
|
69
68
|
prerelease: false
|
70
|
-
version_requirements: *
|
69
|
+
version_requirements: *17143240
|
71
70
|
- !ruby/object:Gem::Dependency
|
72
71
|
name: jeweler
|
73
|
-
requirement: &
|
72
|
+
requirement: &17142480 !ruby/object:Gem::Requirement
|
74
73
|
none: false
|
75
74
|
requirements:
|
76
75
|
- - ~>
|
@@ -78,10 +77,10 @@ dependencies:
|
|
78
77
|
version: 1.6.4
|
79
78
|
type: :development
|
80
79
|
prerelease: false
|
81
|
-
version_requirements: *
|
80
|
+
version_requirements: *17142480
|
82
81
|
- !ruby/object:Gem::Dependency
|
83
82
|
name: rcov
|
84
|
-
requirement: &
|
83
|
+
requirement: &17141560 !ruby/object:Gem::Requirement
|
85
84
|
none: false
|
86
85
|
requirements:
|
87
86
|
- - ! '>='
|
@@ -89,7 +88,7 @@ dependencies:
|
|
89
88
|
version: '0'
|
90
89
|
type: :development
|
91
90
|
prerelease: false
|
92
|
-
version_requirements: *
|
91
|
+
version_requirements: *17141560
|
93
92
|
description: You can order AR records and skip callbacks
|
94
93
|
email: gatis@ithouse.cc
|
95
94
|
executables: []
|
@@ -110,7 +109,6 @@ files:
|
|
110
109
|
- spec/ar-orderable_spec.rb
|
111
110
|
- spec/spec.opts
|
112
111
|
- spec/spec_helper.rb
|
113
|
-
has_rdoc: true
|
114
112
|
homepage: http://github.com/ithouse/ar-orderable
|
115
113
|
licenses:
|
116
114
|
- MIT
|
@@ -126,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
124
|
version: '0'
|
127
125
|
segments:
|
128
126
|
- 0
|
129
|
-
hash: -
|
127
|
+
hash: -531530570232780643
|
130
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
129
|
none: false
|
132
130
|
requirements:
|
@@ -135,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
133
|
version: '0'
|
136
134
|
requirements: []
|
137
135
|
rubyforge_project:
|
138
|
-
rubygems_version: 1.
|
136
|
+
rubygems_version: 1.8.8
|
139
137
|
signing_key:
|
140
138
|
specification_version: 3
|
141
139
|
summary: Rails 3 plugin for simple ordering.
|