ranked-model 0.4.6 → 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
2
  SHA256:
3
- metadata.gz: 608245c9f9eeed1d195c18efd1df0b32d5593465d581f269e20fb8b8867a1260
4
- data.tar.gz: 83a935d3b960f7acf48ada59f9e87962ab04b40d75c9af50b3ddf5dc6c5dfe5b
3
+ metadata.gz: 8dac9a4acefc5a44cfe74e77931854bb8edd031cc986f3bc0bd8232f543dc0f9
4
+ data.tar.gz: 9bd6120114a23ef47ae08581b860cd0cb4fa18779fb237b9b9570d94a889cc75
5
5
  SHA512:
6
- metadata.gz: 9ea50e38ff89431cfcf314144141f44b7aec03dc91aa8d45db24975f2a191bc0457c032511a8cc798fc10646ca29e8e35c011dcaed5adbf0d1344bb84e5469ef
7
- data.tar.gz: 9e2d0cadcc12b87d1602d40ef0eb8a6e2f23eff6270a841ffcdd2cd06ed74b49d40e8bfd9c49ace0680f08b73f06a3980de413aec425e2104b16fa5b5a9b7e71
6
+ metadata.gz: dc8d0ad9348de7596934a2eab69da78228c3351bd4d16303eae592b3893ecfedc28ff4620997977e8a88dc9d26dc27999c99e6e2abddc7ab6fe1f24aeb108527
7
+ data.tar.gz: 03a77db0fc8e9f59b4ffaccf24c966a54125141ab07a931046387e30cf62b7bf0577d9d983d268322dc69a7004c68c001168083d428cffff118832085afe3d21
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  cache: bundler
3
- sudo: false
4
3
  before_install:
5
4
  - gem update --system
6
5
  - gem install bundler
@@ -8,9 +7,11 @@ before_script:
8
7
  - mysql -e 'create database ranked_model_test;'
9
8
  - psql -c 'create database ranked_model_test;' -U postgres
10
9
  rvm:
11
- - 2.4.4
12
- - 2.5.1
13
- - 2.6.4
10
+ - 2.4
11
+ - 2.5
12
+ - 2.6
13
+ - 2.7
14
+ - 3.0
14
15
  - jruby-9.1.17.0
15
16
  env:
16
17
  - DB=sqlite
@@ -27,9 +28,24 @@ gemfile:
27
28
  - gemfiles/rails_6_0.gemfile
28
29
  matrix:
29
30
  exclude:
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
36
+ gemfile: gemfiles/rails_5_0.gemfile
37
+ - rvm: 3.0
38
+ gemfile: gemfiles/rails_5_1.gemfile
39
+ - rvm: 3.0
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
30
46
  - rvm: jruby-9.1.17.0
31
47
  gemfile: gemfiles/rails_5_2.gemfile
32
- - rvm: 2.4.4
48
+ - rvm: 2.4
33
49
  gemfile: gemfiles/rails_6_0.gemfile
34
50
  - rvm: jruby-9.1.17.0
35
51
  gemfile: gemfiles/rails_6_0.gemfile
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ group :sqlite do
8
8
  end
9
9
 
10
10
  group :postgresql do
11
- gem "pg", "~> 1.1.4", platform: :ruby
11
+ gem "pg", "~> 1.2.0", platform: :ruby
12
12
  end
13
13
 
14
14
  group :mysql do
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.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
-
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 :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.**
@@ -92,19 +102,27 @@ class Duck < ActiveRecord::Base
92
102
  include RankedModel
93
103
 
94
104
  ranks :row_order, # Name this ranker, used with rank()
95
- :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
96
106
 
97
107
  belongs_to :pond
98
108
  ranks :swimming_order,
99
- :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
100
110
 
101
111
  ranks :row_order,
102
- :with_same => [:pond_id, :breed] # Lets rank them by breed
112
+ with_same: [:pond_id, :breed] # Lets rank them by breed
103
113
 
104
- scope :walking, where(:walking => true )
114
+ scope :walking, where(walking: true )
105
115
  ranks :walking_order,
106
- :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
107
122
 
123
+ def has_no_parent?
124
+ parent.nil?
125
+ end
108
126
  end
109
127
  ```
110
128
 
@@ -172,6 +190,35 @@ truck.row_order
172
190
  => 4194304
173
191
  ```
174
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
+
175
222
  Internals
176
223
  ---------
177
224
 
@@ -189,6 +236,8 @@ Contributing
189
236
 
190
237
  Fork, clone, write a test, write some code, commit, push, send a pull request. Github FTW!
191
238
 
239
+ The code is published under the [MIT License](LICENSE).
240
+
192
241
  The specs can be run with sqlite, postgres, and mysql:
193
242
 
194
243
  ```
@@ -10,7 +10,7 @@ group :sqlite do
10
10
  end
11
11
 
12
12
  group :postgresql do
13
- gem "pg", "~> 1.1.4", platform: :ruby
13
+ gem "pg", "~> 1.2.0", platform: :ruby
14
14
  gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platform: :jruby
15
15
  end
16
16
 
@@ -10,7 +10,7 @@ group :sqlite do
10
10
  end
11
11
 
12
12
  group :postgresql do
13
- gem "pg", "~> 1.1.4", platform: :ruby
13
+ gem "pg", "~> 1.2.0", platform: :ruby
14
14
  gem "activerecord-jdbcpostgresql-adapter", "~> 51.0", platform: :jruby
15
15
  end
16
16
 
@@ -10,7 +10,7 @@ group :sqlite do
10
10
  end
11
11
 
12
12
  group :postgresql do
13
- gem "pg", "~> 1.1.4", platform: :ruby
13
+ gem "pg", "~> 1.2.0", platform: :ruby
14
14
  gem "activerecord-jdbcpostgresql-adapter", "~> 52.0", platform: :jruby
15
15
  end
16
16
 
@@ -10,7 +10,7 @@ group :sqlite do
10
10
  end
11
11
 
12
12
  group :postgresql do
13
- gem "pg", "~> 1.1.4", platform: :ruby
13
+ gem "pg", "~> 1.2.0", platform: :ruby
14
14
  gem "activerecord-jdbcpostgresql-adapter", "~> 60.0", platform: :jruby
15
15
  end
16
16
 
@@ -78,7 +78,9 @@ module RankedModel
78
78
  end
79
79
 
80
80
  def relative_rank
81
- finder.where("#{ranker.column} < #{rank}").count(:all)
81
+ escaped_column = instance_class.connection.quote_column_name ranker.column
82
+
83
+ finder.where("#{escaped_column} < #{rank}").count(:all)
82
84
  end
83
85
 
84
86
  def rank
@@ -187,7 +189,7 @@ module RankedModel
187
189
 
188
190
  def rearrange_ranks
189
191
  _scope = finder
190
- escaped_column = ActiveRecord::Base.connection.quote_column_name ranker.column
192
+ escaped_column = instance_class.connection.quote_column_name ranker.column
191
193
  # If there is room at the bottom of the list and we're added to the very top of the list...
192
194
  if current_first.rank && current_first.rank > RankedModel::MIN_RANK_VALUE && rank == RankedModel::MAX_RANK_VALUE
193
195
  # ...then move everyone else down 1 to make room for us at the end
@@ -1,3 +1,3 @@
1
1
  module RankedModel
2
- VERSION = "0.4.6"
2
+ VERSION = "0.4.7"
3
3
  end
@@ -28,4 +28,12 @@ describe Number do
28
28
 
29
29
  end
30
30
 
31
+ describe "getting a position with keyword column name" do
32
+
33
+ subject { Number.first }
34
+
35
+ its(:order_rank) { should == 0 }
36
+
37
+ end
38
+
31
39
  end
@@ -62,3 +62,21 @@ describe RankedModel::Ranker, 'unless as Proc' do
62
62
  }
63
63
  end
64
64
  end
65
+
66
+ describe RankedModel::Ranker, 'unless as lambda' do
67
+ context 'returns true' do
68
+ subject { RankedModel::Ranker.new(:overview, unless: ->(_) { true }).with(Class.new) }
69
+ its(:handle_ranking) { should == nil }
70
+ end
71
+
72
+ context 'returns false' do
73
+ subject { RankedModel::Ranker.new(:overview, unless: ->(_) { false }).with(Class.new) }
74
+
75
+ it {
76
+ subject.expects(:update_index_from_position).once
77
+ subject.expects(:assure_unique_position).once
78
+
79
+ subject.handle_ranking
80
+ }
81
+ end
82
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ranked-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Beale
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-16 00:00:00.000000000 Z
11
+ date: 2021-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord