ranked-model 0.4.2 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5cfad22b2acf05bc3deda080d1bb11b96ade3c9d
4
- data.tar.gz: cd2d02de24cf6465ee4211818beb6696ec41e616
2
+ SHA256:
3
+ metadata.gz: 8dac9a4acefc5a44cfe74e77931854bb8edd031cc986f3bc0bd8232f543dc0f9
4
+ data.tar.gz: 9bd6120114a23ef47ae08581b860cd0cb4fa18779fb237b9b9570d94a889cc75
5
5
  SHA512:
6
- metadata.gz: fd1dbb2069c730aeda634d15cc4cd5a5fa0be8def4cca8db3b6bb320e40ef97f29f88bac16f2ba12720a1287cbb06ae3188e7862f5586599757fc2c56f6d3e6e
7
- data.tar.gz: b5f0896c2a4979b3f8d157bc20f3ee1bc7846ea6ef2ffb3432b50d5696e0852f803026bea3d0177c8db22b949c3d8c89022582b4c79da84c8ff8fc4a2611d3a6
6
+ metadata.gz: dc8d0ad9348de7596934a2eab69da78228c3351bd4d16303eae592b3893ecfedc28ff4620997977e8a88dc9d26dc27999c99e6e2abddc7ab6fe1f24aeb108527
7
+ data.tar.gz: 03a77db0fc8e9f59b4ffaccf24c966a54125141ab07a931046387e30cf62b7bf0577d9d983d268322dc69a7004c68c001168083d428cffff118832085afe3d21
data/.gitignore CHANGED
@@ -5,3 +5,4 @@ pkg/*
5
5
 
6
6
  Gemfile.lock
7
7
  *.gemfile.lock
8
+ .ruby-version
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
+ --warnings
2
3
  --format documentation
@@ -1,47 +1,51 @@
1
- sudo: false # use newer travis infrastructure
2
1
  language: ruby
3
2
  cache: bundler
4
3
  before_install:
5
- - gem install bundler -v 1.16.1
4
+ - gem update --system
5
+ - gem install bundler
6
6
  before_script:
7
7
  - mysql -e 'create database ranked_model_test;'
8
8
  - psql -c 'create database ranked_model_test;' -U postgres
9
9
  rvm:
10
- - 1.9.3
11
- - 2.1.10
12
- - 2.2.10
13
- - 2.3.7
14
- - 2.4.4
15
- - 2.5.1
10
+ - 2.4
11
+ - 2.5
12
+ - 2.6
13
+ - 2.7
14
+ - 3.0
16
15
  - jruby-9.1.17.0
17
- - rbx-3.107
18
16
  env:
19
17
  - DB=sqlite
20
18
  - DB=mysql
21
19
  - DB=postgresql
20
+ services:
21
+ - mysql
22
+ - postgresql
22
23
  gemfile:
23
- - gemfiles/rails_4_1.gemfile
24
24
  - gemfiles/rails_4_2.gemfile
25
25
  - gemfiles/rails_5_0.gemfile
26
26
  - gemfiles/rails_5_1.gemfile
27
27
  - gemfiles/rails_5_2.gemfile
28
+ - gemfiles/rails_6_0.gemfile
28
29
  matrix:
29
30
  exclude:
30
- - rvm: 1.9.3
31
+ # Rails <6 does not support Ruby 3, see:
32
+ # https://github.com/rails/rails/issues/40938#issuecomment-751898275
33
+ - rvm: 3.0
34
+ gemfile: gemfiles/rails_4_2.gemfile
35
+ - rvm: 3.0
31
36
  gemfile: gemfiles/rails_5_0.gemfile
32
- - rvm: 1.9.3
37
+ - rvm: 3.0
33
38
  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
39
+ - rvm: 3.0
41
40
  gemfile: gemfiles/rails_5_2.gemfile
41
+ # Ruby ≥2.7 uses a `bigdecimal` version that doesn't support BigDecimal.new
42
+ # that Rails 4.2 uses. See also:
43
+ # https://github.com/ruby/bigdecimal#which-version-should-you-select
44
+ - rvm: 2.7
45
+ gemfile: gemfiles/rails_4_2.gemfile
42
46
  - rvm: jruby-9.1.17.0
43
47
  gemfile: gemfiles/rails_5_2.gemfile
44
- - rvm: 2.4.4
45
- gemfile: gemfiles/rails_4_1.gemfile
46
- - rvm: 2.5.1
47
- gemfile: gemfiles/rails_4_1.gemfile
48
+ - rvm: 2.4
49
+ gemfile: gemfiles/rails_6_0.gemfile
50
+ - rvm: jruby-9.1.17.0
51
+ gemfile: gemfiles/rails_6_0.gemfile
data/Appraisals CHANGED
@@ -1,81 +1,72 @@
1
- appraise "rails-4-1" do
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.3.21", platform: :ruby
6
+ gem "mysql2", "~> 0.4.0", platform: :ruby
7
+ gem "jdbc-mysql", "~> 5.1.47", platform: :jruby
8
8
  gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
9
9
  end
10
10
  group :postgresql do
11
- gem "pg", "~> 0.18.0", platform: :ruby
11
+ gem "pg", "~> 0.18.4", platform: :ruby
12
12
  gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
13
13
  end
14
14
 
15
- gem "activerecord", "~> 4.1.16"
15
+ gem "activerecord", "~> 4.2.0"
16
16
  end
17
17
 
18
- appraise "rails-4-2" do
18
+ appraise "rails-5-0" do
19
19
  group :sqlite do
20
- gem "sqlite3", platform: :ruby
21
- gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platform: :jruby
20
+ gem "activerecord-jdbcsqlite3-adapter", "~> 50.0", platform: :jruby
22
21
  end
23
22
  group :mysql do
24
- gem "mysql2", "~> 0.4.10", platform: :ruby
25
- gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
23
+ gem "activerecord-jdbcmysql-adapter", "~> 50.0", platform: :jruby
26
24
  end
27
25
  group :postgresql do
28
- gem "pg", "~> 0.18.0", platform: :ruby
29
- gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
26
+ gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platform: :jruby
30
27
  end
31
28
 
32
- gem "activerecord", "~> 4.2.10"
29
+ gem "activerecord", "~> 5.0.0"
33
30
  end
34
31
 
35
- appraise "rails-5-0" do
32
+ appraise "rails-5-1" do
36
33
  group :sqlite do
37
- gem "sqlite3", platform: :ruby
38
- gem "activerecord-jdbcsqlite3-adapter", "~> 50.1", platform: :jruby
34
+ gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby
39
35
  end
40
36
  group :mysql do
41
- gem "mysql2", "~> 0.4.10", platform: :ruby
42
- gem "activerecord-jdbcmysql-adapter", "~> 50.1", platform: :jruby
37
+ gem "activerecord-jdbcmysql-adapter", "~> 51.0", platform: :jruby
43
38
  end
44
39
  group :postgresql do
45
- gem "pg", "~> 0.21.0", platform: :ruby
46
- gem "activerecord-jdbcpostgresql-adapter", "~> 50.1", platform: :jruby
40
+ gem "activerecord-jdbcpostgresql-adapter", "~> 51.0", platform: :jruby
47
41
  end
48
42
 
49
- gem "activerecord", "~> 5.0.7"
43
+ gem "activerecord", "~> 5.1.0"
50
44
  end
51
45
 
52
- appraise "rails-5-1" do
46
+ appraise "rails-5-2" do
53
47
  group :sqlite do
54
- gem "sqlite3", platform: :ruby
55
- gem "activerecord-jdbcsqlite3-adapter", "~> 51.1", platform: :jruby
48
+ gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby
56
49
  end
57
50
  group :mysql do
58
- gem "mysql2", "~> 0.4.10", platform: :ruby
59
- gem "activerecord-jdbcmysql-adapter", "~> 51.1", platform: :jruby
51
+ gem "activerecord-jdbcmysql-adapter", "~> 52.0", platform: :jruby
60
52
  end
61
53
  group :postgresql do
62
- gem "pg", "~> 0.21.0", platform: :ruby
63
- gem "activerecord-jdbcpostgresql-adapter", "~> 51.1", platform: :jruby
54
+ gem "activerecord-jdbcpostgresql-adapter", "~> 52.0", platform: :jruby
64
55
  end
65
56
 
66
- gem "activerecord", "~> 5.1.6"
57
+ gem "activerecord", "~> 5.2.0"
67
58
  end
68
59
 
69
- appraise "rails-5-2" do
60
+ appraise "rails-6-0" do
70
61
  group :sqlite do
71
- gem "sqlite3", platform: :ruby
62
+ gem "sqlite3", "~> 1.4", platform: :ruby
63
+ gem "activerecord-jdbcsqlite3-adapter", "~> 60.0", platform: :jruby
72
64
  end
73
65
  group :mysql do
74
- gem "mysql2", "~> 0.4.10", platform: :ruby
66
+ gem "activerecord-jdbcmysql-adapter", "~> 60.0", platform: :jruby
75
67
  end
76
68
  group :postgresql do
77
- gem "pg", "~> 0.21.0", platform: :ruby
69
+ gem "activerecord-jdbcpostgresql-adapter", "~> 60.0", platform: :jruby
78
70
  end
79
-
80
- gem "activerecord", "~> 5.2.0"
81
- end
71
+ gem "activerecord", "~> 6.0.0"
72
+ 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.2.0", platform: :ruby
12
+ end
13
+
14
+ group :mysql do
15
+ gem "mysql2", "~> 0.5.0", platform: :ruby
16
+ end
data/Readme.mkd CHANGED
@@ -1,13 +1,11 @@
1
- **ranked-model** is a modern row sorting library built for Rails 3, 4 and 5. It uses ARel aggressively and is better optimized than most other libraries.
1
+ **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.
2
2
 
3
3
  [![Build Status](https://travis-ci.org/mixonic/ranked-model.png)](https://travis-ci.org/mixonic/ranked-model)
4
4
 
5
5
  Installation
6
6
  ------------
7
7
 
8
- ranked-model passes specs with Rails 4.1, 4.2, 5.0, 5.1 and 5.2 for MySQL, Postgres, and SQLite on Ruby 1.9.3, 2.1 through 2.5, jruby-9.1.17.0, and rbx-3.107 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
-
10
- TL;DR, if you are using Rails 4 and up you are 100% good to go. Before Rails 4, be wary of Postgres.
8
+ ranked-model passes specs with Rails 4.2, 5.0, 5.1, 5.2 and 6.0 for MySQL, Postgres, and SQLite on Ruby 2.4 through 3.0 (with exceptions, please check the CI setup for supported combinations), and jruby-9.1.17.0 where Rails supports the platform.
11
9
 
12
10
  To install ranked-model, just add it to your `Gemfile`:
13
11
 
@@ -16,7 +14,7 @@ gem 'ranked-model'
16
14
 
17
15
  # Or pin ranked-model to git
18
16
  # gem 'ranked-model',
19
- # :git => 'git@github.com:mixonic/ranked-model.git'
17
+ # git: 'git@github.com:mixonic/ranked-model.git'
20
18
  ```
21
19
 
22
20
  Then use `bundle install` to update your `Gemfile.lock`.
@@ -31,6 +29,18 @@ class Duck < ActiveRecord::Base
31
29
  end
32
30
  ```
33
31
 
32
+ Give them an order (integer column):
33
+
34
+ ```bash
35
+ rails g migration AddRowOrderToDucks row_order:integer
36
+ rails db:migrate
37
+ ```
38
+
39
+ **IMPORTANT: The `_order` table column MUST allow null values. For the reason behind this requirement see [issue#167](https://github.com/mixonic/ranked-model/issues/167)**
40
+
41
+
42
+
43
+
34
44
  Put your ducks in a row:
35
45
 
36
46
  ``` ruby
@@ -42,7 +52,7 @@ class Duck < ActiveRecord::Base
42
52
  end
43
53
  ```
44
54
 
45
- This simple example assumes an integer column called `row_order`. To order Ducks by this order:
55
+ Order the Ducks by this order:
46
56
 
47
57
  ``` ruby
48
58
  Duck.rank(:row_order).all
@@ -52,7 +62,7 @@ The ranking integers stored in the `row_order` column will be big and spaced apa
52
62
  implement a sorting UI, just update the resource by appending the column name with `_position` and indicating the desired position:
53
63
 
54
64
  ``` ruby
55
- @duck.update_attribute :row_order_position, 0 # or 1, 2, 37. :first, :last, :up and :down are also valid
65
+ @duck.update row_order_position: 0 # or 1, 2, 37. :first, :last, :up and :down are also valid
56
66
  ```
57
67
 
58
68
  **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 +82,15 @@ $.ajax({
72
82
  });
73
83
  ```
74
84
 
85
+ 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.
86
+
87
+ 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.
88
+
89
+ ``` ruby
90
+ Duck.rank(:row_order).first.row_order_rank # => 0
91
+ Duck.rank(:row_order).third.row_order_rank # => 2
92
+ ```
93
+
75
94
  Complex Use
76
95
  -----------
77
96
 
@@ -83,19 +102,27 @@ class Duck < ActiveRecord::Base
83
102
  include RankedModel
84
103
 
85
104
  ranks :row_order, # Name this ranker, used with rank()
86
- :column => :sort_order # Override the default column, which defaults to the name
105
+ column: :sort_order # Override the default column, which defaults to the name
87
106
 
88
107
  belongs_to :pond
89
108
  ranks :swimming_order,
90
- :with_same => :pond_id # Ducks belong_to Ponds, make the ranker scoped to one pond
109
+ with_same: :pond_id # Ducks belong_to Ponds, make the ranker scoped to one pond
91
110
 
92
111
  ranks :row_order,
93
- :with_same => [:pond_id, :breed] # Lets rank them by breed
112
+ with_same: [:pond_id, :breed] # Lets rank them by breed
94
113
 
95
- scope :walking, where(:walking => true )
114
+ scope :walking, where(walking: true )
96
115
  ranks :walking_order,
97
- :scope => :walking # Narrow this ranker to a scope
116
+ scope: :walking # Narrow this ranker to a scope
117
+
118
+ belongs_to :parent, class_name: 'Duck', optional: true
119
+ ranks :child_order,
120
+ unless: :has_no_parent?, # Rank only ducks that have a parent. Alternatively a Proc or lambda can be passed, e.g. proc { parent.nil? }
121
+ with_same: :parent_id
98
122
 
123
+ def has_no_parent?
124
+ parent.nil?
125
+ end
99
126
  end
100
127
  ```
101
128
 
@@ -163,6 +190,35 @@ truck.row_order
163
190
  => 4194304
164
191
  ```
165
192
 
193
+ Migrations for existing data
194
+ ----------------------------
195
+
196
+ If you use `ranked_model` with existing data, the following migration (for Rails
197
+ 6) can be a starting point. Make sure to declare `include RankedModel` and
198
+ `ranks :row_order` in your `Duck` before running the migration.
199
+
200
+ ```bash
201
+ rails g migration AddRowOrderToDucks row_order:integer
202
+ ```
203
+
204
+ Then, adjust the migration:
205
+ ```ruby
206
+ # e.g. file db/migrate/20200325095038_add_row_order_to_ducks.rb
207
+ class AddRowOrderToDucks < ActiveRecord::Migration[6.0]
208
+ def change
209
+ add_column :ducks, :row_order, :integer
210
+
211
+ # Newest Duck shall rank "highest"" (be last).
212
+ Duck.update_all('row_order = EXTRACT(EPOCH FROM created_at)')
213
+
214
+ # Alternatively, implement any other sorting default
215
+ # Duck.order(created_at: :desc).desc.each do |duck|
216
+ # duck.update!(row_order: duck.created_at.to_i + duck.age / 2)
217
+ # end
218
+ end
219
+ end
220
+ ```
221
+
166
222
  Internals
167
223
  ---------
168
224
 
@@ -180,6 +236,8 @@ Contributing
180
236
 
181
237
  Fork, clone, write a test, write some code, commit, push, send a pull request. Github FTW!
182
238
 
239
+ The code is published under the [MIT License](LICENSE).
240
+
183
241
  The specs can be run with sqlite, postgres, and mysql:
184
242
 
185
243
  ```
@@ -2,21 +2,22 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 4.2.10"
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.0", platform: :ruby
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 "jdbc-mysql", "~> 5.1.47", platform: :jruby
20
+ gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
21
+ end
22
+
22
23
  gemspec path: "../"
@@ -2,21 +2,21 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 5.0.7"
5
+ gem "activerecord", "~> 5.0.0"
6
6
 
7
7
  group :sqlite do
8
- gem "sqlite3", platform: :ruby
9
- gem "activerecord-jdbcsqlite3-adapter", "~> 50.1", platform: :jruby
8
+ gem "sqlite3", "~> 1.3.13", platform: :ruby
9
+ gem "activerecord-jdbcsqlite3-adapter", "~> 50.0", platform: :jruby
10
10
  end
11
11
 
12
- group :mysql do
13
- gem "mysql2", "~> 0.4.10", platform: :ruby
14
- gem "activerecord-jdbcmysql-adapter", "~> 50.1", platform: :jruby
12
+ group :postgresql do
13
+ gem "pg", "~> 1.2.0", platform: :ruby
14
+ gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platform: :jruby
15
15
  end
16
16
 
17
- group :postgresql do
18
- gem "pg", "~> 0.21.0", platform: :ruby
19
- gem "activerecord-jdbcpostgresql-adapter", "~> 50.1", platform: :jruby
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: "../"
@@ -2,21 +2,21 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 5.1.6"
5
+ gem "activerecord", "~> 5.1.0"
6
6
 
7
7
  group :sqlite do
8
- gem "sqlite3", platform: :ruby
9
- gem "activerecord-jdbcsqlite3-adapter", "~> 51.1", platform: :jruby
8
+ gem "sqlite3", "~> 1.3.13", platform: :ruby
9
+ gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby
10
10
  end
11
11
 
12
- group :mysql do
13
- gem "mysql2", "~> 0.4.10", platform: :ruby
14
- gem "activerecord-jdbcmysql-adapter", "~> 51.1", platform: :jruby
12
+ group :postgresql do
13
+ gem "pg", "~> 1.2.0", platform: :ruby
14
+ gem "activerecord-jdbcpostgresql-adapter", "~> 51.0", platform: :jruby
15
15
  end
16
16
 
17
- group :postgresql do
18
- gem "pg", "~> 0.21.0", platform: :ruby
19
- gem "activerecord-jdbcpostgresql-adapter", "~> 51.1", platform: :jruby
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: "../"