diff --git a/lib/acts_as_list/active_record/acts/list.rb b/lib/acts_as_list/active_record/acts/list.rb index 003150f2..9c29ae6c 100644 --- a/lib/acts_as_list/active_record/acts/list.rb +++ b/lib/acts_as_list/active_record/acts/list.rb @@ -112,6 +112,7 @@ def add_new_at after_destroy :decrement_positions_on_lower_items before_update :check_scope after_update :update_positions + before_validation :check_top_position scope :in_list, lambda { where("#{table_name}.#{configuration[:column]} IS NOT NULL") } EOV @@ -441,6 +442,12 @@ def check_scope def reload_position self.reload end + + def check_top_position + if send(position_column) && send(position_column) < acts_as_list_top + self[position_column] = acts_as_list_top + end + end end end end diff --git a/test/shared_zero_based.rb b/test/shared_zero_based.rb index d684ae63..b448ea89 100644 --- a/test/shared_zero_based.rb +++ b/test/shared_zero_based.rb @@ -24,6 +24,11 @@ def test_insert assert_equal 0, new.pos assert new.first? assert new.last? + + new = ZeroBasedMixin.create(parent_id: 1, pos: -500) + assert_equal 0, new.pos + assert new.first? + assert new.last? end def test_reordering