Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a raw error message config option #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/optimism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down