Skip to content

Commit

Permalink
Improves position by stars logic and fixes that case: given 2 items w…
Browse files Browse the repository at this point in the history
…ith 5 stars and updating first's stars to 4 then it will be moved to #3.
  • Loading branch information
volontarian committed Aug 17, 2015
1 parent 81ad3a1 commit 0989538
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/models/user_ranking_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def self.position_for_user_by_stars(user_id, ranking_id, user_ranking_item_id, s
ranking_items = ranking_items.where('id <> ?', user_ranking_item_id) if user_ranking_item_id.present?

if stars.to_i >= 3
ranking_items.order('stars ASC, position DESC').where('stars >= ?', stars).first.try(:position).to_i + 1 || 1
position = ranking_items.order('stars ASC, position DESC').where('stars >= ?', stars).first.try(:position).to_i + 1 || 1

position -= 1 if user_ranking_item_id.present? && position > ranking_items_count

position
else
item = ranking_items.order('stars DESC, position ASC').where('stars <= ?', stars).first

Expand Down

0 comments on commit 0989538

Please sign in to comment.