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.
@@ -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
- before_create callback
16
- before_update callback
15
+ CallbackManager.new(ranker, options).install(self)
17
16
 
18
- before_destroy callback
17
+ scope :handy_sorted, -> { order("#{key} ASC") }
19
18
 
20
- scope :handy_sort, -> { order("#{key} ASC") }
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
@@ -9,6 +9,12 @@ module HandySort
9
9
  @options = options
10
10
  end
11
11
 
12
+ def install(klass)
13
+ [:before_create, :before_update, :before_destroy].each do |hook|
14
+ klass.send hook, self
15
+ end
16
+ end
17
+
12
18
  def before_create(record)
13
19
  max = relation(record).count.succ
14
20
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module HandySort
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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-14 00:00:00.000000000 Z
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: []