module ActiveRecord::ActsAs::OrderableTable::InstanceMethods

Public Instance Methods

acts_ordinal_field() click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 41
def acts_ordinal_field
  self.class.ordinal_field
end
acts_ordinal_value() click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 45
def acts_ordinal_value
  self.send(acts_ordinal_field)
end
insert_at(position, ordinals_scope = nil) click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 26
def insert_at(position, ordinals_scope = nil)
  return if position == acts_ordinal_value # if ordinal haven't changed

  # if new position is not occupied just take this ordinal
  unless self.class.where("#{acts_ordinal_field}": position).first
    update_attributes("#{acts_ordinal_field}": position)
    return
  end

  items = items_scoped(position, ordinals_scope)
  current_positions = items.map { |item| item.send(acts_ordinal_field) }
  reordered_positions = reorder_positions(position, current_positions)
  update_ordinals(items, reordered_positions)
end
starts_from() click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 49
def starts_from
  self.class.starts_from
end

Private Instance Methods

check_ordinal_uniqueness() click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 76
def check_ordinal_uniqueness
  if acts_ordinal_value.present? &&
     self.class.where("#{acts_ordinal_field}": acts_ordinal_value).reject { |record| record == self }.first
    self.errors[acts_ordinal_field] << 'must be unique'
  end
end
items_scoped(position, ordinals_scope) click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 59
def items_scoped(position, ordinals_scope)
  actual_ordinals_scope = *ordinal_range(position)
  actual_ordinals_scope &= ordinals_scope if ordinals_scope
  self.class.where("#{acts_ordinal_field}": actual_ordinals_scope).order(acts_ordinal_field).to_a.push(self)
end
ordinal_range(position) click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 55
def ordinal_range(position)
  position > acts_ordinal_value ? (acts_ordinal_value + 1)..position : position...acts_ordinal_value
end
reorder_positions(position, positions) click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 65
def reorder_positions(position, positions)
  position > acts_ordinal_value ? positions.rotate(-1) : positions.rotate
end
set_defaults() click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 83
def set_defaults
  if acts_ordinal_value.blank?
    ordinal_value_prev = self.class.maximum(acts_ordinal_field)
    ordinal_value_next = (ordinal_value_prev ? ordinal_value_prev + 1 : starts_from)
    assign_attributes("#{acts_ordinal_field}": ordinal_value_next)
  end
end
update_ordinals(items, positions) click to toggle source
# File lib/active_record/acts_as/orderable_table.rb, line 69
def update_ordinals(items, positions)
  items.each_with_index do |item, index|
    item.assign_attributes("#{acts_ordinal_field}": positions[index])
    item.save!(validate: false)
  end
end