Преглед изворни кода

fix faulty fable scrolling

Kylie Jo Swistak пре 4 година
родитељ
комит
4d17a5a980
1 измењених фајлова са 11 додато и 6 уклоњено
  1. 11 6
      app/carousels/fable.rb

+ 11 - 6
app/carousels/fable.rb

@@ -25,6 +25,9 @@ class FableCarousel < Carousel
     # Find next fable
     fable = next_fable(section, carousel)
 
+    carousel.update(fable_id: fable.id)
+    carousel.reload
+
     # Update to new fable
     BotResponse.new(
       carousel: carousel,
@@ -33,17 +36,19 @@ class FableCarousel < Carousel
   end
 
   def self.next_fable(section, carousel)
+    # The list of fables in order, and the index of the current fable
+    fables = Fable.order('id ASC')
+    i = fables.map(&:id).index(carousel.fable_id)
+
     case section
     when 'first'
-      Fable.order('id DESC').first
+      fables.first
     when 'left'
-      Fable.where('id < ?', carousel.fable_id).order('id DESC').first ||
-        Fable.order('id DESC').first
+      i == 0 ? fables.last : fables[i-1]
     when 'right'
-      Fable.where('id > ?', carousel.fable_id).order('id ASC').first ||
-        Fable.order('id ASC').first
+      i == fables.length-1 ? fables.first : fables[i+1]
     when 'last'
-      Fable.order('id ASC').first
+      fables.last
     end
   end
 end