ranked-model 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/lib/ranked-model.rb +8 -1
- data/lib/ranked-model/ranker.rb +2 -2
- data/lib/ranked-model/version.rb +1 -1
- data/spec/duck-model/column_default_ducks_spec.rb +14 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/active_record.rb +6 -0
- data/spec/support/database.yml +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b5933c81c7b8db9b1a26b327b7805d0aa0f6334
|
4
|
+
data.tar.gz: eaf6954358557ed51528cf76d9848f6d3961a7a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b905fc8d5ffb24b27eacbe3de7a7de5be4ff16c23ed2ca6256a1a73f6424076660bbbd74f1b99e472421f50fcd637c2732edf574a22f217ad8963af0f418901
|
7
|
+
data.tar.gz: f917f55c584657fe4d744592e04f131ec7cf58e9cf3abba73b532075a2776ec21d759d488d028e413deaedc946bf9b91b2b5a4c72980a2f535c46cfbc6e59cf9
|
data/.travis.yml
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_defaults[ranker.name.to_s]
|
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|
|
data/lib/ranked-model/ranker.rb
CHANGED
@@ -205,7 +205,7 @@ module RankedModel
|
|
205
205
|
end
|
206
206
|
|
207
207
|
def rebalance_ranks
|
208
|
-
if rank
|
208
|
+
if rank && instance.persisted?
|
209
209
|
origin = current_order.index { |item| item.instance.id == instance.id }
|
210
210
|
destination = current_order.index { |item| rank <= item.rank }
|
211
211
|
destination -= 1 if origin < destination
|
@@ -256,7 +256,7 @@ module RankedModel
|
|
256
256
|
_finder = _finder.where.not instance_class.primary_key.to_sym => instance.id
|
257
257
|
end
|
258
258
|
|
259
|
-
_finder.
|
259
|
+
_finder.reorder(ranker.column.to_sym => order).select(columns)
|
260
260
|
end
|
261
261
|
end
|
262
262
|
|
data/lib/ranked-model/version.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
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
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -33,6 +33,12 @@ ActiveRecord::Schema.define :version => 0 do
|
|
33
33
|
t.string :pond
|
34
34
|
end
|
35
35
|
|
36
|
+
create_table :column_default_ducks, :force => true do |t|
|
37
|
+
t.string :name
|
38
|
+
t.integer :size, default: 0
|
39
|
+
t.string :pond
|
40
|
+
end
|
41
|
+
|
36
42
|
create_table :elements, :force => true do |t|
|
37
43
|
t.string :symbol
|
38
44
|
t.string :type
|
data/spec/support/database.yml
CHANGED
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.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Beale
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -150,6 +150,7 @@ files:
|
|
150
150
|
- lib/ranked-model/version.rb
|
151
151
|
- rails/init.rb
|
152
152
|
- ranked-model.gemspec
|
153
|
+
- spec/duck-model/column_default_ducks_spec.rb
|
153
154
|
- spec/duck-model/duck_spec.rb
|
154
155
|
- spec/duck-model/lots_of_ducks_spec.rb
|
155
156
|
- spec/duck-model/wrong_ducks_spec.rb
|
@@ -189,6 +190,7 @@ signing_key:
|
|
189
190
|
specification_version: 4
|
190
191
|
summary: An acts_as_sortable replacement built for Rails 3 & 4
|
191
192
|
test_files:
|
193
|
+
- spec/duck-model/column_default_ducks_spec.rb
|
192
194
|
- spec/duck-model/duck_spec.rb
|
193
195
|
- spec/duck-model/lots_of_ducks_spec.rb
|
194
196
|
- spec/duck-model/wrong_ducks_spec.rb
|