Class: Narabikae::ActiveRecordExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/narabikae/active_record_extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(record, option) ⇒ ActiveRecordExtension

Returns a new instance of ActiveRecordExtension.



3
4
5
6
7
8
# File 'lib/narabikae/active_record_extension.rb', line 3

def initialize(record, option)
  @record = record
  @option = option

  @position_generator = Narabikae::Position.new(record, option)
end

Instance Method Details

#auto_set_position?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'lib/narabikae/active_record_extension.rb', line 10

def auto_set_position?
  # check valid key for fractional_indexer
  # when invalid key, raise FractionalIndexer::Error
  FractionalIndexer.generate_key(prev_key: record.send(option.field))
  record.send(option.field).nil? ||
    (option.scope.any? { |s| record.will_save_change_to_attribute?(s) } && !record.will_save_change_to_attribute?(option.field))
rescue FractionalIndexer::Error
  true
end

#move_to_after(target, **args) ⇒ Object



53
54
55
56
57
# File 'lib/narabikae/active_record_extension.rb', line 53

def move_to_after(target, **args)
  return false unless set_after(target, **args)

  record.save
end

#move_to_before(target, **args) ⇒ Object



59
60
61
62
63
# File 'lib/narabikae/active_record_extension.rb', line 59

def move_to_before(target, **args)
  return false unless set_before(target, **args)

  record.save
end

#move_to_between(prev_target, next_target, **args) ⇒ Object



65
66
67
68
69
# File 'lib/narabikae/active_record_extension.rb', line 65

def move_to_between(prev_target, next_target, **args)
  return false unless set_between(prev_target, next_target, **args)

  record.save
end

#set_after(target, **args) ⇒ Object



32
33
34
35
36
37
# File 'lib/narabikae/active_record_extension.rb', line 32

def set_after(target, **args)
  new_position = position_generator.find_position_after(target, **args)
  return false if new_position.blank?

  record.send("#{option.field}=", new_position)
end

#set_before(target, **args) ⇒ Object



39
40
41
42
43
44
# File 'lib/narabikae/active_record_extension.rb', line 39

def set_before(target, **args)
  new_position = position_generator.find_position_before(target, **args)
  return false if new_position.blank?

  record.send("#{option.field}=", new_position)
end

#set_between(prev_target, next_target, **args) ⇒ Object



46
47
48
49
50
51
# File 'lib/narabikae/active_record_extension.rb', line 46

def set_between(prev_target, next_target, **args)
  new_position = position_generator.find_position_between(prev_target, next_target, **args)
  return false if new_position.blank?

  record.send("#{option.field}=", new_position)
end

#set_position(position = option.default_position) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/narabikae/active_record_extension.rb', line 20

def set_position(position = option.default_position)
  new_position =
    case position
    when :first
      position_generator.create_first_position
    when :last
      position_generator.create_last_position
    end

  record.send("#{option.field}=", new_position)
end