Class: HandySort::Retentioner

Inherits:
Object
  • Object
show all
Defined in:
lib/handy_sort/retentioner.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, sort_key, fk) ⇒ Retentioner

Returns a new instance of Retentioner.



3
4
5
# File 'lib/handy_sort/retentioner.rb', line 3

def initialize(klass, sort_key, fk)
  @klass, @sort_key, @fk = klass, sort_key, fk
end

Instance Method Details

#retentionObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/handy_sort/retentioner.rb', line 7

def retention
  parent = @klass.quoted_table_name
  @klass.update_all <<-SQL.strip_heredoc
    #{@sort_key} = (
      SELECT COUNT(*) + 1
      FROM   #{parent} sub
      WHERE  sub.#{@fk}  = #{parent}.#{@fk}
      AND    sub.#{@sort_key} < #{parent}.#{@sort_key}
    )
  SQL
end