5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-23 00:56:22 +00:00
panel/app/models/concerns/aasm_events_concern.rb

73 lines
2.2 KiB
Ruby
Raw Normal View History

2024-03-04 16:49:07 +00:00
# frozen_string_literal: true
module AasmEventsConcern
extend ActiveSupport::Concern
included do
# Todos los eventos de la máquina de estados
#
# @return [Array<Symbol>]
def self.events
aasm.events.map(&:name) - self::IGNORED_EVENTS
end
# Encuentra todos los eventos que se pueden ejecutar con el filtro
# actual.
#
# @return [Array<Symbol>]
def self.transitionable_events(current_state)
self.events.select do |event|
aasm.events.find { |x| x.name == event }.transitions_from_state? current_state
end
end
# Todos los estados de la máquina de estados
#
# @return [Array<Symbol>]
def self.states
aasm.states.map(&:name) - self::IGNORED_STATES
end
# Define un método que cambia el estado para todos los objetos del
# scope actual.
#
# @return [Bool] Si hubo al menos un error, devuelve false.
fix: no ignorar ningún evento closes #15366 closes #15367 closes #15368 closes #15369 closes #15370 closes #15371 closes #15372 closes #15373 closes #15374 closes #15375 closes #15376 closes #15377 closes #15378 closes #15379 closes #15380 closes #15381 closes #15382 closes #15383 closes #15384 closes #15385 closes #15386 closes #15387 closes #15388 closes #15389 closes #15390 closes #15391 closes #15392 closes #15393 closes #15394 closes #15395 closes #15396 closes #15397 closes #15398 closes #15399 closes #15400 closes #15401 closes #15402 closes #15403 closes #15404 closes #15405 closes #15406 closes #15407 closes #15408 closes #15409 closes #15410 closes #15411 closes #15412 closes #15413 closes #15414 closes #15415 closes #15416 closes #15417 closes #15418 closes #15419 closes #15420 closes #15421 closes #15422 closes #15423 closes #15424 closes #15425 closes #15426 closes #15427 closes #15428 closes #15429 closes #15430 closes #15431 closes #15432 closes #15433 closes #15434 closes #15435 closes #15436 closes #15437 closes #15438 closes #15439 closes #15440 closes #15441 closes #15442 closes #15443 closes #15444 closes #15445 closes #15446 closes #15447 closes #15448 closes #15449 closes #15450 closes #15451 closes #15452 closes #15453 closes #15454 closes #15455 closes #15456 closes #15457 closes #15458 closes #15459 closes #15460 closes #15461 closes #15462 closes #15463 closes #15464 closes #15465 closes #15466 closes #15467 closes #15468 closes #15469 closes #15470 closes #15471 closes #15472 closes #15473 closes #15477 closes #15478 closes #15479 closes #15480 closes #15481 closes #15482 closes #15483 closes #15484 closes #15485 closes #15486 closes #15487 closes #15488 closes #15489 closes #15490 closes #15491 closes #15492 closes #15493 closes #15494 closes #15495 closes #15496 closes #15497 closes #15498 closes #15499 closes #15500 closes #15501 closes #15502 closes #15503 closes #15504 closes #15505 closes #15506 closes #15507 closes #15508 closes #15509 closes #15510 closes #15511 closes #15512 closes #15513 closes #15514 closes #15515 closes #15516 closes #15517 closes #15518 closes #15519 closes #15520 closes #15521 closes #15522 closes #15523 closes #15524 closes #15525 closes #15526 closes #15527 closes #15528 closes #15529 closes #15530 closes #15531 closes #15532 closes #15533 closes #15534 closes #15535 closes #15536 closes #15537 closes #15538 closes #15539 closes #15540 closes #15541 closes #15542 closes #15543 closes #15544 closes #15545 closes #15546 closes #15547 closes #15548 closes #15549
2024-03-13 14:05:28 +00:00
self.aasm.events.map(&:name).each do |event|
define_singleton_method(:"#{event}_all!") do
success = true
self.find_each do |object|
object.public_send(:"#{event}!") if object.public_send(:"may_#{event}?")
rescue Exception => e
success = false
notify_exception! e, object
end
success
end
# Ejecuta la transición del evento en la base de datos sin
# ejecutar los callbacks, sin modificar los items del scope que no
# pueden transicionar.
#
# @return [Integer] Registros modificados
define_singleton_method(:"#{event}_all_without_callbacks!") do
aasm_event = self.aasm.events.find { |e| e.name == event }
to_state = aasm_event.transitions.map(&:to).first
from_states = aasm_event.transitions.map(&:from)
self.unscope(where: :aasm_state).where(aasm_state: from_states).update_all(aasm_state: to_state, updated_at: Time.now)
end
end
# Envía notificación de errores
#
# @param exception [Exception]
# @param record [ApplicationRecord]
def self.notify_exception!(exception, record)
ExceptionNotifier.notify_exception(exception, data: { record_type: record.class.name, record_id: record.id })
end
2024-03-04 16:49:07 +00:00
end
end