Module: Iron::Lexorank::Rankable::InstanceMethods

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

#move_to(position) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/iron/lexorank/rankable.rb', line 81

def move_to(position)
  # exceptions:
  #   move to the beginning (aka move to position 0)
  #   move to end (aka position = collection.size - 1)
  # when moving to the end of the collection the offset and limit statement automatically handles
  # that 'after' is nil which is the same like [collection.last, nil]
  before, after =
    if position.zero?
      [ nil, scoped_collection.first ]
    else
      scoped_collection.where.not(id: id).offset(position - 1).limit(2)
    end

  rank =
    if self == after && send(rank_column).present?
      send(rank_column)
    else
      value_between(before&.send(rank_column), after&.send(rank_column))
    end

  send("#{rank_column}=", rank)
end

#move_to!(position) ⇒ Object



104
105
106
107
# File 'lib/iron/lexorank/rankable.rb', line 104

def move_to!(position)
  move_to(position)
  save
end

#move_to_bottomObject



120
121
122
123
124
# File 'lib/iron/lexorank/rankable.rb', line 120

def move_to_bottom
  before = scoped_collection.last
  rank = value_between(before&.send(rank_column), nil)
  send("#{rank_column}=", rank)
end

#move_to_bottom!Object



126
127
128
129
# File 'lib/iron/lexorank/rankable.rb', line 126

def move_to_bottom!
  move_to_bottom
  save
end

#move_to_topObject



109
110
111
112
113
# File 'lib/iron/lexorank/rankable.rb', line 109

def move_to_top
  after = scoped_collection.first
  rank = value_between(nil, after&.send(rank_column))
  send("#{rank_column}=", rank)
end

#move_to_top!Object



115
116
117
118
# File 'lib/iron/lexorank/rankable.rb', line 115

def move_to_top!
  move_to_top
  save
end

#no_rank?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/iron/lexorank/rankable.rb', line 131

def no_rank?
  !send(rank_column)
end