From 87bbb850c7e70ae5e459c678ad1b9efa1f079d8a Mon Sep 17 00:00:00 2001 From: Alic Szecsei Date: Tue, 14 Nov 2017 17:26:58 -0600 Subject: [PATCH 1/4] Make next fields optional --- lib/resort.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resort.rb b/lib/resort.rb index 9a8518c..6e1fbd8 100644 --- a/lib/resort.rb +++ b/lib/resort.rb @@ -62,7 +62,7 @@ def included(base) base.send :include, InstanceMethods base.has_one :previous, class_name: base.name, foreign_key: 'next_id', inverse_of: :next - base.belongs_to :next, class_name: base.name, inverse_of: :previous + base.belongs_to :next, class_name: base.name, inverse_of: :previous, optional: true base.after_create :include_in_list! base.after_destroy :delete_from_list From 85ef08c782e61cd33dd342f7985227ccb27057e8 Mon Sep 17 00:00:00 2001 From: Alic Szecsei Date: Fri, 17 Nov 2017 03:26:08 +0000 Subject: [PATCH 2/4] Fix deprecation warning --- lib/resort.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/resort.rb b/lib/resort.rb index 6e1fbd8..c64e93c 100644 --- a/lib/resort.rb +++ b/lib/resort.rb @@ -142,6 +142,7 @@ def siblings # empty list. def include_in_list! self.class.transaction do + save lock! _siblings.count > 0 ? last!\ : prepend From 8ed469293f43eda1546320c636acb09d1c75b6c1 Mon Sep 17 00:00:00 2001 From: Alic Szecsei Date: Fri, 17 Nov 2017 03:31:51 +0000 Subject: [PATCH 3/4] Remove more deprecation warnings --- lib/resort.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/resort.rb b/lib/resort.rb index c64e93c..1845dc2 100644 --- a/lib/resort.rb +++ b/lib/resort.rb @@ -152,6 +152,7 @@ def include_in_list! # Puts the object in the first position of the list. def prepend self.class.transaction do + save lock! return if first? if _siblings.count > 0 @@ -167,6 +168,7 @@ def prepend # Puts the object in the last position of the list. def push self.class.transaction do + save lock! append_to(_siblings.last_in_order) unless last? end @@ -175,6 +177,7 @@ def push # Puts the object right after another object in the list. def append_to(another) self.class.transaction do + save lock! return if another.next_id == id another.lock! @@ -194,6 +197,7 @@ def last? def last! self.class.transaction do + save lock! raise(ActiveRecord::RecordNotSaved) unless _siblings.last_in_order.update_attribute(:next_id, id) end @@ -203,9 +207,11 @@ def last! def delete_from_list if first? && self.next + self.next.save self.next.lock! raise(ActiveRecord::RecordNotSaved) unless self.next.update_attribute(:first, true) elsif previous + previous.save previous.lock! p = previous self.previous = nil unless frozen? From cdd41135e3ecb9d0609a7756dae673d406f34f9f Mon Sep 17 00:00:00 2001 From: Alic Szecsei Date: Fri, 17 Nov 2017 03:33:12 +0000 Subject: [PATCH 4/4] More deprecation fixes --- lib/resort.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/resort.rb b/lib/resort.rb index 1845dc2..b66e235 100644 --- a/lib/resort.rb +++ b/lib/resort.rb @@ -180,6 +180,7 @@ def append_to(another) save lock! return if another.next_id == id + another.save another.lock! delete_from_list if next_id || (another && another.next_id)