ranked-model 0.4.2 → 0.4.7

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.
@@ -5,7 +5,7 @@ describe Duck do
5
5
  before {
6
6
  200.times do |i|
7
7
  Duck.create \
8
- :name => "Duck #{i}"
8
+ :name => "Duck #{i + 1}"
9
9
  end
10
10
  }
11
11
 
@@ -29,7 +29,7 @@ describe Duck do
29
29
 
30
30
  before {
31
31
  @last = Duck.last
32
- @last.update_attribute :row_position, 137
32
+ @last.update :row_position => 137
33
33
  }
34
34
 
35
35
  subject { Duck.ranker(:row).with(Duck.new).current_at_position(137).instance }
@@ -42,7 +42,7 @@ describe Duck do
42
42
 
43
43
  before {
44
44
  @last = Duck.last
45
- @last.update_attribute :row_position, 2
45
+ @last.update :row_position => 2
46
46
  }
47
47
 
48
48
  subject { Duck.ranker(:row).with(Duck.new).current_at_position(2).instance }
@@ -55,7 +55,7 @@ describe Duck do
55
55
 
56
56
  before {
57
57
  @last = Duck.last
58
- @last.update_attribute :row_position, :last
58
+ @last.update :row_position => :last
59
59
  }
60
60
 
61
61
  subject { Duck.rank(:row).last }
@@ -68,7 +68,7 @@ describe Duck do
68
68
 
69
69
  before {
70
70
  @last = Duck.last
71
- @last.update_attribute :row_position, :first
71
+ @last.update :row_position => :first
72
72
  }
73
73
 
74
74
  subject { Duck.rank(:row).first }
@@ -87,8 +87,8 @@ describe Duck do
87
87
  @first = Duck.first
88
88
  @second = Duck.offset(1).first
89
89
  @ordered = Duck.rank(:row).where(Duck.arel_table[:id].not_in([@first.id, @second.id])).collect {|d| d.id }
90
- @first.update_attribute :row, RankedModel::MAX_RANK_VALUE
91
- @second.update_attribute :row, RankedModel::MAX_RANK_VALUE
90
+ @first.update :row => RankedModel::MAX_RANK_VALUE
91
+ @second.update :row => RankedModel::MAX_RANK_VALUE
92
92
  }
93
93
 
94
94
  context {
@@ -105,13 +105,13 @@ describe Duck do
105
105
 
106
106
  before {
107
107
  Duck.first(50).each_with_index do |d, index|
108
- d.update_attributes :age => index % 10, :pond => "Pond #{index / 10}"
108
+ d.update :age => index % 10, :pond => "Pond #{index / 10}"
109
109
  end
110
110
  @duck_11 = Duck.where(:pond => 'Pond 1').rank(:age).first
111
111
  @duck_12 = Duck.where(:pond => 'Pond 1').rank(:age).second
112
112
  @ordered = Duck.where(:pond => 'Pond 1').rank(:age).where(Duck.arel_table[:id].not_in([@duck_11.id, @duck_12.id])).collect {|d| d.id }
113
- @duck_11.update_attribute :age, RankedModel::MAX_RANK_VALUE
114
- @duck_12.update_attribute :age, RankedModel::MAX_RANK_VALUE
113
+ @duck_11.update :age => RankedModel::MAX_RANK_VALUE
114
+ @duck_12.update :age => RankedModel::MAX_RANK_VALUE
115
115
  }
116
116
 
117
117
  context {
@@ -133,8 +133,8 @@ describe Duck do
133
133
  @first = Duck.first
134
134
  @second = Duck.offset(1).first
135
135
  @ordered = Duck.rank(:row).where(Duck.arel_table[:id].not_in([@first.id, @second.id])).collect {|d| d.id }
136
- @first.update_attribute :row, RankedModel::MIN_RANK_VALUE
137
- @second.update_attribute :row, RankedModel::MIN_RANK_VALUE
136
+ @first.update :row => RankedModel::MIN_RANK_VALUE
137
+ @second.update :row => RankedModel::MIN_RANK_VALUE
138
138
  }
139
139
 
140
140
  context {
@@ -149,34 +149,30 @@ describe Duck do
149
149
 
150
150
  describe "with no more gaps" do
151
151
 
152
- before {
152
+ before do
153
153
  @first = Duck.rank(:row).first
154
154
  @second = Duck.rank(:row).offset(1).first
155
155
  @third = Duck.rank(:row).offset(2).first
156
- @fourth = Duck.rank(:row).offset(4).first
157
- @fifth = Duck.rank(:row).offset(5).first
156
+ @fourth = Duck.rank(:row).offset(3).first
158
157
  @lower = Duck.rank(:row).
159
- where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id, @fifth.id])).
158
+ where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id])).
160
159
  where(Duck.arel_table[:row].lt(RankedModel::MAX_RANK_VALUE / 2)).
161
- collect {|d| d.id }
160
+ pluck(:id)
162
161
  @upper = Duck.rank(:row).
163
- where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id, @fifth.id])).
162
+ where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id])).
164
163
  where(Duck.arel_table[:row].gteq(RankedModel::MAX_RANK_VALUE / 2)).
165
- collect {|d| d.id }
166
- @first.update_attribute :row, RankedModel::MIN_RANK_VALUE
167
- @second.update_attribute :row, RankedModel::MAX_RANK_VALUE
168
- @third.update_attribute :row, (RankedModel::MAX_RANK_VALUE / 2)
169
- Duck.where(id: @fifth.id).update_all row: @third.row
170
- @fourth.update_attribute :row, @third.row
171
- }
172
-
173
- context {
174
-
175
- subject { Duck.rank(:row).collect {|d| d.id } }
176
-
177
- it { is_expected.to eq([@first.id] + @lower + [@fourth.id, @third.id, @fifth.id] + @upper + [@second.id]) }
178
-
179
- }
164
+ pluck(:id)
165
+ @first.update(row: RankedModel::MIN_RANK_VALUE)
166
+ @second.update(row: RankedModel::MAX_RANK_VALUE)
167
+ @third.update(row: (RankedModel::MAX_RANK_VALUE / 2))
168
+ @fourth.update(row: @third.row)
169
+ end
170
+
171
+ it 'works correctly' do
172
+ result = Duck.rank(:row).pluck(:id)
173
+ expected = [@first.id, *@lower, @fourth.id, @third.id, *@upper, @second.id]
174
+ expect(result).to eq(expected)
175
+ end
180
176
 
181
177
  end
182
178
 
@@ -10,7 +10,7 @@ describe Ego do
10
10
  }
11
11
  @egos.each { |name, ego|
12
12
  ego.reload
13
- ego.update_attribute :size_position, 0
13
+ ego.update :size_position => 0
14
14
  ego.save!
15
15
  }
16
16
  @egos.each {|name, ego| ego.reload }
@@ -19,8 +19,8 @@ describe Ego do
19
19
  describe "sorting on size alternative primary key" do
20
20
 
21
21
  before {
22
- @egos[:nick].update_attribute :size_position, 0
23
- @egos[:sally].update_attribute :size_position, 2
22
+ @egos[:nick].update :size_position => 0
23
+ @egos[:sally].update :size_position => 2
24
24
  }
25
25
 
26
26
  subject { Ego.rank(:size).to_a }
@@ -14,8 +14,8 @@ describe Number do
14
14
  @first = Number.first
15
15
  @second = Number.offset(1).first
16
16
  @ordered = Number.rank(:order).where(Number.arel_table[:id].not_in([@first.id, @second.id])).collect {|d| d.id }
17
- @first.update_attribute :order, RankedModel::MAX_RANK_VALUE
18
- @second.update_attribute :order, RankedModel::MAX_RANK_VALUE
17
+ @first.update :order => RankedModel::MAX_RANK_VALUE
18
+ @second.update :order => RankedModel::MAX_RANK_VALUE
19
19
  }
20
20
 
21
21
  context {
@@ -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
@@ -16,7 +16,7 @@ describe Player do
16
16
 
17
17
  describe "setting the position of a record that already exists" do
18
18
  it "sets the rank without error" do
19
- expect{@players[:bob].update_attributes! :score_position => 1}.to_not raise_error
19
+ expect{@players[:bob].update! :score_position => 1}.to_not raise_error
20
20
  end
21
21
  end
22
22
  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
@@ -29,6 +29,8 @@ RSpec.configure do |config|
29
29
  DatabaseCleaner.clean
30
30
  end
31
31
 
32
+ config.order = :random
33
+ Kernel.srand config.seed
32
34
  end
33
35
 
34
36
  RSpec::Matchers.define :define_constant do |expected|
@@ -12,7 +12,7 @@ describe Element do
12
12
  }
13
13
  @elements.each { |name, element|
14
14
  element.reload
15
- element.update_attribute :combination_order_position, 0
15
+ element.update :combination_order_position => 0
16
16
  }
17
17
  @elements.each {|name, element| element.reload }
18
18
  }
@@ -20,9 +20,9 @@ describe Element do
20
20
  describe "rebalancing on an STI class should not affect the other class" do
21
21
 
22
22
  before {
23
- @elements[:helium].update_attribute :combination_order_position, :first
24
- @elements[:xenon].update_attribute :combination_order_position, :first
25
- @elements[:argon].update_attribute :combination_order_position, :last
23
+ @elements[:helium].update :combination_order_position => :first
24
+ @elements[:xenon].update :combination_order_position => :first
25
+ @elements[:argon].update :combination_order_position => :last
26
26
 
27
27
  TransitionMetal.ranker(:combination_order).with(@elements[:chromium]).instance_eval { rebalance_ranks }
28
28
  }
@@ -40,16 +40,16 @@ describe Element do
40
40
  describe "setting positions on STI classes" do
41
41
 
42
42
  before {
43
- @elements[:helium].update_attribute :combination_order_position, :first
44
- @elements[:xenon].update_attribute :combination_order_position, :first
45
- @elements[:argon].update_attribute :combination_order_position, :first
46
-
47
- @elements[:chromium].update_attribute :combination_order_position, 1
48
- @elements[:manganese].update_attribute :combination_order_position, 1
49
- @elements[:manganese].update_attribute :combination_order_position, 0
50
- @elements[:chromium].update_attribute :combination_order_position, 0
51
- @elements[:manganese].update_attribute :combination_order_position, 0
52
- @elements[:chromium].update_attribute :combination_order_position, 0
43
+ @elements[:helium].update :combination_order_position => :first
44
+ @elements[:xenon].update :combination_order_position => :first
45
+ @elements[:argon].update :combination_order_position => :first
46
+
47
+ @elements[:chromium].update :combination_order_position => 1
48
+ @elements[:manganese].update :combination_order_position => 1
49
+ @elements[:manganese].update :combination_order_position => 0
50
+ @elements[:chromium].update :combination_order_position => 0
51
+ @elements[:manganese].update :combination_order_position => 0
52
+ @elements[:chromium].update :combination_order_position => 0
53
53
  }
54
54
 
55
55
  describe "NobleGas" do
@@ -81,16 +81,16 @@ describe Element do
81
81
  describe "setting positions on STI classes" do
82
82
 
83
83
  before {
84
- @elements[:helium].update_attribute :combination_order_position, :first
85
- @elements[:xenon].update_attribute :combination_order_position, :first
86
- @elements[:argon].update_attribute :combination_order_position, :first
87
-
88
- @elements[:chromium].update_attribute :combination_order_position, 1
89
- @elements[:manganese].update_attribute :combination_order_position, 1
90
- @elements[:manganese].update_attribute :combination_order_position, 0
91
- @elements[:chromium].update_attribute :combination_order_position, 0
92
- @elements[:manganese].update_attribute :combination_order_position, 0
93
- @elements[:chromium].update_attribute :combination_order_position, 0
84
+ @elements[:helium].update :combination_order_position => :first
85
+ @elements[:xenon].update :combination_order_position => :first
86
+ @elements[:argon].update :combination_order_position => :first
87
+
88
+ @elements[:chromium].update :combination_order_position => 1
89
+ @elements[:manganese].update :combination_order_position => 1
90
+ @elements[:manganese].update :combination_order_position => 0
91
+ @elements[:chromium].update :combination_order_position => 0
92
+ @elements[:manganese].update :combination_order_position => 0
93
+ @elements[:chromium].update :combination_order_position => 0
94
94
  }
95
95
 
96
96
  describe "NobleGas" do
@@ -12,7 +12,7 @@ describe Vehicle do
12
12
  }
13
13
  @vehicles.each { |name, vehicle|
14
14
  vehicle.reload
15
- vehicle.update_attribute :parking_order_position, 0
15
+ vehicle.update :parking_order_position => 0
16
16
  }
17
17
  @vehicles.each {|name, vehicle| vehicle.reload }
18
18
  }
@@ -20,8 +20,8 @@ describe Vehicle do
20
20
  describe "ranking by STI parent" do
21
21
 
22
22
  before {
23
- @vehicles[:volvo].update_attribute :parking_order_position, :first
24
- @vehicles[:ford].update_attribute :parking_order_position, :first
23
+ @vehicles[:volvo].update :parking_order_position => :first
24
+ @vehicles[:ford].update :parking_order_position => :first
25
25
  }
26
26
 
27
27
  describe "Vehicle" do
@@ -72,4 +72,4 @@ describe Vehicle do
72
72
 
73
73
  end
74
74
 
75
- end
75
+ end
@@ -1,13 +1,13 @@
1
1
  require 'active_record'
2
2
  require 'logger'
3
3
 
4
- ROOT = File.join(File.dirname(__FILE__), '..')
5
-
6
- DB_CONFIG = ENV["DB"] || "sqlite"
4
+ unless ENV['DB']
5
+ ENV['DB'] = 'sqlite'
6
+ end
7
7
 
8
8
  ActiveRecord::Base.logger = Logger.new('tmp/ar_debug.log')
9
9
  ActiveRecord::Base.configurations = YAML::load(IO.read('spec/support/database.yml'))
10
- ActiveRecord::Base.establish_connection(DB_CONFIG.to_sym)
10
+ ActiveRecord::Base.establish_connection(ENV['DB'].to_sym)
11
11
 
12
12
  ActiveRecord::Schema.define :version => 0 do
13
13
  create_table :ducks, :force => true do |t|
@@ -21,6 +21,8 @@ ActiveRecord::Schema.define :version => 0 do
21
21
  t.string :pond
22
22
  end
23
23
 
24
+ add_index :ducks, [:landing_order, :lake_id, :flock_id], unique: true
25
+
24
26
  create_table :wrong_scope_ducks, :force => true do |t|
25
27
  t.string :name
26
28
  t.integer :size
@@ -33,6 +35,12 @@ ActiveRecord::Schema.define :version => 0 do
33
35
  t.string :pond
34
36
  end
35
37
 
38
+ create_table :column_default_ducks, :force => true do |t|
39
+ t.string :name
40
+ t.integer :size, default: 0
41
+ t.string :pond
42
+ end
43
+
36
44
  create_table :elements, :force => true do |t|
37
45
  t.string :symbol
38
46
  t.string :type
@@ -142,7 +150,7 @@ class MotorBike < Vehicle
142
150
  end
143
151
 
144
152
  class Ego < ActiveRecord::Base
145
- primary_key = :alternative_to_id
153
+ self.primary_key = :alternative_to_id
146
154
  include RankedModel
147
155
  ranks :size
148
156
  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.2
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: 2018-12-09 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
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.16
19
+ version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.16
26
+ version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: ranked-model is a modern row sorting library built for Rails 3 & 4. It
125
+ description: ranked-model is a modern row sorting library built for Rails 4.2+. It
126
126
  uses ARel aggressively and is better optimized than most other libraries.
127
127
  email:
128
128
  - matt.beale@madhatted.com
@@ -138,18 +138,18 @@ files:
138
138
  - LICENSE
139
139
  - Rakefile
140
140
  - Readme.mkd
141
- - gemfiles/rails_3_2.gemfile
142
- - gemfiles/rails_4_1.gemfile
143
141
  - gemfiles/rails_4_2.gemfile
144
142
  - gemfiles/rails_5_0.gemfile
145
143
  - gemfiles/rails_5_1.gemfile
146
144
  - gemfiles/rails_5_2.gemfile
145
+ - gemfiles/rails_6_0.gemfile
147
146
  - lib/ranked-model.rb
148
147
  - lib/ranked-model/railtie.rb
149
148
  - lib/ranked-model/ranker.rb
150
149
  - lib/ranked-model/version.rb
151
150
  - rails/init.rb
152
151
  - ranked-model.gemspec
152
+ - spec/duck-model/column_default_ducks_spec.rb
153
153
  - spec/duck-model/duck_spec.rb
154
154
  - spec/duck-model/lots_of_ducks_spec.rb
155
155
  - spec/duck-model/wrong_ducks_spec.rb
@@ -183,12 +183,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
183
  - !ruby/object:Gem::Version
184
184
  version: '0'
185
185
  requirements: []
186
- rubyforge_project:
187
- rubygems_version: 2.5.2.2
186
+ rubygems_version: 3.0.3
188
187
  signing_key:
189
188
  specification_version: 4
190
- summary: An acts_as_sortable replacement built for Rails 3 & 4
189
+ summary: An acts_as_sortable replacement built for Rails 4.2+
191
190
  test_files:
191
+ - spec/duck-model/column_default_ducks_spec.rb
192
192
  - spec/duck-model/duck_spec.rb
193
193
  - spec/duck-model/lots_of_ducks_spec.rb
194
194
  - spec/duck-model/wrong_ducks_spec.rb
@@ -1,22 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 3.2.22.5"
6
-
7
- group :sqlite do
8
- gem "sqlite3", platform: :ruby
9
- gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.24", platform: :jruby
10
- end
11
-
12
- group :mysql do
13
- gem "mysql2", "~> 0.3.21", platform: :ruby
14
- gem "activerecord-jdbcmysql-adapter", "~> 1.3.24", platform: :jruby
15
- end
16
-
17
- group :postgresql do
18
- gem "pg", "~> 0.18.0", platform: :ruby
19
- gem "activerecord-jdbcpostgresql-adapter", "~> 1.3.24", platform: :jruby
20
- end
21
-
22
- gemspec path: "../"