Module: LexoRanker::RankableMethods::Base

Defined in:
lib/lexoranker/rankable_methods/base.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Ruby lifecycle callback, executed when included into other classes



7
8
9
# File 'lib/lexoranker/rankable_methods/base.rb', line 7

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#move_to(position) ⇒ String

Moves an instance to a rank that corresponds to position (0-indexed). Throws OutOfBoundsError if position is negative

Examples:

moving an instance to the 3rd position

element = Element.find('some_id')
element.move_to(2) # => 'Ea'
element.changed? # => false

moving an instance to a negative position

element = Element.find('some_id')
element.move_to(-1) # OutOfBoundsError raised

Parameters:

  • position (Integer)

    the position to move the instance to (0-indexed)

Returns:

  • (String)

    the rank the instance has been assigned

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/lexoranker/rankable_methods/base.rb', line 139

def move_to(position)
  raise OutOfBoundsError, "position mus be 0 or a positive integer" if position.negative?
  position = ranked_collection.length if position > ranked_collection.length

  previous, following = if position.zero?
    [nil, ranked_collection.first]
  else
    scope_value = send(self.class.rankable_scope) if rankable_scoped?
    self.class.ranks_around_position(id, position, scope_value: scope_value)
  end

  rank = self.class.rankable_ranker.between(previous, following)

  send(:"#{self.class.rankable_column}=", rank)
end

#move_to_bottomString

Move an instance to the bottom of the rankings

Examples:

Moving an element to the bottom of the rankings (ActiveRecord, #rank column)

element = Element.find('some_id')
element.move_to_bottom # => 'zzz'
element.rank # => 'zzz'
element.changed? => true

Returns:

  • (String)

    The rank the instance has been assigned



96
97
98
# File 'lib/lexoranker/rankable_methods/base.rb', line 96

def move_to_bottom
  move_to(ranked_collection.length)
end

#move_to_bottom!String

Move an instance to the bottom of the rankings and save. Raises an error if it can not be saved.

Examples:

Moving an element to the bottom of the rankings (ActiveRecord, #rank column)

element = Element.find('some_id')
element.move_to_bottom! # => 'zzz'
element.rank # => 'zzz'
element.changed? => false

Returns:

  • (String)

    The rank the instance has been assigned



109
110
111
# File 'lib/lexoranker/rankable_methods/base.rb', line 109

def move_to_bottom!
  move_to!(ranked_collection.length)
end

#move_to_topString

Move an instance to the top of the rankings

Examples:

Moving an element to the top of the rankings (ActiveRecord, #rank column)

element = Element.find('some_id')
element.move_to_top # => 'aaa'
element.rank # => 'aaa'
element.changed? => true

Returns:

  • (String)

    The rank the instance has been assigned



70
71
72
# File 'lib/lexoranker/rankable_methods/base.rb', line 70

def move_to_top
  move_to(0)
end

#move_to_top!String

Move an instance to the top of the rankings and save. Raises an error if it can not be saved

Examples:

Moving an element to the top of the rankings (ActiveRecord, #rank column)

element = Element.find('some_id')
element.move_to_top! # => 'aaa'
element.rank # => 'aaa'
element.changed? => false

Returns:

  • (String)

    The rank the instance has been assigned



83
84
85
# File 'lib/lexoranker/rankable_methods/base.rb', line 83

def move_to_top!
  move_to!(0)
end

#rank_valueString

Returns the value of the rank column

Examples:

Getting the rank of an instance

element = Element.find('some_id')
element.rank_value # => 'rra'

Returns:

  • (String)

    the rank the instance has been assigned



120
121
122
# File 'lib/lexoranker/rankable_methods/base.rb', line 120

def rank_value
  send(self.class.rankable_column)
end