Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11450: deprecation_warnings_for_the_masses.diff

File deprecation_warnings_for_the_masses.diff, 3.8 kB (added by thechrisoshow, 8 months ago)

includes docs and tests

  • activesupport/test/deprecation_test.rb

    old new  
    2424  def d; end 
    2525  def e; end 
    2626  deprecate :a, :b, :c => :e, :d => "you now need to do something extra for this one" 
     27   
     28  def custom 
     29    ActiveSupport::Deprecation.warn("Don't go using this method") 
     30  end 
    2731end 
    2832 
    2933 
     
    148152    assert_not_deprecated { error.message } 
    149153    assert_nil @last_message 
    150154  end 
     155   
     156  def test_deprecated_warning_with_custom_details_message 
     157    ActiveSupport::Deprecation.detail_message = "This message is so 90s" 
     158    assert_deprecated(/This message is so 90s/) {@dtc.custom} 
     159  ensure 
     160    ActiveSupport::Deprecation.detail_message = ActiveSupport::Deprecation.detail_message = ActiveSupport::Deprecation::DEFAULT_DETAIL_MESSAGE 
     161  end 
     162   
     163  def test_oldskool_deprecation_details 
     164    assert_deprecated(/#{ActiveSupport::Deprecation::DEFAULT_DETAIL_MESSAGE}/) {@dtc.custom} 
     165  end 
     166   
    151167end 
  • activesupport/lib/active_support/deprecation.rb

    old new  
    11require 'yaml' 
    22 
    33module ActiveSupport 
    4   module Deprecation #:nodoc: 
    5     mattr_accessor :debug 
     4  # 
     5  # ActiveSupport::Deprecation is used by the Rails code to create deprecation warnings. 
     6  # You can add deprecation warnings to your own code by calling  
     7  #   ActiveSupport::Deprecation.warn(message, callstack) 
     8  # Every deprecation message includes a standard detail message.  You can change this by putting something like the following at the bottom of environment.rb: 
     9  #   ActiveSupport::Deprecation.detail_message = "For more deprecation details see our webpage" 
     10  module Deprecation  
     11    mattr_accessor :debug #:nodoc: 
    612    self.debug = false 
     13     
     14    mattr_accessor :detail_message 
     15     
     16    # 
     17    # This is the default message that Rails adds to all of its deprecation messages. 
     18    # You can override it with your own message using ActiveSupport.Deprecation.detail_message  
     19    # This can be handy if you have a particular webpage or document that details all the deprecations. 
     20    DEFAULT_DETAIL_MESSAGE = "See http://www.rubyonrails.org/deprecation for details." 
     21    self.detail_message = DEFAULT_DETAIL_MESSAGE 
    722 
    823    # Choose the default warn behavior according to RAILS_ENV. 
    924    # Ignore deprecation warnings in production. 
     
    1833                         logger.debug callstack.join("\n  ") if debug 
    1934                       } 
    2035    } 
    21  
     36     
    2237    class << self 
     38      # Use warn to mark your methods as deprecated with a message. 
     39      # Pass in an appropriate <tt>message</tt> detailing what the user of the method should do instead. 
    2340      def warn(message = nil, callstack = caller) 
    2441        behavior.call(deprecation_message(callstack, message), callstack) if behavior && !silenced? 
    2542      end 
    2643 
    27       def default_behavior 
     44      def default_behavior #:nodoc: 
    2845        if defined?(RAILS_ENV) 
    2946          DEFAULT_BEHAVIORS[RAILS_ENV.to_s] 
    3047        else 
     
    5067 
    5168 
    5269      private 
    53         def deprecation_message(callstack, message = nil
     70        def deprecation_message(callstack, message = nil, details_message = nil
    5471          message ||= "You are using deprecated behavior which will be removed from Rails 2.0." 
    55           "DEPRECATION WARNING: #{message}  See http://www.rubyonrails.org/deprecation for details. #{deprecation_caller_message(callstack)}" 
     72          "DEPRECATION WARNING: #{message}  #{self.detail_message} #{deprecation_caller_message(callstack)}" 
    5673        end 
    5774 
    5875        def deprecation_caller_message(callstack)