Sha256: 522cc7f2afe26cfe797c6234e20c7547cf90af1f010b6c19fb248e937629c61b

Contents?: true

Size: 1010 Bytes

Versions: 3

Compression:

Stored size: 1010 Bytes

Contents

module SteelWheel
  module SkipActiveModelErrorsKeys
    def self.build_module
      mod = Module.new do
        class << self
          attr_accessor :skip_keys

          def extended(klass)
            klass.include self
          end
        end
      end
    end

    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, message|
          return message if mod.skip_keys.include?(attribute)
          super(attribute, message)
        end
        instance
      end
    end

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
steel_wheel-0.5.2 lib/steel_wheel/skip_active_model_errors_keys.rb
steel_wheel-0.5.1 lib/steel_wheel/skip_active_model_errors_keys.rb
steel_wheel-0.5.0 lib/steel_wheel/skip_active_model_errors_keys.rb