-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(contact): form is persisted in db before pushed to HS
- Loading branch information
Showing
10 changed files
with
207 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,80 @@ | ||
class ContactForm < ApplicationRecord | ||
attr_accessor :user | ||
attr_reader :options | ||
|
||
after_initialize :set_options | ||
before_validation :normalize_strings | ||
before_validation :sanitize_email | ||
before_save :add_default_tags | ||
|
||
validates :email, presence: true, strict_email: true, if: :require_email? | ||
validates :subject, presence: true | ||
validates :text, presence: true | ||
validates :question_type, presence: true | ||
|
||
has_one_attached :piece_jointe | ||
|
||
TYPE_INFO = 'procedure_info' | ||
TYPE_PERDU = 'lost_user' | ||
TYPE_INSTRUCTION = 'instruction_info' | ||
TYPE_AMELIORATION = 'product' | ||
TYPE_AUTRE = 'other' | ||
|
||
ADMIN_TYPE_RDV = 'admin_demande_rdv' | ||
ADMIN_TYPE_QUESTION = 'admin_question' | ||
ADMIN_TYPE_SOUCIS = 'admin_soucis' | ||
ADMIN_TYPE_PRODUIT = 'admin_suggestion_produit' | ||
ADMIN_TYPE_DEMANDE_COMPTE = 'admin_demande_compte' | ||
ADMIN_TYPE_AUTRE = 'admin_autre' | ||
|
||
def self.default_options | ||
[ | ||
[I18n.t(:question, scope: [:support, :index, TYPE_INFO]), TYPE_INFO, I18n.t("links.common.faq.contacter_service_en_charge_url")], | ||
[I18n.t(:question, scope: [:support, :index, TYPE_PERDU]), TYPE_PERDU, LISTE_DES_DEMARCHES_URL], | ||
[I18n.t(:question, scope: [:support, :index, TYPE_INSTRUCTION]), TYPE_INSTRUCTION, I18n.t("links.common.faq.ou_en_est_mon_dossier_url")], | ||
[I18n.t(:question, scope: [:support, :index, TYPE_AMELIORATION]), TYPE_AMELIORATION, FEATURE_UPVOTE_URL], | ||
[I18n.t(:question, scope: [:support, :index, TYPE_AUTRE]), TYPE_AUTRE] | ||
] | ||
end | ||
|
||
def self.admin_options | ||
[ | ||
[I18n.t(:question, scope: [:support, :admin, ADMIN_TYPE_QUESTION], app_name: Current.application_name), ADMIN_TYPE_QUESTION], | ||
[I18n.t(:question, scope: [:support, :admin, ADMIN_TYPE_RDV], app_name: Current.application_name), ADMIN_TYPE_RDV], | ||
[I18n.t(:question, scope: [:support, :admin, ADMIN_TYPE_SOUCIS], app_name: Current.application_name), ADMIN_TYPE_SOUCIS], | ||
[I18n.t(:question, scope: [:support, :admin, ADMIN_TYPE_PRODUIT]), ADMIN_TYPE_PRODUIT], | ||
[I18n.t(:question, scope: [:support, :admin, ADMIN_TYPE_DEMANDE_COMPTE]), ADMIN_TYPE_DEMANDE_COMPTE], | ||
[I18n.t(:question, scope: [:support, :admin, ADMIN_TYPE_AUTRE]), ADMIN_TYPE_AUTRE] | ||
] | ||
end | ||
|
||
def for_admin=(value) | ||
super(value) | ||
set_options | ||
end | ||
|
||
def create_conversation_later | ||
HelpscoutCreateConversationJob.perform_later(self) | ||
end | ||
|
||
def require_email? = user.blank? | ||
|
||
private | ||
|
||
def normalize_strings | ||
self.subject = subject&.strip | ||
self.text = text&.strip | ||
end | ||
|
||
def sanitize_email | ||
self.email = EmailSanitizableConcern::EmailSanitizer.sanitize(email) if email.present? | ||
end | ||
|
||
def add_default_tags | ||
self.tags = tags.push('contact form', question_type).uniq | ||
end | ||
|
||
def set_options | ||
@options = for_admin? ? self.class.admin_options : self.class.default_options | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.