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

Changeset 4482

Show
Ignore:
Timestamp:
06/21/06 20:40:14 (2 years ago)
Author:
bitsweat
Message:

alias_method_chain preserves method punctuation so foo, foo?, and foo! may be chained with the same feature.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/validations.rb

    r4348 r4482  
    219219      base.class_eval do 
    220220        alias_method_chain :save, :validation 
    221         alias_method_chain :save!, :validation!        
     221        alias_method_chain :save!, :validation 
    222222        alias_method_chain :update_attribute, :validation_skipping 
    223223      end 
  • trunk/activesupport/CHANGELOG

    r4455 r4482  
    11*SVN* 
     2 
     3* alias_method_chain preserves method punctuation so foo, foo?, and foo! may be chained with the same feature. [Jeremy Kemper] 
     4    Example: 
     5      alias_method_chain :save!, :validation 
     6    is equivalent to 
     7      alias_method :save_without_validation!, :save! 
     8      alias_method :save!, :save_with_validation! 
    29 
    310* Enhance Symbol#to_proc so it works with list objects, such as multi-dimensional arrays. Closes #5295 [nov@yo.rim.or.jp].  Example: 
  • trunk/activesupport/lib/active_support/core_ext/module/aliasing.rb

    r4430 r4482  
    1010  # 
    1111  # And both aliases are set up for you. 
     12  # 
     13  # Query and bang methods (foo?, foo!) keep the same punctuation: 
     14  # 
     15  #   alias_method_chain :foo?, :feature 
     16  # 
     17  # is equivalent to 
     18  # 
     19  #   alias_method :foo_without_feature?, :foo? 
     20  #   alias_method :foo?, :foo_without_feature? 
     21  # 
     22  # so you can safely chain foo, foo?, and foo! with the same feature. 
    1223  def alias_method_chain(target, feature) 
    1324    # Strip out punctuation on predicates or bang methods since 
    1425    # e.g. target?_without_feature is not a valid method name. 
    15     aliased_target = target.to_s.sub(/[?!]/, '') 
    16     alias_method "#{aliased_target}_without_#{feature}", target 
    17     alias_method target, "#{aliased_target}_with_#{feature}
     26    aliased_target, punctuation = target.to_s.sub(/([?!])$/, ''), $1 
     27    alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target 
     28    alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}
    1829  end 
    1930end