mirror of
https://github.com/github/rails.git
synced 2026-02-02 02:04:59 -05:00
Let alias_attribute work with attributes with initial capital letters (legacy columns etc). Closes #8596 [mpalmer]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7195 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Let alias_attribute work with attributes with initial capital letters (legacy columns etc). Closes #8596 [mpalmer]
|
||||
|
||||
* Added Hash#except which is the inverse of Hash#slice -- return the hash except the keys that are specified [DHH]
|
||||
|
||||
* Added support for pluralization with a different starting letter than the singular version (cow/kine) #4929 [norri_b/hasmanyjosh]
|
||||
|
||||
@@ -62,8 +62,8 @@ class Module
|
||||
# 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}; self.#{old_name}; end
|
||||
def #{new_name}?; self.#{old_name}?; end
|
||||
def #{new_name}=(v); self.#{old_name} = v; end
|
||||
STR
|
||||
end
|
||||
|
||||
@@ -2,15 +2,20 @@ require File.dirname(__FILE__) + '/../../abstract_unit'
|
||||
|
||||
module AttributeAliasing
|
||||
class Content
|
||||
attr_accessor :title
|
||||
attr_accessor :title, :Data
|
||||
|
||||
def title?
|
||||
!title.nil?
|
||||
end
|
||||
|
||||
def Data?
|
||||
!self.Data.nil?
|
||||
end
|
||||
end
|
||||
|
||||
class Email < Content
|
||||
alias_attribute :subject, :title
|
||||
alias_attribute :body, :Data
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,4 +33,22 @@ class AttributeAliasingTest < Test::Unit::TestCase
|
||||
assert_equal "We got a long way to go", e.title
|
||||
assert e.title?
|
||||
end
|
||||
|
||||
def test_aliasing_to_uppercase_attributes
|
||||
# Although it's very un-Ruby, some people's AR-mapped tables have
|
||||
# upper-case attributes, and when people want to alias those names
|
||||
# to more sensible ones, everything goes *foof*.
|
||||
e = AttributeAliasing::Email.new
|
||||
|
||||
assert !e.body?
|
||||
assert !e.Data?
|
||||
|
||||
e.body = "No, really, this is not a joke."
|
||||
assert_equal "No, really, this is not a joke.", e.Data
|
||||
assert e.Data?
|
||||
|
||||
e.Data = "Uppercased methods are teh suck"
|
||||
assert_equal "Uppercased methods are teh suck", e.body
|
||||
assert e.body?
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user