Decision can belong to subsequently created bubble

This commit is contained in:
Kevin McConnell
2025-02-10 09:09:32 +00:00
parent 6832ba8f37
commit fd1556b298
4 changed files with 37 additions and 12 deletions
@@ -2,8 +2,6 @@ class Bubbles::RecoversController < ApplicationController
include BubbleScoped, BucketScoped
def create
if bubble = Bubble.recover_recently_abandoned_creation(Current.user)
redirect_to bubble
end
redirect_to @bubble.recover_abandoned_creation
end
end
+11 -8
View File
@@ -7,15 +7,13 @@ module Bubble::Statuses
scope :published_or_drafted_by, ->(user) { where(status: :published).or(where(status: :drafted, creator: user)) }
end
class_methods do
def recently_abandoned_creation(user)
creating.where(creator: user).where("created_at != updated_at").where(updated_at: 1.day.ago..).last
end
def can_recover_abandoned_creation?
abandoned_creations.where(updated_at: 1.day.ago..).any?
end
def recover_recently_abandoned_creation(user)
recently_abandoned_creation(user).tap do |bubble|
creating.where(creator: user).excluding(bubble).destroy_all
end
def recover_abandoned_creation
abandoned_creations.last.tap do |bubble|
Bubble.creating.where(creator: creator).excluding(bubble).destroy_all
end
end
@@ -29,4 +27,9 @@ module Bubble::Statuses
end
end
end
private
def abandoned_creations
Bubble.creating.where(creator: creator).where("created_at != updated_at").excluding(self)
end
end
+2 -1
View File
@@ -1,7 +1,7 @@
<% @page_title = @bubble.title %>
<% content_for :header do %>
<% if @bubble.creating? && (abandoned = Bubble.recently_abandoned_creation(Current.user)) && abandoned != @bubble %>
<% if @bubble.can_recover_abandoned_creation? %>
<div class="flex align-center gap-half fill-selected justify-center border-block margin-block-end"
style="--border-color: var(--color-selected-dark); --border-style: dashed; view-transition-name: draft-banner;">
You have an unsaved bubble. Would you like to continue where you left off?
@@ -10,6 +10,7 @@
<% end %>
</div>
<% end %>
<% if @bubble.drafted? %>
<div class="flex align-center gap-half fill-selected justify-center border-block margin-block-end"
style="--border-color: var(--color-selected-dark); --border-style: dashed; view-transition-name: draft-banner;">
+23
View File
@@ -24,4 +24,27 @@ class Bubble::StatusesTest < ActiveSupport::TestCase
assert_includes Bubble.published_or_drafted_by(users(:kevin)), bubble
assert_includes Bubble.published_or_drafted_by(users(:jz)), bubble
end
test "can_recover_abandoned_creation?" do
bubble = buckets(:writebook).bubbles.create! creator: users(:kevin)
unsaved_bubble = buckets(:writebook).bubbles.new creator: users(:kevin)
assert_not unsaved_bubble.can_recover_abandoned_creation?
bubble.update!(title: "Something worth keeping")
assert unsaved_bubble.can_recover_abandoned_creation?
end
test "recover_abandoned_creation" do
bubble_edited = buckets(:writebook).bubbles.create! creator: users(:kevin)
bubble_edited.update!(title: "Something worth keeping")
bubble_not_edited = buckets(:writebook).bubbles.create! creator: users(:kevin)
assert bubble_not_edited.can_recover_abandoned_creation?
assert_equal bubble_edited, bubble_not_edited.recover_abandoned_creation
assert_raises(ActiveRecord::RecordNotFound) { bubble_not_edited.reload }
end
end