2012-03-13 4 views
1

resolved 인 ActiveResource와 관련된 문제가 발생하여 많은 도움없이 내 응용 프로그램에 패치를 적용하려고했습니다.Monkey patching ActiveResource :: Errors

class ActiveResource::Errors < ActiveModel::Errors 
    # https://github.com/rails/rails/commit/b09b2a8401c18d1efff21b3919ac280470a6eb8b 
    def from_hash(messages, save_cache = false) 
     clear unless save_cache 

     messages.each do |(key,errors)| 
     errors.each do |error| 
      if @base.attributes.keys.include?(key) 
      add key, error 
      elsif key == 'base' 
      self[:base] << error 
      else 
      # reporting an error on an attribute not in attributes 
      # format and add themActive to base 
      self[:base] << "#{key.humanize} #{error}" 
      end 
     end 
     end 
    end 

    # Grabs errors from a json response. 
    def from_json(json, save_cache = false) 
     decoded = ActiveSupport::JSON.decode(json) || {} rescue {} 
     if decoded.kind_of?(Hash) && (decoded.has_key?('errors') || decoded.empty?) 
     errors = decoded['errors'] || {} 
     if errors.kind_of?(Array) 
      # 3.2.1-style with array of strings 
      ActiveSupport::Deprecation.warn('Returning errors as an array of strings is deprecated.') 
      from_array errors, save_cache 
     else 
      # 3.2.2+ style 
      from_hash errors, save_cache 
     end 
     else 
     # <3.2-style respond_with - lacks 'errors' key 
     ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.') 
     from_hash decoded, save_cache 
     end 
    end 
end 

을하지만 여전히 activeresource-3.2.2/lib/active_resource/validations.rb:31:in 'from_json'를 호출하는 것 같다 :

나는 다음과 같은 포함/설정/초기화에 파일을 추가했습니다. 원숭이가 이것을 어떻게 적절히 패치하는지에 대한 도움은 매우 감사 할 것입니다.

감사합니다.

답변

7

파일이 config에로드 된 후 레일스가 ActiveResource를 로딩하는 것이 원래의 정의로 덮어 쓰는 것이 문제라는 것을 알게되었습니다. 이 수정은 패치 된 코드를 정의하기 전에 필요한 파일을 요구하기 만합니다.

내 수정 된 코드 :

require 'active_resource/base' 
require 'active_resource/validations' 

module ActiveResource 
    class Errors 
    # https://github.com/rails/rails/commit/b09b2a8401c18d1efff21b3919ac280470a6eb8b 

    def from_hash(messages, save_cache = false) 
     clear unless save_cache 

     messages.each do |(key,errors)| 
     errors.each do |error| 
      if @base.attributes.keys.include?(key) 
      add key, error 
      elsif key == 'base' 
      self[:base] << error 
      else 
      # reporting an error on an attribute not in attributes 
      # format and add themActive to base 
      self[:base] << "#{key.humanize} #{error}" 
      end 
     end 
     end 
    end 

    # Grabs errors from a json response. 
    def from_json(json, save_cache = false) 
     decoded = ActiveSupport::JSON.decode(json) || {} rescue {} 
     if decoded.kind_of?(Hash) && (decoded.has_key?('errors') || decoded.empty?) 
     errors = decoded['errors'] || {} 
     if errors.kind_of?(Array) 
      # 3.2.1-style with array of strings 
      ActiveSupport::Deprecation.warn('Returning errors as an array of strings is deprecated.') 
      from_array errors, save_cache 
     else 
      # 3.2.2+ style 
      from_hash errors, save_cache 
     end 
     else 
     # <3.2-style respond_with - lacks 'errors' key 
     ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.') 
     from_hash decoded, save_cache 
     end 
    end 
    end 
end 
+0

정말 감사합니다. 마침내이 문제에 대한 해결책을 찾게되어 기쁩니다. –