Skip to content

Commit

Permalink
Added Module#alias_attribute [Jamis/DHH]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4653 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Aug 3, 2006
1 parent b5c2366 commit 9d00b0c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
17 changes: 17 additions & 0 deletions activesupport/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
*SVN*

* Added Module#alias_attribute [Jamis/DHH]. Example:

class Content < ActiveRecord::Base
# has a title attribute
end

class Email < ActiveRecord::Base
alias_attribute :subject, :title
end

e = Email.find(1)
e.title # => "Superstars"
e.subject # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title # => "Megastars"

* Deprecation: easier to work with warning behavior as procs; default behaviors for each environment so users needn't update env.rb; and testing pleasure with assert_deprecated, assert_not_deprecated. [Jeremy Kemper]
By default, test prints to $stderr, dev logs, production ignores.
Provide your own per-environment in e.g. config/environments/development.rb:
Expand Down
8 changes: 5 additions & 3 deletions activesupport/lib/active_support/core_ext/date/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ def to_date
def to_time(form = :local)
::Time.send(form, year, month, day)
end

alias :xmlschema :to_s

def xmlschema
to_time.xmlschema
end
end
end
end
end
end
27 changes: 27 additions & 0 deletions activesupport/lib/active_support/core_ext/module/aliasing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,31 @@ def alias_method_chain(target, feature)
alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
end

# Allows you to make aliases for attributes, which includes
# getter, setter, and query methods.
#
# Example:
#
# class Content < ActiveRecord::Base
# # has a title attribute
# end
#
# class Email < ActiveRecord::Base
# alias_attribute :subject, :title
# end
#
# e = Email.find(1)
# e.title # => "Superstars"
# e.subject # => "Superstars"
# e.subject? # => true
# e.subject = "Megastars"
# e.title # => "Megastars"
def alias_attribute(new_name, old_name)
module_eval <<-STR, __FILE__, __LINE__+1
def #{new_name}; #{old_name}; end
def #{new_name}?; #{old_name}?; end
def #{new_name}=(v); self.#{old_name} = v; end
STR
end
end
31 changes: 31 additions & 0 deletions activesupport/test/core_ext/module/attribute_aliasing_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.dirname(__FILE__) + '/../../abstract_unit'

module AttributeAliasing
class Content
attr_accessor :title

def title?
!title.nil?
end
end

class Email < Content
alias_attribute :subject, :title
end
end

class AttributeAliasingTest < Test::Unit::TestCase
def test_attribute_alias
e = AttributeAliasing::Email.new

assert !e.subject?

e.title = "Upgrade computer"
assert_equal "Upgrade computer", e.subject
assert e.subject?

e.subject = "We got a long way to go"
assert_equal "We got a long way to go", e.title
assert e.title?
end
end
1 change: 0 additions & 1 deletion activesupport/test/core_ext/module_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def quux_with_baz?
end

class MethodAliasingTest < Test::Unit::TestCase

def setup
Object.const_set(:FooClassWithBarMethod, Class.new)
FooClassWithBarMethod.send(:define_method, 'bar', Proc.new { 'bar' })
Expand Down

0 comments on commit 9d00b0c

Please sign in to comment.