Sha256: 371a4494de3470f1d7020add5ecf1b09ef99e8c8ce3058649bafa709ec4735ae

Contents?: true

Size: 1.02 KB

Versions: 33

Compression:

Stored size: 1.02 KB

Contents

module ActiveModel
  class ErrorMessage < String
    attr_accessor :error_code, :data

    def initialize(message, error_code = nil, data = nil)
      super message
      @error_code = error_code
      @data = data
    end
  end

  class Errors
    def add(attribute, message = nil, options = {})
      # build error code from message symbol
      error_code = normalize_error_code(attribute, message, options)
      message = normalize_message(attribute, message, options)
      if exception = options[:strict]
        exception = ActiveModel::StrictValidationFailed if exception == true
        raise exception, full_message(attribute, message)
      end

      # use customized String subclass with "error_code" attribute
      self[attribute] << ErrorMessage.new(message, error_code, options[:data])
    end

    def normalize_error_code(_attribute, message, options)
      if !options[:error_code].blank?
        options[:error_code]
      elsif message.class == Symbol
        message
      else
        :invalid
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
releaf-core-2.0.1 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-2.0.0 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.22 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.21 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.20 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.19 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.18 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.17 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.16 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.15 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.14 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.13 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.12 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.11 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.10 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.9 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.8 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.7 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.6 lib/releaf/rails_ext/validation_error_codes.rb
releaf-core-1.1.5 lib/releaf/rails_ext/validation_error_codes.rb