handy_sort 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/handy_sort.rb +8 -5
- data/lib/handy_sort/callback_manager.rb +6 -0
- data/lib/handy_sort/retentioner.rb +19 -0
- data/lib/handy_sort/version.rb +1 -1
- metadata +3 -2
data/lib/handy_sort.rb
CHANGED
@@ -3,6 +3,7 @@ require 'handy_sort/version'
|
|
3
3
|
require 'active_support/concern'
|
4
4
|
require 'handy_sort/callback_manager'
|
5
5
|
require 'handy_sort/ranker'
|
6
|
+
require 'handy_sort/retentioner'
|
6
7
|
|
7
8
|
module HandySort
|
8
9
|
extend ActiveSupport::Concern
|
@@ -10,14 +11,16 @@ module HandySort
|
|
10
11
|
module ClassMethods
|
11
12
|
def handy_sort(key, options = {})
|
12
13
|
ranker = Ranker.new(key)
|
13
|
-
callback = CallbackManager.new(ranker, options)
|
14
14
|
|
15
|
-
|
16
|
-
before_update callback
|
15
|
+
CallbackManager.new(ranker, options).install(self)
|
17
16
|
|
18
|
-
|
17
|
+
scope :handy_sorted, -> { order("#{key} ASC") }
|
19
18
|
|
20
|
-
|
19
|
+
@retentioner = Retentioner.new(self, key, options[:within])
|
20
|
+
end
|
21
|
+
|
22
|
+
def retention
|
23
|
+
@retentioner.retention
|
21
24
|
end
|
22
25
|
end
|
23
26
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module HandySort
|
2
|
+
class Retentioner
|
3
|
+
def initialize(klass, sort_key, fk)
|
4
|
+
@klass, @sort_key, @fk = klass, sort_key, fk
|
5
|
+
end
|
6
|
+
|
7
|
+
def retention
|
8
|
+
parent = @klass.quoted_table_name
|
9
|
+
@klass.update_all <<-SQL.strip_heredoc
|
10
|
+
#{@sort_key} = (
|
11
|
+
SELECT COUNT(*) + 1
|
12
|
+
FROM #{parent} sub
|
13
|
+
WHERE sub.#{@fk} = #{parent}.#{@fk}
|
14
|
+
AND sub.#{@sort_key} < #{parent}.#{@sort_key}
|
15
|
+
)
|
16
|
+
SQL
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/handy_sort/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handy_sort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/handy_sort.rb
|
44
44
|
- lib/handy_sort/callback_manager.rb
|
45
45
|
- lib/handy_sort/ranker.rb
|
46
|
+
- lib/handy_sort/retentioner.rb
|
46
47
|
- lib/handy_sort/version.rb
|
47
48
|
homepage: https://github.com/moro/handy_sort
|
48
49
|
licenses: []
|