Retrospring/app/controllers/concerns/turbo_streamable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.1 KiB
Ruby
Raw Normal View History

2023-02-10 21:15:50 -08:00
# frozen_string_literal: true
module TurboStreamable
extend ActiveSupport::Concern
class_methods do
def turbo_stream_actions(*actions)
around_action :handle_error, only: actions
end
2023-02-10 21:15:50 -08:00
end
def render_toast(message, success = true)
turbo_stream.append("toasts", partial: "shared/toast", locals: { message:, success: })
end
private
def handle_error
yield
rescue Errors::Base => e
render_error I18n.t(e.locale_tag)
rescue KeyError, ActionController::ParameterMissing => e
2023-02-11 06:41:17 -08:00
render_error t("errors.parameter_error", parameter: e.instance_of?(KeyError) ? e.key : e.param.capitalize)
2023-02-10 21:15:50 -08:00
rescue Dry::Types::CoercionError, Dry::Types::ConstraintError
render_error t("errors.invalid_parameter")
rescue ActiveRecord::RecordInvalid => e
render_error e.record.errors.full_messages.flatten.join(" ")
2023-02-10 21:15:50 -08:00
rescue ActiveRecord::RecordNotFound
render_error t("errors.record_not_found")
end
def render_error(message)
respond_to do |format|
format.turbo_stream do
render turbo_stream: render_toast(message, false)
end
end
end
end