Undo working for assignments

This commit is contained in:
Jorge Manrubia
2025-05-06 09:25:52 +02:00
parent 7a51e13d7c
commit 3e8896ef93
13 changed files with 77 additions and 34 deletions
-1
View File
@@ -49,7 +49,6 @@
.terminal__menu {
display: none;
list-style: none;
transition: display 1000ms allow-discrete;
.terminal--open & {
display: flex;
@@ -0,0 +1,14 @@
class Commands::UndosController < ApplicationController
before_action :set_command
def create
@command.undo
@command.destroy
redirect_back_or_to root_path
end
private
def set_command
@command = Current.user.commands.find(params[:command_id])
end
end
@@ -12,6 +12,7 @@ export default class extends Controller {
}
remove() {
console.debug("CALLED!");
this.element.classList.remove(this.toggleClass)
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ class Command < ApplicationRecord
def undo
end
def can_undo?
def undoable?
false
end
+36 -15
View File
@@ -1,22 +1,8 @@
class Command::Assign < Command
store_accessor :data, :card_ids, :assignee_ids
store_accessor :data, :card_ids, :assignee_ids, :toggled_assignees_by_card
validates_presence_of :card_ids, :assignee_ids
def execute
transaction do
cards.each do |card|
assignees.each do |assignee|
card.toggle_assignment(assignee) unless card.assigned_to?(assignee)
end
end
end
end
def undo
raise "pending"
end
def title
card_description = if cards.one?
"card '#{cards.first.title}'"
@@ -29,6 +15,41 @@ class Command::Assign < Command
"Assign #{assignee_description} to #{card_description}"
end
def execute
toggled_assignees_by_card = {}
transaction do
cards.each do |card|
toggled_assignees_by_card[card.id] = []
assignees.each do |assignee|
unless card.assigned_to?(assignee)
toggled_assignees_by_card[card.id] << assignee.id
card.toggle_assignment(assignee)
end
end
end
update! toggled_assignees_by_card: toggled_assignees_by_card
end
end
def undo
toggled_assignees_by_card.each do |card_id, assignee_ids|
card = user.accessible_cards.find_by_id(card_id)
assignees = User.where(id: assignee_ids)
if card && assignees.any?
assignees.each do |assignee|
card.toggle_assignment(assignee) if card.assigned_to?(assignee)
end
end
end
end
def undoable?
true
end
private
def assignees
User.where(id: assignee_ids)
+4 -4
View File
@@ -1,14 +1,14 @@
class Command::GoToCard < Command
store_accessor :data, :card_id
def execute
redirect_to card
end
def title
"Visit card '#{card.title}'"
end
def execute
redirect_to card
end
private
def card
user.accessible_cards.find(card_id)
+4 -4
View File
@@ -3,14 +3,14 @@ class Command::GoToUser < Command
validates_presence_of :user_id
def execute
redirect_to user
end
def title
"View profile of '#{user.name}'"
end
def execute
redirect_to user
end
private
def user
User.find(user_id)
+4 -4
View File
@@ -1,11 +1,11 @@
class Command::Search < Command
store_accessor :data, :query
def execute
redirect_to cards_path("terms[]": query.presence)
end
def title
"Search '#{query}'"
end
def execute
redirect_to cards_path("terms[]": query.presence)
end
end
+5 -1
View File
@@ -1,3 +1,7 @@
<li class="min-width overflow-ellipsis">
<li class="min-width overflow-ellipsis flex">
<%= command.title %>
<% if command.undoable? %>
<%= button_to "Undo", command_undo_path(command), class: "btn btn--remove", data: { confirm: "Are you sure you want to undo '#{command.title}'?", turbo_frame: "_top" } %>
<% end %>
</li>
+1 -1
View File
@@ -1,7 +1,7 @@
<%= tag.div class: "terminal full-width flex flex-column gap justify-end", data: {
controller: "toggle-class commands",
toggle_class_toggle_class: "terminal--open" } do %>
<%= turbo_frame_tag :recent_commands, src: commands_path %>
<%= turbo_frame_tag :recent_commands, src: commands_path, refresh: "morph" %>
<%= form_tag commands_path,
class: "flex align-center gap-half",
+1 -1
View File
@@ -47,7 +47,7 @@
<%= yield :footer %>
<% if Current.user && !@hide_footer_frames %>
<div id="footer_frames" data-turbo-permanent="true">
<div id="footer_frames">
<%= render "notifications/tray" %>
<%= render "my/pins/tray" %>
<%= render "commands/terminal" %>
+1 -1
View File
@@ -3,7 +3,7 @@
<dialog class="tray notification-tray"
data-controller="dialog" data-dialog-modal-value="false" data-dialog-target="dialog"
data-action="keydown.esc->dialog#close:stop click@document->dialog#closeOnClickOutside">
<%= turbo_frame_tag "notifications", src: tray_notifications_path %>
<%= turbo_frame_tag "notifications", src: tray_notifications_path, refresh: "morph" %>
<%= link_to notifications_path, class: "tray__overflow border-radius txt-normal", data: { action: "click->dialog#close" } do %>
<div class="notification__content border-radius shadow fill-white flex align-center txt-align-center gap full-width border txt-ink txt-small">
+5 -1
View File
@@ -86,7 +86,11 @@ Rails.application.routes.draw do
end
end
resources :commands
resources :commands do
scope module: :commands do
resource :undo, only: :create
end
end
namespace :my do
resources :pins