Permalink
Browse files

r3167@asus: jeremy | 2005-11-18 22:47:31 -0800

 Apply [3089] to stable.  Don't generate read methods for columns whose names are not valid ruby method names.  Closes #2946.


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@3090 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent beddd96 commit b8ea04f8e8f77a9eb9cf249fa5fc8dbcd2a66eaa @jeremy jeremy committed Nov 19, 2005
Showing with 19 additions and 1 deletion.
  1. +2 −0 activerecord/CHANGELOG
  2. +10 −1 activerecord/lib/active_record/base.rb
  3. +7 −0 activerecord/test/base_test.rb
@@ -1,5 +1,7 @@
*SVN*
+* Don't generate read methods for columns whose names are not valid ruby method names. #2946 [Stefan Kaes]
+
* Document :force option to create_table. #2921 [Blair Zajac <blair@orcaware.com>]
* Don't add the same conditions twice in has_one finder sql. #2916 [Jeremy Evans]
@@ -1556,7 +1556,16 @@ def define_read_method(symbol, attr_name, column)
self.class.read_methods << attr_name
end
- self.class.class_eval("def #{symbol}; #{access_code}; end")
+ begin
+ self.class.class_eval("def #{symbol}; #{access_code}; end")
+ rescue SyntaxError => err
+ self.class.read_methods.delete(attr_name)
+ if logger
+ logger.warn "Exception occured during reader method compilation."
+ logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?"
+ logger.warn "#{err.message}"
+ end
+ end
end
# Returns true if the attribute is of a text column and marked for serialization.
@@ -221,6 +221,13 @@ def test_reader_generation
end
end
+ def test_reader_for_invalid_column_names
+ # column names which aren't legal ruby ids
+ topic = Topic.find(:first)
+ topic.send(:define_read_method, "mumub-jumbo".to_sym, "mumub-jumbo", nil)
+ assert !Topic.read_methods.include?("mumub-jumbo")
+ end
+
def test_non_attribute_access_and_assignment
topic = Topic.new
assert !topic.respond_to?("mumbo")

0 comments on commit b8ea04f

Please sign in to comment.