Module: SteelWheel::SkipActiveModelErrorsKeys
- Defined in:
- lib/steel_wheel/skip_active_model_errors_keys.rb
Overview
Allows to treat some error codes as :base
EX:
generic_validation_keys :not_found
errors.add(:not_found, 'Can not find')
errors.full_messages => ['Can not find']
not ['Not found an not find']
Class Method Summary collapse
-
.[](*skip_keys) ⇒ Object
rubocop:enable Metrics/MethodLength.
- .build_module ⇒ Object
-
.patch_errors_method_on_instance(mod, klass) ⇒ Object
rubocop:disable Metrics/MethodLength.
Class Method Details
.[](*skip_keys) ⇒ Object
rubocop:enable Metrics/MethodLength
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/steel_wheel/skip_active_model_errors_keys.rb', line 41 def self.[](*skip_keys) mod = build_module mod.skip_keys = skip_keys builder = self mod.module_eval do define_singleton_method(:included) do |klass| builder.patch_errors_method_on_instance(self, klass) end end mod end |
.build_module ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/steel_wheel/skip_active_model_errors_keys.rb', line 11 def self.build_module Module.new do class << self attr_accessor :skip_keys def extended(klass) klass.include self end end end end |
.patch_errors_method_on_instance(mod, klass) ⇒ Object
rubocop:disable Metrics/MethodLength
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/steel_wheel/skip_active_model_errors_keys.rb', line 24 def self.patch_errors_method_on_instance(mod, klass) class << klass alias_method :__new__, :new end klass.define_singleton_method :new do |*args| instance = __new__(*args) instance.errors.define_singleton_method :full_message do |attribute, | return if mod.skip_keys.include?(attribute) super(attribute, ) end instance end end |