Allow managing bucket subscriptions

This commit is contained in:
Kevin McConnell
2025-02-25 13:12:57 +00:00
parent f22742c431
commit a802b8de5a
6 changed files with 67 additions and 16 deletions
@@ -0,0 +1,11 @@
class Buckets::SubscriptionsController < ApplicationController
include BucketScoped
def update
if params[:subscribed] == "1"
@bucket.subscribe(Current.user)
else
@bucket.unsubscribe(Current.user)
end
end
end
+8
View File
@@ -11,4 +11,12 @@ module Subscribable
def subscribe(user)
subscriptions.create_or_find_by!(user: user)
end
def unsubscribe(user)
subscriptions.find_by(user: user)&.destroy!
end
def subscribed_by?(user)
subscriptions.exists?(user: user)
end
end
@@ -0,0 +1,18 @@
<%= turbo_frame_tag dom_id(bucket, :subscription) do %>
<%= form_with url: bucket_subscriptions_path(bucket), method: :put, data: { controller: "form"} do |form| %>
<div class="flex align-center gap-half pad-inline">
<h2 class="txt-medium overflow-ellipsis">
<%= bucket.name %>
</h2>
<hr class="separator--horizontal flex-item-grow" style="--border-color: var(--color-subtle-dark); --border-style: dashed" aria-hidden="true">
<label class="switch">
<%= form.checkbox :subscribed, { checked: bucket.subscribed_by?(Current.user), id: dom_id(bucket, :subscribed),
class: "switch__input", data: { action: "form#submit" } } %>
<span class="switch__btn round"></span>
<span class="for-screen-reader">Enable notifications for this bucket</span>
</label>
</div>
<% end %>
<% end %>
+1 -16
View File
@@ -20,20 +20,5 @@
<% end %>
<section class="panel center flex flex-column gap-half margin-block">
<% @buckets.each do | bucket | %>
<div class="flex align-center gap-half pad-inline">
<h2 class="txt-medium overflow-ellipsis">
<%= bucket.name %>
</h2>
<hr class="separator--horizontal flex-item-grow" style="--border-color: var(--color-subtle-dark); --border-style: dashed" aria-hidden="true">
<label for="notifications_setting" class="switch">
<input type="checkbox" id="notifications_setting" class="switch__input">
<span class="switch__btn round"></span>
<span class="for-screen-reader">Enable notifications for this bucket</span>
</label>
</div>
<% end %>
<%= render partial: "notifications/settings/bucket", collection: @buckets %>
</section>
+4
View File
@@ -33,6 +33,10 @@ Rails.application.routes.draw do
end
resources :buckets do
scope module: :buckets do
resource :subscriptions
end
resources :bubbles do
resources :boosts
resources :comments do
@@ -0,0 +1,25 @@
require "test_helper"
class Buckets::SubscriptionsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :jz
end
test "subscribing to a bucket" do
assert_changes -> { buckets(:writebook).subscribed_by?(users(:jz)) }, from: false, to: true do
put bucket_subscriptions_url(buckets(:writebook)), params: { subscribed: "1" }
end
assert_response :success
end
test "unsubscribing from a bucket" do
buckets(:writebook).subscribe(users(:jz))
assert_changes -> { buckets(:writebook).subscribed_by?(users(:jz)) }, from: true, to: false do
put bucket_subscriptions_url(buckets(:writebook)), params: { subscribed: "0" }
end
assert_response :success
end
end