ranked-model 0.4.2 → 0.4.5
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 +5 -5
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.travis.yml +11 -23
- data/Appraisals +26 -36
- data/Gemfile +12 -0
- data/Readme.mkd +11 -2
- data/gemfiles/rails_4_2.gemfile +8 -8
- data/gemfiles/rails_5_0.gemfile +9 -9
- data/gemfiles/rails_5_1.gemfile +9 -9
- data/gemfiles/rails_5_2.gemfile +8 -5
- data/gemfiles/rails_6_0.gemfile +22 -0
- data/lib/ranked-model/ranker.rb +14 -13
- data/lib/ranked-model/version.rb +1 -1
- data/lib/ranked-model.rb +16 -1
- data/ranked-model.gemspec +3 -3
- data/spec/duck-model/column_default_ducks_spec.rb +29 -0
- data/spec/duck-model/duck_spec.rb +107 -46
- data/spec/duck-model/lots_of_ducks_spec.rb +15 -15
- data/spec/ego-model/ego_spec.rb +3 -3
- data/spec/number-model/number_spec.rb +2 -2
- data/spec/player-model/records_already_exist_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- data/spec/sti-model/element_spec.rb +24 -24
- data/spec/sti-model/vehicle_spec.rb +4 -4
- data/spec/support/active_record.rb +11 -5
- metadata +10 -10
- data/gemfiles/rails_3_2.gemfile +0 -22
- data/gemfiles/rails_4_1.gemfile +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c342543b3341c81d62ceca340fc0966a3386a6115fc2ee36f309582995f8af52
|
|
4
|
+
data.tar.gz: 25408af509201219ce0539dbfe3324c8b3806bce367d3e00b80a2f15dc6e7dc4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 139bcf7c985b68e017e03bb3718a4377ec878ec7b249ac32ad20900d1467e5026a093e82725f8e39e1d8a378a7db0959f6a37f7571d1bccae40e9b8f70244a55
|
|
7
|
+
data.tar.gz: ca739746fb067e6de9ae52bcb08f352fcdf676a02063e04f5d3b5f3b8a54d773ff57227d5dc5fca7c6d402155920b3da39c73d83d495680ff988f67248fac321
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.travis.yml
CHANGED
|
@@ -1,47 +1,35 @@
|
|
|
1
|
-
sudo: false # use newer travis infrastructure
|
|
2
1
|
language: ruby
|
|
3
2
|
cache: bundler
|
|
3
|
+
sudo: false
|
|
4
4
|
before_install:
|
|
5
|
-
- gem
|
|
5
|
+
- gem update --system
|
|
6
|
+
- gem install bundler
|
|
6
7
|
before_script:
|
|
7
8
|
- mysql -e 'create database ranked_model_test;'
|
|
8
9
|
- psql -c 'create database ranked_model_test;' -U postgres
|
|
9
10
|
rvm:
|
|
10
|
-
- 1.9.3
|
|
11
|
-
- 2.1.10
|
|
12
|
-
- 2.2.10
|
|
13
|
-
- 2.3.7
|
|
14
11
|
- 2.4.4
|
|
15
12
|
- 2.5.1
|
|
13
|
+
- 2.6.4
|
|
16
14
|
- jruby-9.1.17.0
|
|
17
|
-
- rbx-3.107
|
|
18
15
|
env:
|
|
19
16
|
- DB=sqlite
|
|
20
17
|
- DB=mysql
|
|
21
18
|
- DB=postgresql
|
|
19
|
+
services:
|
|
20
|
+
- mysql
|
|
21
|
+
- postgresql
|
|
22
22
|
gemfile:
|
|
23
|
-
- gemfiles/rails_4_1.gemfile
|
|
24
23
|
- gemfiles/rails_4_2.gemfile
|
|
25
24
|
- gemfiles/rails_5_0.gemfile
|
|
26
25
|
- gemfiles/rails_5_1.gemfile
|
|
27
26
|
- gemfiles/rails_5_2.gemfile
|
|
27
|
+
- gemfiles/rails_6_0.gemfile
|
|
28
28
|
matrix:
|
|
29
29
|
exclude:
|
|
30
|
-
- rvm: 1.9.3
|
|
31
|
-
gemfile: gemfiles/rails_5_0.gemfile
|
|
32
|
-
- rvm: 1.9.3
|
|
33
|
-
gemfile: gemfiles/rails_5_1.gemfile
|
|
34
|
-
- rvm: 1.9.3
|
|
35
|
-
gemfile: gemfiles/rails_5_2.gemfile
|
|
36
|
-
- rvm: 2.1.10
|
|
37
|
-
gemfile: gemfiles/rails_5_0.gemfile
|
|
38
|
-
- rvm: 2.1.10
|
|
39
|
-
gemfile: gemfiles/rails_5_1.gemfile
|
|
40
|
-
- rvm: 2.1.10
|
|
41
|
-
gemfile: gemfiles/rails_5_2.gemfile
|
|
42
30
|
- rvm: jruby-9.1.17.0
|
|
43
31
|
gemfile: gemfiles/rails_5_2.gemfile
|
|
44
32
|
- rvm: 2.4.4
|
|
45
|
-
gemfile: gemfiles/
|
|
46
|
-
- rvm:
|
|
47
|
-
gemfile: gemfiles/
|
|
33
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
|
34
|
+
- rvm: jruby-9.1.17.0
|
|
35
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
data/Appraisals
CHANGED
|
@@ -1,81 +1,71 @@
|
|
|
1
|
-
appraise "rails-4-
|
|
1
|
+
appraise "rails-4-2" do
|
|
2
2
|
group :sqlite do
|
|
3
|
-
gem "sqlite3", platform: :ruby
|
|
4
3
|
gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platform: :jruby
|
|
5
4
|
end
|
|
6
5
|
group :mysql do
|
|
7
|
-
gem "mysql2", "~> 0.
|
|
6
|
+
gem "mysql2", "~> 0.4.0", platform: :ruby
|
|
8
7
|
gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
|
|
9
8
|
end
|
|
10
9
|
group :postgresql do
|
|
11
|
-
gem "pg", "~> 0.18.
|
|
10
|
+
gem "pg", "~> 0.18.4", platform: :ruby
|
|
12
11
|
gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
|
|
13
12
|
end
|
|
14
13
|
|
|
15
|
-
gem "activerecord", "~> 4.
|
|
14
|
+
gem "activerecord", "~> 4.2.0"
|
|
16
15
|
end
|
|
17
16
|
|
|
18
|
-
appraise "rails-
|
|
17
|
+
appraise "rails-5-0" do
|
|
19
18
|
group :sqlite do
|
|
20
|
-
gem "
|
|
21
|
-
gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platform: :jruby
|
|
19
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 50.0", platform: :jruby
|
|
22
20
|
end
|
|
23
21
|
group :mysql do
|
|
24
|
-
gem "
|
|
25
|
-
gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
|
|
22
|
+
gem "activerecord-jdbcmysql-adapter", "~> 50.0", platform: :jruby
|
|
26
23
|
end
|
|
27
24
|
group :postgresql do
|
|
28
|
-
gem "
|
|
29
|
-
gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
|
|
25
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platform: :jruby
|
|
30
26
|
end
|
|
31
27
|
|
|
32
|
-
gem "activerecord", "~>
|
|
28
|
+
gem "activerecord", "~> 5.0.0"
|
|
33
29
|
end
|
|
34
30
|
|
|
35
|
-
appraise "rails-5-
|
|
31
|
+
appraise "rails-5-1" do
|
|
36
32
|
group :sqlite do
|
|
37
|
-
gem "
|
|
38
|
-
gem "activerecord-jdbcsqlite3-adapter", "~> 50.1", platform: :jruby
|
|
33
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby
|
|
39
34
|
end
|
|
40
35
|
group :mysql do
|
|
41
|
-
gem "
|
|
42
|
-
gem "activerecord-jdbcmysql-adapter", "~> 50.1", platform: :jruby
|
|
36
|
+
gem "activerecord-jdbcmysql-adapter", "~> 51.0", platform: :jruby
|
|
43
37
|
end
|
|
44
38
|
group :postgresql do
|
|
45
|
-
gem "
|
|
46
|
-
gem "activerecord-jdbcpostgresql-adapter", "~> 50.1", platform: :jruby
|
|
39
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 51.0", platform: :jruby
|
|
47
40
|
end
|
|
48
41
|
|
|
49
|
-
gem "activerecord", "~> 5.0
|
|
42
|
+
gem "activerecord", "~> 5.1.0"
|
|
50
43
|
end
|
|
51
44
|
|
|
52
|
-
appraise "rails-5-
|
|
45
|
+
appraise "rails-5-2" do
|
|
53
46
|
group :sqlite do
|
|
54
|
-
gem "
|
|
55
|
-
gem "activerecord-jdbcsqlite3-adapter", "~> 51.1", platform: :jruby
|
|
47
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby
|
|
56
48
|
end
|
|
57
49
|
group :mysql do
|
|
58
|
-
gem "
|
|
59
|
-
gem "activerecord-jdbcmysql-adapter", "~> 51.1", platform: :jruby
|
|
50
|
+
gem "activerecord-jdbcmysql-adapter", "~> 52.0", platform: :jruby
|
|
60
51
|
end
|
|
61
52
|
group :postgresql do
|
|
62
|
-
gem "
|
|
63
|
-
gem "activerecord-jdbcpostgresql-adapter", "~> 51.1", platform: :jruby
|
|
53
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0", platform: :jruby
|
|
64
54
|
end
|
|
65
55
|
|
|
66
|
-
gem "activerecord", "~> 5.
|
|
56
|
+
gem "activerecord", "~> 5.2.0"
|
|
67
57
|
end
|
|
68
58
|
|
|
69
|
-
appraise "rails-
|
|
59
|
+
appraise "rails-6-0" do
|
|
70
60
|
group :sqlite do
|
|
71
|
-
gem "sqlite3", platform: :ruby
|
|
61
|
+
gem "sqlite3", "~> 1.4", platform: :ruby
|
|
62
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 60.0", platform: :jruby
|
|
72
63
|
end
|
|
73
64
|
group :mysql do
|
|
74
|
-
gem "
|
|
65
|
+
gem "activerecord-jdbcmysql-adapter", "~> 60.0", platform: :jruby
|
|
75
66
|
end
|
|
76
67
|
group :postgresql do
|
|
77
|
-
gem "
|
|
68
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 60.0", platform: :jruby
|
|
78
69
|
end
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
end
|
|
70
|
+
gem "activerecord", "~> 6.0.0"
|
|
71
|
+
end
|
data/Gemfile
CHANGED
|
@@ -2,3 +2,15 @@ source "https://rubygems.org"
|
|
|
2
2
|
|
|
3
3
|
# Specify your gem's dependencies in ranked-model.gemspec
|
|
4
4
|
gemspec
|
|
5
|
+
|
|
6
|
+
group :sqlite do
|
|
7
|
+
gem "sqlite3", "~> 1.3.13", platform: :ruby
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
group :postgresql do
|
|
11
|
+
gem "pg", "~> 1.1.4", platform: :ruby
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
group :mysql do
|
|
15
|
+
gem "mysql2", "~> 0.5.0", platform: :ruby
|
|
16
|
+
end
|
data/Readme.mkd
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
Installation
|
|
6
6
|
------------
|
|
7
7
|
|
|
8
|
-
ranked-model passes specs with Rails 4.
|
|
8
|
+
ranked-model passes specs with Rails 4.2, 5.0, 5.1, 5.2, 6.0 for MySQL, Postgres, and SQLite on Ruby 2.4 through 2.6, and jruby-9.1.17.0 where Rails supports the platform. This is with the exception of Postgres before Rails 4.0 on all platforms, which is unsupported by `ranked-model`. Note that the `pg` gem has pulled support for rbx (Rubinius) from version 1 onward.
|
|
9
9
|
|
|
10
10
|
TL;DR, if you are using Rails 4 and up you are 100% good to go. Before Rails 4, be wary of Postgres.
|
|
11
11
|
|
|
@@ -52,7 +52,7 @@ The ranking integers stored in the `row_order` column will be big and spaced apa
|
|
|
52
52
|
implement a sorting UI, just update the resource by appending the column name with `_position` and indicating the desired position:
|
|
53
53
|
|
|
54
54
|
``` ruby
|
|
55
|
-
@duck.
|
|
55
|
+
@duck.update :row_order_position => 0 # or 1, 2, 37. :first, :last, :up and :down are also valid
|
|
56
56
|
```
|
|
57
57
|
|
|
58
58
|
**IMPORTANT: Note that you MUST append _position to the column name when setting a new position on an instance. This is a fake column that can take relative as well as absolute index-based values for position.**
|
|
@@ -72,6 +72,15 @@ $.ajax({
|
|
|
72
72
|
});
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
+
If you need to find the rank of an item with respect to other ranked items, you can use the `{column_name}_rank` method on the model instance. `{column_name}` is your resource ranking column.
|
|
76
|
+
|
|
77
|
+
Following on from our examples above, the `row_order_rank` method will return the position of the duck object in the list with respect to the order defined by the row_order column.
|
|
78
|
+
|
|
79
|
+
``` ruby
|
|
80
|
+
Duck.rank(:row_order).first.row_order_rank # => 0
|
|
81
|
+
Duck.rank(:row_order).third.row_order_rank # => 2
|
|
82
|
+
```
|
|
83
|
+
|
|
75
84
|
Complex Use
|
|
76
85
|
-----------
|
|
77
86
|
|
data/gemfiles/rails_4_2.gemfile
CHANGED
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "activerecord", "~> 4.2.
|
|
5
|
+
gem "activerecord", "~> 4.2.0"
|
|
6
6
|
|
|
7
7
|
group :sqlite do
|
|
8
|
-
gem "sqlite3", platform: :ruby
|
|
8
|
+
gem "sqlite3", "~> 1.3.13", platform: :ruby
|
|
9
9
|
gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platform: :jruby
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
group :mysql do
|
|
13
|
-
gem "mysql2", "~> 0.4.10", platform: :ruby
|
|
14
|
-
gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
|
|
15
|
-
end
|
|
16
|
-
|
|
17
12
|
group :postgresql do
|
|
18
|
-
gem "pg", "~> 0.18.
|
|
13
|
+
gem "pg", "~> 0.18.4", platform: :ruby
|
|
19
14
|
gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
|
|
20
15
|
end
|
|
21
16
|
|
|
17
|
+
group :mysql do
|
|
18
|
+
gem "mysql2", "~> 0.4.0", platform: :ruby
|
|
19
|
+
gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
|
|
20
|
+
end
|
|
21
|
+
|
|
22
22
|
gemspec path: "../"
|
data/gemfiles/rails_5_0.gemfile
CHANGED
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "activerecord", "~> 5.0.
|
|
5
|
+
gem "activerecord", "~> 5.0.0"
|
|
6
6
|
|
|
7
7
|
group :sqlite do
|
|
8
|
-
gem "sqlite3", platform: :ruby
|
|
9
|
-
gem "activerecord-jdbcsqlite3-adapter", "~> 50.
|
|
8
|
+
gem "sqlite3", "~> 1.3.13", platform: :ruby
|
|
9
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 50.0", platform: :jruby
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
group :
|
|
13
|
-
gem "
|
|
14
|
-
gem "activerecord-
|
|
12
|
+
group :postgresql do
|
|
13
|
+
gem "pg", "~> 1.1.4", platform: :ruby
|
|
14
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platform: :jruby
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
group :
|
|
18
|
-
gem "
|
|
19
|
-
gem "activerecord-
|
|
17
|
+
group :mysql do
|
|
18
|
+
gem "mysql2", "~> 0.5.0", platform: :ruby
|
|
19
|
+
gem "activerecord-jdbcmysql-adapter", "~> 50.0", platform: :jruby
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
gemspec path: "../"
|
data/gemfiles/rails_5_1.gemfile
CHANGED
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "activerecord", "~> 5.1.
|
|
5
|
+
gem "activerecord", "~> 5.1.0"
|
|
6
6
|
|
|
7
7
|
group :sqlite do
|
|
8
|
-
gem "sqlite3", platform: :ruby
|
|
9
|
-
gem "activerecord-jdbcsqlite3-adapter", "~> 51.
|
|
8
|
+
gem "sqlite3", "~> 1.3.13", platform: :ruby
|
|
9
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
group :
|
|
13
|
-
gem "
|
|
14
|
-
gem "activerecord-
|
|
12
|
+
group :postgresql do
|
|
13
|
+
gem "pg", "~> 1.1.4", platform: :ruby
|
|
14
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 51.0", platform: :jruby
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
group :
|
|
18
|
-
gem "
|
|
19
|
-
gem "activerecord-
|
|
17
|
+
group :mysql do
|
|
18
|
+
gem "mysql2", "~> 0.5.0", platform: :ruby
|
|
19
|
+
gem "activerecord-jdbcmysql-adapter", "~> 51.0", platform: :jruby
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
gemspec path: "../"
|
data/gemfiles/rails_5_2.gemfile
CHANGED
|
@@ -5,15 +5,18 @@ source "https://rubygems.org"
|
|
|
5
5
|
gem "activerecord", "~> 5.2.0"
|
|
6
6
|
|
|
7
7
|
group :sqlite do
|
|
8
|
-
gem "sqlite3", platform: :ruby
|
|
8
|
+
gem "sqlite3", "~> 1.3.13", platform: :ruby
|
|
9
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby
|
|
9
10
|
end
|
|
10
11
|
|
|
11
|
-
group :
|
|
12
|
-
gem "
|
|
12
|
+
group :postgresql do
|
|
13
|
+
gem "pg", "~> 1.1.4", platform: :ruby
|
|
14
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0", platform: :jruby
|
|
13
15
|
end
|
|
14
16
|
|
|
15
|
-
group :
|
|
16
|
-
gem "
|
|
17
|
+
group :mysql do
|
|
18
|
+
gem "mysql2", "~> 0.5.0", platform: :ruby
|
|
19
|
+
gem "activerecord-jdbcmysql-adapter", "~> 52.0", platform: :jruby
|
|
17
20
|
end
|
|
18
21
|
|
|
19
22
|
gemspec path: "../"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "activerecord", "~> 6.0.0"
|
|
6
|
+
|
|
7
|
+
group :sqlite do
|
|
8
|
+
gem "sqlite3", "~> 1.4", platform: :ruby
|
|
9
|
+
gem "activerecord-jdbcsqlite3-adapter", "~> 60.0", platform: :jruby
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
group :postgresql do
|
|
13
|
+
gem "pg", "~> 1.1.4", platform: :ruby
|
|
14
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 60.0", platform: :jruby
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
group :mysql do
|
|
18
|
+
gem "mysql2", "~> 0.5.0", platform: :ruby
|
|
19
|
+
gem "activerecord-jdbcmysql-adapter", "~> 60.0", platform: :jruby
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
gemspec path: "../"
|
data/lib/ranked-model/ranker.rb
CHANGED
|
@@ -73,6 +73,10 @@ module RankedModel
|
|
|
73
73
|
instance.send "#{ranker.name}_position"
|
|
74
74
|
end
|
|
75
75
|
|
|
76
|
+
def relative_rank
|
|
77
|
+
finder.where("#{ranker.column} < #{rank}").count(:all)
|
|
78
|
+
end
|
|
79
|
+
|
|
76
80
|
def rank
|
|
77
81
|
instance.send "#{ranker.column}"
|
|
78
82
|
end
|
|
@@ -171,7 +175,7 @@ module RankedModel
|
|
|
171
175
|
|
|
172
176
|
def assure_unique_position
|
|
173
177
|
if ( new_record? || rank_changed? )
|
|
174
|
-
if (rank > RankedModel::MAX_RANK_VALUE) ||
|
|
178
|
+
if (rank > RankedModel::MAX_RANK_VALUE) || rank_taken?
|
|
175
179
|
rearrange_ranks
|
|
176
180
|
end
|
|
177
181
|
end
|
|
@@ -205,12 +209,14 @@ module RankedModel
|
|
|
205
209
|
end
|
|
206
210
|
|
|
207
211
|
def rebalance_ranks
|
|
208
|
-
if rank
|
|
212
|
+
if rank && instance.persisted?
|
|
209
213
|
origin = current_order.index { |item| item.instance.id == instance.id }
|
|
210
|
-
|
|
211
|
-
|
|
214
|
+
if origin
|
|
215
|
+
destination = current_order.index { |item| rank <= item.rank }
|
|
216
|
+
destination -= 1 if origin < destination
|
|
212
217
|
|
|
213
|
-
|
|
218
|
+
current_order.insert destination, current_order.delete_at(origin)
|
|
219
|
+
end
|
|
214
220
|
end
|
|
215
221
|
|
|
216
222
|
gaps = current_order.size + 1
|
|
@@ -256,7 +262,7 @@ module RankedModel
|
|
|
256
262
|
_finder = _finder.where.not instance_class.primary_key.to_sym => instance.id
|
|
257
263
|
end
|
|
258
264
|
|
|
259
|
-
_finder.
|
|
265
|
+
_finder.reorder(ranker.column.to_sym => order).select(columns)
|
|
260
266
|
end
|
|
261
267
|
end
|
|
262
268
|
|
|
@@ -286,13 +292,8 @@ module RankedModel
|
|
|
286
292
|
end
|
|
287
293
|
end
|
|
288
294
|
|
|
289
|
-
def
|
|
290
|
-
|
|
291
|
-
except( :order ).
|
|
292
|
-
where( ranker.column => _rank ).
|
|
293
|
-
first)
|
|
294
|
-
RankedModel::Ranker::Mapper.new ranker, ordered_instance
|
|
295
|
-
end
|
|
295
|
+
def rank_taken?
|
|
296
|
+
finder.except(:order).where(ranker.column => rank).exists?
|
|
296
297
|
end
|
|
297
298
|
|
|
298
299
|
def neighbors_at_position _pos
|
data/lib/ranked-model/version.rb
CHANGED
data/lib/ranked-model.rb
CHANGED
|
@@ -3,6 +3,8 @@ require File.dirname(__FILE__)+'/ranked-model/railtie' if defined?(Rails::Railti
|
|
|
3
3
|
|
|
4
4
|
module RankedModel
|
|
5
5
|
|
|
6
|
+
class NonNilColumnDefault < StandardError; end
|
|
7
|
+
|
|
6
8
|
# Signed INT in MySQL
|
|
7
9
|
#
|
|
8
10
|
MAX_RANK_VALUE = 2147483647
|
|
@@ -18,7 +20,7 @@ module RankedModel
|
|
|
18
20
|
before_save :handle_ranking
|
|
19
21
|
|
|
20
22
|
scope :rank, lambda { |name|
|
|
21
|
-
|
|
23
|
+
reorder ranker(name.to_sym).column
|
|
22
24
|
}
|
|
23
25
|
end
|
|
24
26
|
|
|
@@ -45,6 +47,11 @@ module RankedModel
|
|
|
45
47
|
def ranks *args
|
|
46
48
|
self.rankers ||= []
|
|
47
49
|
ranker = RankedModel::Ranker.new(*args)
|
|
50
|
+
|
|
51
|
+
if column_default(ranker)
|
|
52
|
+
raise NonNilColumnDefault, %Q{Your ranked model column "#{ranker.name}" must not have a default value in the database.}
|
|
53
|
+
end
|
|
54
|
+
|
|
48
55
|
self.rankers << ranker
|
|
49
56
|
attr_reader "#{ranker.name}_position"
|
|
50
57
|
define_method "#{ranker.name}_position=" do |position|
|
|
@@ -54,9 +61,17 @@ module RankedModel
|
|
|
54
61
|
end
|
|
55
62
|
end
|
|
56
63
|
|
|
64
|
+
define_method "#{ranker.name}_rank" do
|
|
65
|
+
ranker.with(self).relative_rank
|
|
66
|
+
end
|
|
67
|
+
|
|
57
68
|
public "#{ranker.name}_position", "#{ranker.name}_position="
|
|
58
69
|
end
|
|
59
70
|
|
|
71
|
+
def column_default ranker
|
|
72
|
+
column_defaults[ranker.name.to_s] if ActiveRecord::Base.connected? && table_exists?
|
|
73
|
+
end
|
|
74
|
+
|
|
60
75
|
end
|
|
61
76
|
|
|
62
77
|
end
|
data/ranked-model.gemspec
CHANGED
|
@@ -9,11 +9,11 @@ Gem::Specification.new do |s|
|
|
|
9
9
|
s.authors = ["Matthew Beale"]
|
|
10
10
|
s.email = ["matt.beale@madhatted.com"]
|
|
11
11
|
s.homepage = "https://github.com/mixonic/ranked-model"
|
|
12
|
-
s.summary = %q{An acts_as_sortable replacement built for Rails
|
|
13
|
-
s.description = %q{ranked-model is a modern row sorting library built for Rails
|
|
12
|
+
s.summary = %q{An acts_as_sortable replacement built for Rails 4.2+}
|
|
13
|
+
s.description = %q{ranked-model is a modern row sorting library built for Rails 4.2+. It uses ARel aggressively and is better optimized than most other libraries.}
|
|
14
14
|
s.license = 'MIT'
|
|
15
15
|
|
|
16
|
-
s.add_dependency "activerecord", ">= 4.
|
|
16
|
+
s.add_dependency "activerecord", ">= 4.2"
|
|
17
17
|
s.add_development_dependency "rspec", "~> 3"
|
|
18
18
|
s.add_development_dependency "rspec-its"
|
|
19
19
|
s.add_development_dependency "mocha"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'ColumnDefaultDuck' do
|
|
4
|
+
|
|
5
|
+
it "should raise an error if we try to initialise ranked_model on a column with a default value" do
|
|
6
|
+
expect {
|
|
7
|
+
class ColumnDefaultDuck < ActiveRecord::Base
|
|
8
|
+
include RankedModel
|
|
9
|
+
ranks :size, :with_same => :pond
|
|
10
|
+
end
|
|
11
|
+
}.to raise_error(RankedModel::NonNilColumnDefault, 'Your ranked model column "size" must not have a default value in the database.')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should not raise an error if we don't have a database connection when checking for default value" do
|
|
15
|
+
begin
|
|
16
|
+
ActiveRecord::Base.remove_connection
|
|
17
|
+
|
|
18
|
+
expect {
|
|
19
|
+
class ColumnDefaultDuck < ActiveRecord::Base
|
|
20
|
+
include RankedModel
|
|
21
|
+
ranks :size, :with_same => :pond
|
|
22
|
+
end
|
|
23
|
+
}.not_to raise_error
|
|
24
|
+
ensure
|
|
25
|
+
ActiveRecord::Base.establish_connection(ENV['DB'].to_sym)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|