Class: HandySort::CallbackManager

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

Instance Method Summary collapse

Constructor Details

#initialize(ranker, options = {}) ⇒ CallbackManager

Returns a new instance of CallbackManager.



7
8
9
10
# File 'lib/handy_sort/callback_manager.rb', line 7

def initialize(ranker, options = {})
  @ranker  = ranker
  @options = options
end

Instance Method Details

#before_create(record) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/handy_sort/callback_manager.rb', line 18

def before_create(record)
  max = relation(record).count.succ

  if new_val = record[key]
    @ranker.move_affected(relation(record), max, new_val)
  else
    record[key] = max
  end
end

#before_destroy(record) ⇒ Object



34
35
36
37
# File 'lib/handy_sort/callback_manager.rb', line 34

def before_destroy(record)
  max = relation(record).count.succ
  @ranker.move_affected(relation(record), record[key], max)
end

#before_update(record) ⇒ Object



28
29
30
31
32
# File 'lib/handy_sort/callback_manager.rb', line 28

def before_update(record)
  return unless change = record.changes[key]

  @ranker.move_affected(relation(record), *change)
end

#install(klass) ⇒ Object



12
13
14
15
16
# File 'lib/handy_sort/callback_manager.rb', line 12

def install(klass)
  [:before_create, :before_update, :before_destroy].each do |hook|
    klass.send hook, self
  end
end