Browse Source

Fixes #3451: New created overviews should be at the end of the overview list

Bola Ahmed Buari 4 years ago
parent
commit
dadb564d1f
2 changed files with 17 additions and 1 deletions
  1. 1 1
      app/models/overview.rb
  2. 16 0
      spec/models/overview_spec.rb

+ 1 - 1
app/models/overview.rb

@@ -63,7 +63,7 @@ class Overview < ApplicationModel
   def fill_prio
     return true if prio.present?
 
-    self.prio = Overview.count + 1
+    self.prio = Overview.maximum(:prio) + 1
     true
   end
 

+ 16 - 0
spec/models/overview_spec.rb

@@ -73,4 +73,20 @@ RSpec.describe Overview, type: :model do
       expect(overviews.third).to eq(overview2.id)
     end
   end
+
+  describe '#fill_prio' do
+
+    it 'fill an empty prio with the maximum prio plus one' do
+
+      overview1 = create(:overview, prio: 1)
+      overview2 = create(:overview, prio: 200)
+      overview3 = create(:overview, prio: nil)
+
+      overviews = described_class.all.order(prio: :asc).pluck(:id)
+
+      expect(overviews.first).to eq(overview1.id)
+      expect(overviews.second).to eq(overview2.id)
+      expect(overviews.last).to eq(overview3.id)
+    end
+  end
 end