Module: Controllers::Workflowable
- Extended by:
- ActiveSupport::Concern
- Included in:
- CampaignsController, CatalogItemsController, CommunicationsController, CreditMemosController, Crm::AgreementsController, Crm::CertificationsController, Crm::ContactTrainingTopicsController, Crm::EdiCommunicationLogsController, Crm::FloorPlanDisplaysController, Crm::LiabilityInsurancesController, Crm::PostCommentsController, Crm::SalesCommissionsController, Crm::ShowcasesController, Crm::TimeOffRequestsController, Crm::WebhookLogsController, Crm::WorkSchedulesController, CustomersController, DeliveriesController, EmployeeReviewsController, EmployeeTopicsController, InvoicesController, OpportunitiesController, OrdersController, OutgoingPaymentsController, PraisesController, PurchaseOrdersController, QuotesController, RmasController, RoomConfigurationsController, ShipmentsController, SupportCasesController, TopicsController, VouchersController
- Defined in:
- app/concerns/controllers/workflowable.rb
Overview
Shared state-machine action handling for CRM controllers: runs a workflow
event (from params[:wf_action]) against the record under a PostgreSQL
advisory lock, dispatching to a controller-defined handler method when one
exists, otherwise applying the event directly and responding with flash +
redirect (HTML) or turbo_stream.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#render_workflow_error_stream ⇒ void
Override in controllers to customize the turbo_stream error response.
-
#render_workflow_success_stream ⇒ void
Override in controllers to customize the turbo_stream success response.
-
#workflow_action ⇒ void
Entry-point action: loads the record, takes an advisory lock keyed on the record, then calls the controller's custom handler for the event when one is defined, else falls through to #workflow_action_complete.
-
#workflow_action_complete ⇒ void
Default event handler: applies the requested state event (plus optional note / record attributes) to the record and responds with a success or error flash, via turbo_stream redirect or HTML redirect.
Instance Method Details
#render_workflow_error_stream ⇒ void
This method returns an undefined value.
Override in controllers to customize the turbo_stream error response.
Default: redirect via turbo_stream so the behavior matches the HTML path.
94 95 96 |
# File 'app/concerns/controllers/workflowable.rb', line 94 def render_workflow_error_stream render turbo_stream: turbo_stream.redirect(@return_path || record_redirect_path) end |
#render_workflow_success_stream ⇒ void
This method returns an undefined value.
Override in controllers to customize the turbo_stream success response.
Default: redirect via turbo_stream so the behavior matches the HTML path.
86 87 88 |
# File 'app/concerns/controllers/workflowable.rb', line 86 def render_workflow_success_stream render turbo_stream: turbo_stream.redirect(@return_path || record_redirect_path) end |
#workflow_action ⇒ void
This method returns an undefined value.
Entry-point action: loads the record, takes an advisory lock keyed on the
record, then calls the controller's custom handler for the event when one
is defined, else falls through to #workflow_action_complete.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/concerns/controllers/workflowable.rb', line 22 def workflow_action # initialize all our goodies load_record lock_key = "#{@record.class.name.downcase}_#{@record.id}".freeze @record.with_advisory_lock(lock_key, timeout_seconds: 120) do # If our controller defines a custom handler for this event (e.g. a form), # call it. We check the controller's own public instance methods to avoid # matching inherited methods like :error from ActionController. if self.class.public_method_defined?(@event, false) send(@event) else workflow_action_complete end end end |
#workflow_action_complete ⇒ void
This method returns an undefined value.
Default event handler: applies the requested state event (plus optional
note / record attributes) to the record and responds with a success or
error flash, via turbo_stream redirect or HTML redirect.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/concerns/controllers/workflowable.rb', line 43 def workflow_action_complete load_record unless @event flash[:error] = "Event not specified" redirect_to_return_path_or_default record_redirect_path return end @record.quick_note(params[:note]) if params[:note].present? && @record.respond_to?(:quick_note) @record.attributes = params[:record] if params[:record] @record.state_event = @event if @record.save respond_to do |format| format.turbo_stream do flash.now[:info] = "Status updated to #{@record.human_state_name}" render_workflow_success_stream end format.html do flash[:info] = "Status updated to #{@record.human_state_name}" redirect_to_return_path_or_default record_redirect_path end end elsif self.class.public_method_defined?(@event, false) send(@event) else respond_to do |format| format.turbo_stream do flash.now[:error] = @record.errors_to_s render_workflow_error_stream end format.html do flash[:error] = @record.errors_to_s redirect_to_return_path_or_default record_redirect_path end end end end |