Skip to content

Commit

Permalink
r3167@asus: jeremy | 2005-11-18 22:47:31 -0800
Browse files Browse the repository at this point in the history
 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
jeremy committed Nov 19, 2005
1 parent beddd96 commit b8ea04f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -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]
Expand Down
11 changes: 10 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/base_test.rb
Expand Up @@ -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")
Expand Down

0 comments on commit b8ea04f

Please sign in to comment.