Skip to content

Instantly share code, notes, and snippets.

@dorianmariecom
Created April 27, 2022 08:39
Show Gist options
  • Save dorianmariecom/0c045fd563d5672e1548051e96603bfc to your computer and use it in GitHub Desktop.
Save dorianmariecom/0c045fd563d5672e1548051e96603bfc to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class ErrorsService < ApplicationService
def initialize(error:, current_user: nil)
super(current_user: current_user)
@error = error
@class_name = error.record.class.name.to_s.underscore
@query = error.query.to_s.sub("?", "")
end
def response
if current_user&.email_verified?
response_for_verified_user
elsif current_user
response_for_user
else
response_for_visitor
end
end
private
attr_reader :error, :class_name, :query
def response_for_verified_user
{
url: url_for(:root),
alert:
I18n.t(
"errors.service.verified_user.#{class_name}.#{query}",
default: I18n.t("errors.service.verified_user.default")
)
}.to_struct
end
def response_for_user
{
url: url_for([current_user, :email_verification]),
alert:
I18n.t(
"errors.service.user.#{class_name}.#{query}",
default: I18n.t("errors.service.user.default")
)
}.to_struct
end
def response_for_visitor
{
url: url_for(:new_user),
alert:
I18n.t(
"errors.service.visitor.#{class_name}.#{query}",
default: I18n.t("errors.service.visitor.default")
)
}.to_struct
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment