Module: Iron::Lexorank::Rankable::ClassMethods

Includes:
Utils
Defined in:
lib/iron/lexorank/rankable.rb

Constant Summary

Constants included from Utils

Utils::BASE, Utils::MAX_CHAR, Utils::MIN_CHAR

Instance Method Summary collapse

Methods included from Utils

#get_char, #mid, #optimal_rank_numeric_interval_for, #to_rank, #value_between

Instance Method Details

#rebalance(rank_scope = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/iron/lexorank/rankable.rb', line 47

def rebalance(rank_scope = nil)
  if rank_scope.present?
    if rank_scoped_by.is_a?(Array)
      type_column, id_column = rank_scoped_by
      ranked_scoped_items = ranked.where(
        "#{type_column}": rank_scope.first,
        "#{id_column}": rank_scope.second
      )
    else
      ranked_scoped_items = ranked.where("#{rank_scoped_by}": rank_scope)
    end

    interval = optimal_rank_numeric_interval_for(ranked_scoped_items.count)

    self.transaction do
      ranked_scoped_items.each.with_index(1) do |item, index|
        item.rank = to_rank(interval * index) + "-rebalancing"
        item.save!
      end

      ranked_scoped_items.each do |item|
        item.rank = item.rank.gsub("-rebalancing", "")
        item.save!
      end
    end
  else
    raise "Not implemented yet"
  end
end