Module: LexoRanker::RankableMethods::Base
- Defined in:
- lib/lexoranker/rankable_methods/base.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(klass) ⇒ Object
Ruby lifecycle callback, executed when included into other classes.
Instance Method Summary collapse
-
#move_to(position) ⇒ String
Moves an instance to a rank that corresponds to position (0-indexed).
-
#move_to_bottom ⇒ String
Move an instance to the bottom of the rankings.
-
#move_to_bottom! ⇒ String
Move an instance to the bottom of the rankings and save.
-
#move_to_top ⇒ String
Move an instance to the top of the rankings.
-
#move_to_top! ⇒ String
Move an instance to the top of the rankings and save.
-
#rank_value ⇒ String
Returns the value of the rank column.
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
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_bottom ⇒ String
Move an instance to the bottom of the rankings
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.
109 110 111 |
# File 'lib/lexoranker/rankable_methods/base.rb', line 109 def move_to_bottom! move_to!(ranked_collection.length) end |
#move_to_top ⇒ String
Move an instance to the top of the rankings
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
83 84 85 |
# File 'lib/lexoranker/rankable_methods/base.rb', line 83 def move_to_top! move_to!(0) end |
#rank_value ⇒ String
Returns the value of the rank column
120 121 122 |
# File 'lib/lexoranker/rankable_methods/base.rb', line 120 def rank_value send(self.class.rankable_column) end |