Class: Narabikae::ActiveRecordExtension
- Inherits:
-
Object
- Object
- Narabikae::ActiveRecordExtension
- Defined in:
- lib/narabikae/active_record_extension.rb
Instance Method Summary collapse
- #auto_set_position? ⇒ Boolean
-
#initialize(record, option) ⇒ ActiveRecordExtension
constructor
A new instance of ActiveRecordExtension.
- #move_to_after(target, **args) ⇒ Object
- #move_to_before(target, **args) ⇒ Object
- #move_to_between(prev_target, next_target, **args) ⇒ Object
- #set_position ⇒ Object
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
10 11 12 13 14 15 16 17 |
# 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)) 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
23 24 25 26 27 28 29 |
# File 'lib/narabikae/active_record_extension.rb', line 23 def move_to_after(target, **args) new_position = position_generator.find_position_after(target, **args) return false if new_position.blank? record.send("#{option.field}=", new_position) record.save end |
#move_to_before(target, **args) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/narabikae/active_record_extension.rb', line 31 def move_to_before(target, **args) new_position = position_generator.find_position_before(target, **args) return false if new_position.blank? record.send("#{option.field}=", new_position) record.save end |
#move_to_between(prev_target, next_target, **args) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/narabikae/active_record_extension.rb', line 39 def move_to_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) record.save end |
#set_position ⇒ Object
19 20 21 |
# File 'lib/narabikae/active_record_extension.rb', line 19 def set_position record.send("#{option.field}=", position_generator.create_last_position) end |