179ae45bf3
I went with placing htis logic at the command level since it plays well with the composite structure, where you don't know whether you are handling one command or many commands, and you want to aggregate all the errors. I didn't leverage the existing support with rails' model errors because that customizing those message is cumbersome (either i18n or using custom validation logic for everything).
72 lines
1.2 KiB
Ruby
72 lines
1.2 KiB
Ruby
class Command < ApplicationRecord
|
|
include Rails.application.routes.url_helpers
|
|
|
|
belongs_to :user
|
|
belongs_to :parent, class_name: "Command", optional: true
|
|
|
|
scope :root, -> { where(parent_id: nil) }
|
|
|
|
attribute :context
|
|
|
|
def title
|
|
model_name.human
|
|
end
|
|
|
|
def confirmation_prompt
|
|
title
|
|
end
|
|
|
|
def execute
|
|
end
|
|
|
|
def undo
|
|
end
|
|
|
|
def undo!
|
|
transaction do
|
|
undo
|
|
destroy
|
|
end
|
|
end
|
|
|
|
def undoable?
|
|
false
|
|
end
|
|
|
|
def needs_confirmation?
|
|
false
|
|
end
|
|
|
|
def error_messages
|
|
errors.to_hash.flat_map do |attribute, message|
|
|
error_message_for(attribute, message)
|
|
end.uniq
|
|
end
|
|
|
|
private
|
|
def redirect_to(...)
|
|
Command::Result::Redirection.new(...)
|
|
end
|
|
|
|
def error_message_for(attribute, message)
|
|
case attribute.to_sym
|
|
when :cards, :card_ids
|
|
"No cards in this context"
|
|
when :card
|
|
"Can't find the card"
|
|
when :collection
|
|
"The collection is missing"
|
|
when :assignee_ids
|
|
"Assignees are missing"
|
|
when :user
|
|
"Can't find that user"
|
|
when :stage
|
|
"Can't find that workflow stage"
|
|
when :tag_title
|
|
"A tag is required"
|
|
else
|
|
message
|
|
end
|
|
end
|
|
end
|