From abc3e44ec78b72b5e8832ce03ac97ba3409d255f Mon Sep 17 00:00:00 2001 From: "Matt E. Patterson" Date: Sun, 5 Jul 2020 22:26:16 -0500 Subject: [PATCH] Adds a raw error message config option --- lib/optimism.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/optimism.rb b/lib/optimism.rb index b4f780b..a99d358 100644 --- a/lib/optimism.rb +++ b/lib/optimism.rb @@ -5,7 +5,7 @@ module Optimism include CableReady::Broadcaster class << self - mattr_accessor :channel, :form_class, :error_class, :disable_submit, :suffix, :emit_events, :add_css, :inject_inline, :container_selector, :error_selector, :form_selector, :submit_selector + mattr_accessor :channel, :form_class, :error_class, :disable_submit, :suffix, :emit_events, :add_css, :inject_inline, :container_selector, :error_selector, :form_selector, :submit_selector, :raw_error_messages self.channel = ->(context) { "OptimismChannel" } self.form_class = "invalid" self.error_class = "error" @@ -18,6 +18,7 @@ class << self self.error_selector = "#RESOURCE_ATTRIBUTE_error" self.form_selector = "#RESOURCE_form" self.submit_selector = "#RESOURCE_submit" + self.raw_error_messages = false end def self.configure(&block) @@ -80,7 +81,12 @@ def process_attribute(model, attribute, ancestry) end container_selector, error_selector = Optimism.container_selector.sub("RESOURCE", resource).sub("ATTRIBUTE", attribute), Optimism.error_selector.sub("RESOURCE", resource).sub("ATTRIBUTE", attribute) if model.errors.any? && model.errors.messages.map(&:first).include?(attribute.to_sym) - message = "#{model.errors.full_message(attribute.to_sym, model.errors.messages[attribute.to_sym].first)}#{Optimism.suffix}" + err_message = if Optimism.raw_error_messages + model.errors.messages[attribute.to_sym].first + else + model.errors.full_message(attribute.to_sym, model.errors.messages[attribute.to_sym].first) + end + message = "#{err_message}#{Optimism.suffix}" cable_ready[Optimism.channel[self]].dispatch_event(name: "optimism:attribute:invalid", detail: {resource: resource, attribute: attribute, text: message}) if Optimism.emit_events cable_ready[Optimism.channel[self]].add_css_class(selector: container_selector, name: Optimism.error_class) if Optimism.add_css cable_ready[Optimism.channel[self]].text_content(selector: error_selector, text: message) if Optimism.inject_inline