Module: ActiveRecordFlorder::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record_florder/base.rb

Overview

Base model implements all required logic It’s depend on ActiveRecordFlorder::Configurable attributes

Instance Method Summary collapse

Instance Method Details

#move(position) ⇒ Object

Move model to position

Parameters:

  • position (Float)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_record_florder/base.rb', line 46

def move(position)
  position = position.to_f

  fail ActiveRecordFlorder::Error, 'Position param is required' unless position
  fail ActiveRecordFlorder::Error, 'Position should be > 0' unless (normalized_position = normalize_position(position)) > 0
  normalized_position = normalize_position(position)

  affected = ensure_position_solving(position, normalized_position)

  if new_record?
    self[position_attr_name.to_sym] = normalized_position
  else
    update_attribute(position_attr_name.to_sym, normalized_position)
  end

  # populating all affected records
  # only if return_all_affected_by_move option is true
  if return_all_affected_by_move
    affected ||= []
    return affected << self
  end

  self
end