public
Fork of NZKoz/koz-rails
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/eventualbuddha/koz-rails.git
Search Repo:
Allow aliased attributes in ActiveRecord dynamic finders.
Fri Mar 14 17:03:06 -0700 2008
commit  cd2ede9e0c5fd33980605a367b04abd2d8027529
tree    c6c7289d304c762dd38b44569c562b866a865417
parent  b4ca01d58828964492644ad24ca96850351008e2
...
1139
1140
1141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1142
1143
1144
...
1558
1559
1560
1561
 
 
 
 
 
 
 
1562
1563
1564
...
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
...
1583
1584
1585
 
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
0
@@ -1139,6 +1139,31 @@
0
         attribute_key_name.humanize
0
       end
0
 
0
+ # Allows you to make aliases for attributes, which includes
0
+ # getter, setter, query, and finder methods.
0
+ #
0
+ # Example:
0
+ #
0
+ # class Content < ActiveRecord::Base
0
+ # # has a title attribute
0
+ # end
0
+ #
0
+ # class Email < Content
0
+ # alias_attribute :subject, :title
0
+ # end
0
+ #
0
+ # e = Email.find(1)
0
+ # e.title # => "Superstars"
0
+ # e.subject # => "Superstars"
0
+ # e.subject? # => true
0
+ # e.subject = "Megastars"
0
+ # e.title # => "Megastars"
0
+ # e == Email.find_by_subject("Superstars") # => true
0
+ def alias_attribute(new_name, old_name)
0
+ super
0
+ attribute_aliases[new_name.to_s] = old_name.to_s
0
+ end
0
+
0
       # True if this isn't a concrete subclass needing a STI type condition.
0
       def descends_from_active_record?
0
         if superclass.abstract_class?
0
@@ -1558,7 +1583,13 @@
0
         end
0
 
0
         def extract_attribute_names_from_match(match)
0
- match.captures.last.split('_and_')
0
+ match.captures.last.split('_and_').map do |attribute|
0
+ attribute_aliases[attribute] || attribute
0
+ end
0
+ end
0
+
0
+ def attribute_aliases
0
+ @@attribute_aliases ||= {}
0
         end
0
 
0
         def construct_attributes_from_arguments(attribute_names, arguments)
...
426
427
428
 
 
 
 
429
430
431
...
426
427
428
429
430
431
432
433
434
435
0
@@ -426,6 +426,10 @@
0
     assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC')
0
   end
0
 
0
+ def test_find_by_aliased_attribute
0
+ assert_equal topics(:first), Topic.find_by_author_email('david@loudthinking.com')
0
+ end
0
+
0
   def test_find_by_one_attribute_with_conditions
0
     assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
0
   end
...
14
15
16
 
17
18
19
...
14
15
16
17
18
19
20
0
@@ -14,6 +14,7 @@
0
     id
0
   end
0
 
0
+ alias_attribute :author_email, :author_email_address
0
 
0
   protected
0
     def approved=(val)

Comments

    No one has commented yet.