Module: Lexoranking::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lexoranking/model.rb
Overview
Allows your models to sort elements using lexographical sorting
Options: self.ranking_scope - Determine if the elements should be scoped to a specific association before sorting.
Instance Method Summary collapse
-
#rank_first ⇒ Object
Ranks the record to the first postiion of the list and saves it.
-
#rank_last ⇒ Object
Ranks the record to the last position of the list and saves it.
-
#rank_to(position) ⇒ Object
Ranks the record to a specific position in the list and saves it.
-
#save ⇒ Object
Rank the record to the last position of the list before saving it.
Instance Method Details
#rank_first ⇒ Object
Ranks the record to the first postiion of the list and saves it.
32 33 34 |
# File 'lib/lexoranking/model.rb', line 32 def rank_first rank_to(0) end |
#rank_last ⇒ Object
Ranks the record to the last position of the list and saves it.
27 28 29 |
# File 'lib/lexoranking/model.rb', line 27 def rank_last rank_to(:last) end |
#rank_to(position) ⇒ Object
Ranks the record to a specific position in the list and saves it.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lexoranking/model.rb', line 37 def rank_to(position) model_collection = get_collection # This return statement handle the case of when there is only one # element in the scoped collection and we call this method. Since # there is only one element in the collection and the rank column is # present there is no reason to calculate the ranking value again. return if model_collection.size == 1 && rank.present? position = position == :last ? model_collection.size-1 : position.to_i previous, nextt = get_prev_next_elements(position, model_collection) ranking = calculate_ranking(previous, nextt) send("#{self.class.ranking_column}=", ranking) save end |
#save ⇒ Object
Rank the record to the last position of the list before saving it.
21 22 23 24 |
# File 'lib/lexoranking/model.rb', line 21 def save rank_to(:last) if rank.nil? super end |