Files
fizzy/app/models/command/composite.rb
T
Jorge Manrubia 2510c1e0ac Major rework of the command/ai abstractions
- Big simplification of the overall workflow.
- Composite commands store their commands properly, these are undoable now.
- Child commands are excluded from history
2025-05-16 13:29:18 +02:00

40 lines
814 B
Ruby

# A composite of commands
class Command::Composite < Command
store_accessor :data, :title
has_many :commands, inverse_of: :parent, dependent: :destroy
def execute
ApplicationRecord.transaction do
commands.collect { it.execute }
end
end
def undo
ApplicationRecord.transaction do
undoable_commands.reverse.each(&:undo)
end
end
def undoable?
undoable_commands.any?
end
def confirmation_prompt
commands_excluding_redirections.collect(&:confirmation_prompt).to_sentence
end
def needs_confirmation?
commands.any?(&:needs_confirmation?)
end
private
def commands_excluding_redirections
commands.reject { it.is_a?(Command::VisitUrl) }
end
def undoable_commands
@undoable_commands ||= commands.filter(&:undoable?)
end
end