Skip to content

Commit

Permalink
Set _position after updating rank order with inferred value (#193)
Browse files Browse the repository at this point in the history
* Set _position after updating the rank order due to a non-integer position setting

* Fix for dynamic method names in tests

* Update needs to be of row_position, not row column itself

* Use previously_changed? because this is after_save

* Adjust Appraisals, gemspec and CI matrix for Rails 5.2+ only support

* Rails 7 requires Ruby 2.7+

* Try setting position to relative rank in rank_at if needed

* Remove unnecessary additional callback

* Handle rebalance rank setting (happens before position is set)

* Don't use rank_at when rebalancing, instead, mutate the column directly

---------

Co-authored-by: Brendon Muir <brendon@spike.net.nz>
  • Loading branch information
CodingAnarchy and brendon committed May 2, 2023
1 parent 680a8e4 commit f9145d4
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 144 deletions.
22 changes: 1 addition & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ jobs:
- '3.0'
- 3.1
gemfile:
- gemfiles/rails_4_2.gemfile
- gemfiles/rails_5_0.gemfile
- gemfiles/rails_5_1.gemfile
- gemfiles/rails_5_2.gemfile
- gemfiles/rails_6_0.gemfile
- gemfiles/rails_6_1.gemfile
Expand All @@ -39,25 +36,8 @@ jobs:
gemfile: gemfiles/rails_7_0.gemfile
- ruby: 2.6
gemfile: gemfiles/rails_7_0.gemfile
- ruby: 2.7
gemfile: gemfiles/rails_4_2.gemfile
- ruby: 2.7
gemfile: gemfiles/rails_5_0.gemfile
db: sqlite
- ruby: '3.0'
gemfile: gemfiles/rails_4_2.gemfile
- ruby: '3.0'
gemfile: gemfiles/rails_5_0.gemfile
- ruby: '3.0'
gemfile: gemfiles/rails_5_1.gemfile
- ruby: '3.0'
gemfile: gemfiles/rails_5_2.gemfile
- ruby: 3.1
gemfile: gemfiles/rails_4_2.gemfile
- ruby: 3.1
gemfile: gemfiles/rails_5_0.gemfile
- ruby: 3.1
gemfile: gemfiles/rails_5_1.gemfile
- ruby: 3.1
gemfile: gemfiles/rails_5_2.gemfile
- ruby: 3.1
Expand Down Expand Up @@ -107,4 +87,4 @@ jobs:
run: |
psql -c 'create database ranked_model_test;' -h localhost -U postgres
- name: Run tests
run: bundle exec rake
run: bundle exec rake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pkg/*
*.gem
.bundle
*.un~
*.swp

Gemfile.lock
*.gemfile.lock
Expand Down
59 changes: 14 additions & 45 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
appraise "rails-4-2" do
group :sqlite do
gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platform: :jruby
end
group :mysql do
gem "mysql2", "~> 0.4.0", platform: :ruby
gem "jdbc-mysql", "~> 5.1.47", platform: :jruby
gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
end
group :postgresql do
gem "pg", "~> 0.18.4", platform: :ruby
gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
end

gem "activerecord", "~> 4.2.0"
end

appraise "rails-5-0" do
group :sqlite do
gem "activerecord-jdbcsqlite3-adapter", "~> 50.0", platform: :jruby
end
group :mysql do
gem "activerecord-jdbcmysql-adapter", "~> 50.0", platform: :jruby
end
group :postgresql do
gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platform: :jruby
end

gem "activerecord", "~> 5.0.0"
end

appraise "rails-5-1" do
group :sqlite do
gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby
end
group :mysql do
gem "activerecord-jdbcmysql-adapter", "~> 51.0", platform: :jruby
end
group :postgresql do
gem "activerecord-jdbcpostgresql-adapter", "~> 51.0", platform: :jruby
end

gem "activerecord", "~> 5.1.0"
end

appraise "rails-5-2" do
group :sqlite do
gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby
Expand Down Expand Up @@ -84,3 +39,17 @@ appraise "rails-6-1" do
end
gem "activerecord", "~> 6.1.0"
end

appraise "rails-7-0" do
group :sqlite do
gem "sqlite3", "~> 1.4", platform: :ruby
gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby
end
group :mysql do
gem "activerecord-jdbcmysql-adapter", "~> 61.0", platform: :jruby
end
group :postgresql do
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0", platform: :jruby
end
gem "activerecord", "~> 7.0.0"
end
23 changes: 0 additions & 23 deletions gemfiles/rails_4_2.gemfile

This file was deleted.

22 changes: 0 additions & 22 deletions gemfiles/rails_5_0.gemfile

This file was deleted.

22 changes: 0 additions & 22 deletions gemfiles/rails_5_1.gemfile

This file was deleted.

6 changes: 3 additions & 3 deletions gemfiles/rails_5_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "https://rubygems.org"
gem "activerecord", "~> 5.2.0"

group :sqlite do
gem "sqlite3", "~> 1.3.13", platform: :ruby
gem "sqlite3", platform: :ruby
gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby
end

group :postgresql do
gem "pg", "~> 1.2.0", platform: :ruby
gem "pg", platform: :ruby
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0", platform: :jruby
end

group :mysql do
gem "mysql2", "~> 0.5.0", platform: :ruby
gem "mysql2", platform: :ruby
gem "activerecord-jdbcmysql-adapter", "~> 52.0", platform: :jruby
end

Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ group :sqlite do
end

group :postgresql do
gem "pg", "~> 1.2.0", platform: :ruby
gem "pg", platform: :ruby
gem "activerecord-jdbcpostgresql-adapter", "~> 60.0", platform: :jruby
end

group :mysql do
gem "mysql2", "~> 0.5.0", platform: :ruby
gem "mysql2", platform: :ruby
gem "activerecord-jdbcmysql-adapter", "~> 60.0", platform: :jruby
end

Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ group :sqlite do
end

group :postgresql do
gem "pg", "~> 1.2.0", platform: :ruby
gem "pg", platform: :ruby
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0", platform: :jruby
end

group :mysql do
gem "mysql2", "~> 0.5.0", platform: :ruby
gem "mysql2", platform: :ruby
gem "activerecord-jdbcmysql-adapter", "~> 61.0", platform: :jruby
end

Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_7_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ group :sqlite do
end

group :postgresql do
gem "pg", "~> 1.3.0", platform: :ruby
gem "pg", platform: :ruby
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0", platform: :jruby
end

group :mysql do
gem "mysql2", "~> 0.5.0", platform: :ruby
gem "mysql2", platform: :ruby
gem "activerecord-jdbcmysql-adapter", "~> 61.0", platform: :jruby
end

Expand Down
3 changes: 2 additions & 1 deletion lib/ranked-model/ranker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def position_at value

def rank_at value
instance.send "#{ranker.column}=", value
instance.send "#{ranker.name}_position=", relative_rank unless position.is_a?(Integer)
end

def rank_changed?
Expand Down Expand Up @@ -236,7 +237,7 @@ def rebalance_ranks
new_rank = (gap_size * position) + RankedModel::MIN_RANK_VALUE

if item.instance.id == instance.id
rank_at new_rank
instance.send "#{ranker.column}=", new_rank
else
item.update_rank! new_rank
end
Expand Down
2 changes: 1 addition & 1 deletion ranked-model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
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.}
s.license = 'MIT'

s.add_dependency "activerecord", ">= 4.2"
s.add_dependency "activerecord", ">= 5.2"
s.add_development_dependency "rspec", "~> 3"
s.add_development_dependency "rspec-its"
s.add_development_dependency "mocha"
Expand Down
71 changes: 71 additions & 0 deletions spec/duck-model/inferred_ducks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "spec_helper"

describe Duck do
before do
5.times do |i|
Duck.create(name: "Duck #{i + 1}")
end
end

describe "updating a duck order with last" do
it "should maintain the order after creating a new duck" do
duck = Duck.first
duck.update(row_position: :last)
expect(duck.row_rank).to eq(4)

Duck.create(name: "Wacky")

expect(duck.row_rank).to eq(4)

duck.update(pond: 'Shin')
expect(duck.row_rank).to eq(4)
end
end

describe "updating a duck order with first" do
it "should maintain the order after creating a new duck" do
duck = Duck.last
duck.update(row_position: :first)
expect(duck.row_rank).to eq(0)

Duck.create(name: "Wacky")

expect(duck.row_rank).to eq(0)

duck.update(pond: 'Shin')
expect(duck.row_rank).to eq(0)
end
end

describe "updating a duck order with up" do
it "should maintain the order after creating a new duck" do
duck_id = Duck.ranker(:row).with(Duck.new).current_at_position(2).instance.id
duck = Duck.find(duck_id)
duck.update(row_position: :up)
expect(duck.row_rank).to eq(1)

Duck.create(name: "Wacky")

expect(duck.row_rank).to eq(1)

duck.update(pond: 'Shin')
expect(duck.row_rank).to eq(1)
end
end

describe "updating a duck order with down" do
it "should maintain the order after creating a new duck" do
duck_id = Duck.ranker(:row).with(Duck.new).current_at_position(2).instance.id
duck = Duck.find(duck_id)
duck.update(row_position: :down)
expect(duck.row_rank).to eq(3)

Duck.create(name: "Wacky")

expect(duck.row_rank).to eq(3)

duck.update(pond: 'Shin')
expect(duck.row_rank).to eq(3)
end
end
end

0 comments on commit f9145d4

Please sign in to comment.