Sha256: 937fe25a9bf258026f63be9d2ad516f20e82aef1696676fc0c9d3168e8e0f74b

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

module Xing
  module Services
    class ErrorConverter


      def initialize(am_object)
        @am_object = am_object
      end
      attr_reader :am_object

      # This is a terrible hack to preserve the semantic meaning of
      # different error types -- neccesary because ActiveModel::Errors
      # frustratingly translates semantic error messages through i18n
      # as soon as it gets them
      def json_errors
        @json_errors ||= begin
                           old_locale = I18n.locale
                           I18n.locale = "json"
                           am_object.valid?
                           error_hash = am_object.errors.to_hash.deep_dup
                           I18n.locale = old_locale
                           error_hash
                         end
      end

      def regular_errors
        @regular_errors ||= begin
                              am_object.valid?
                              am_object.errors.to_hash.deep_dup
                            end
      end

      def convert
        final_errors = {}
        json_errors.each_key do |key|
          final_errors[key] = {
            :type => json_errors[key][0],
            :message => regular_errors[key][0]
          }
        end
        final_errors
      end


    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
xing-backend-1.0.0.pre.beta lib/xing/services/error_converter.rb
xing-backend-0.0.25 lib/xing/services/error_converter.rb
xing-backend-0.0.23 lib/xing/services/error_converter.rb
xing-backend-0.0.22 lib/xing/services/error_converter.rb
xing-backend-0.0.21 lib/xing/services/error_converter.rb
xing-backend-0.0.20 lib/xing/services/error_converter.rb
xing-backend-0.0.19 lib/xing/services/error_converter.rb
xing-backend-0.0.18 lib/xing/services/error_converter.rb
xing-backend-0.0.17 lib/xing/services/error_converter.rb
xing-backend-0.0.16 lib/xing/services/error_converter.rb
xing-backend-0.0.15 lib/xing/services/error_converter.rb
xing-backend-0.0.14 lib/xing/services/error_converter.rb
xing-backend-0.0.13 lib/xing/services/error_converter.rb
xing-backend-0.0.12 lib/xing/services/error_converter.rb
xing-backend-0.0.11 lib/xing/services/error_converter.rb
xing-backend-0.0.10 lib/xing/services/error_converter.rb