Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Clarify semantics of ActiveRecord::Base#respond_to? Closes #2560.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2705 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3ab3a70b7cc2d40d8c0682f814e5f0bb0caf6213 1 parent d0a3185
@jeremy jeremy authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*1.12.1* (October 19th, 2005)
+* Clarify semantics of ActiveRecord::Base#respond_to? #2560 [skaes@web.de]
+
* Fixed Association#clear for associations which have not yet been accessed. #2524 [Patrick Lenz <patrick@lenz.sh>]
* HABTM finders shouldn't return readonly records. #2525 [Patrick Lenz <patrick@lenz.sh>]
View
39 activerecord/lib/active_record/base.rb
@@ -731,10 +731,11 @@ def content_columns
# is available.
def column_methods_hash
@dynamic_methods_hash ||= column_names.inject(Hash.new(false)) do |methods, attr|
- methods[attr.to_sym] = true
- methods["#{attr}=".to_sym] = true
- methods["#{attr}?".to_sym] = true
- methods["#{attr}_before_type_cast".to_sym] = true
+ attr_name = attr.to_s
+ methods[attr.to_sym] = attr_name
+ methods["#{attr}=".to_sym] = attr_name
+ methods["#{attr}?".to_sym] = attr_name
+ methods["#{attr}_before_type_cast".to_sym] = attr_name
methods
end
end
@@ -1089,8 +1090,8 @@ def validate_find_options(options)
def encode_quoted_value(value)
quoted_value = connection.quote(value)
- quoted_value = "'#{quoted_value[1..-2].gsub(/\'/, "\\\\'")}'" if quoted_value.include?("\\\'")
- quoted_value
+ quoted_value = "'#{quoted_value[1..-2].gsub(/\'/, "\\\\'")}'" if quoted_value.include?("\\\'") # (for ruby mode) "
+ quoted_value
end
end
@@ -1269,6 +1270,11 @@ def attribute_present?(attribute)
!value.blank? or value == 0
end
+ # Returns true if the given attribute is in the attributes hash
+ def has_attribute?(attr_name)
+ @attributes.has_key?(attr_name.to_s)
+ end
+
# Returns an array of names for the attributes available on this object sorted alphabetically.
def attribute_names
@attributes.keys.sort
@@ -1304,7 +1310,17 @@ def hash
# A Person object with a name attribute can ask person.respond_to?("name"), person.respond_to?("name="), and
# person.respond_to?("name?") which will all return true.
def respond_to?(method, include_priv = false)
- self.class.column_methods_hash[method.to_sym] || respond_to_without_attributes?(method, include_priv)
+ if attr_name = self.class.column_methods_hash[method.to_sym]
+ return true if @attributes.include?(attr_name) || attr_name == self.class.primary_key
+ return false if self.class.read_methods.include?(attr_name)
+ elsif @attributes.include?(method_name = method.to_s)
+ return true
+ elsif md = /(=|\?|_before_type_cast)$/.match(method_name)
+ return true if @attributes.include?(md.pre_match)
+ end
+ # super must be called at the end of the method, because the inherited respond_to?
+ # would return true for generated readers, even if the attribute wasn't present
+ super
end
# Just freeze the attributes hash, such that associations are still accessible even on destroyed records.
@@ -1424,7 +1440,7 @@ def read_attribute_before_type_cast(attr_name)
# ActiveRecord::Base.generate_read_methods is set to true.
def define_read_methods
self.class.columns_hash.each do |name, column|
- unless column.primary || self.class.serialized_attributes[name] || respond_to_without_attributes?(name)
+ unless self.class.serialized_attributes[name] || respond_to_without_attributes?(name)
define_read_method(name.to_sym, name, column)
end
end
@@ -1434,15 +1450,12 @@ def define_read_methods
def define_read_method(symbol, attr_name, column)
cast_code = column.type_cast_code('v')
access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
- body = access_code
- # The following 3 lines behave exactly like method_missing if the
- # attribute isn't present.
unless symbol == :id
- body = body.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
+ access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
end
- self.class.class_eval("def #{symbol}; #{body} end")
+ self.class.class_eval("def #{symbol}; #{access_code}; end")
self.class.read_methods[attr_name] = true unless symbol == :id
end
View
16 activerecord/test/associations_test.rb
@@ -1099,6 +1099,22 @@ def test_adding_uses_explicit_values_on_join_table
assert_equal 3, project.access_level.to_i
end
+ def test_hatbm_attribute_access_and_respond_to
+ project = developers(:jamis).projects[0]
+ assert project.has_attribute?("name")
+ assert project.has_attribute?("joined_on")
+ assert project.has_attribute?("access_level")
+ assert project.respond_to?("name")
+ assert project.respond_to?("name=")
+ assert project.respond_to?("name?")
+ assert project.respond_to?("joined_on")
+ assert project.respond_to?("joined_on=")
+ assert project.respond_to?("joined_on?")
+ assert project.respond_to?("access_level")
+ assert project.respond_to?("access_level=")
+ assert project.respond_to?("access_level?")
+ end
+
def test_habtm_adding_before_save
no_of_devels = Developer.count
no_of_projects = Project.count
View
7 activerecord/test/base_test.rb
@@ -211,6 +211,13 @@ def test_reader_generation
end
end
+ def test_non_attribute_access_and_assignment
+ topic = Topic.new
+ assert !topic.respond_to?("mumbo")
+ assert_raises(NoMethodError) { topic.mumbo }
+ assert_raises(NoMethodError) { topic.mumbo = 5 }
+ end
+
def test_preserving_date_objects
# SQL Server doesn't have a separate column type just for dates, so all are returned as time
if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter
View
8 activerecord/test/finder_test.rb
@@ -96,7 +96,13 @@ def test_unexisting_record_exception_handling
end
def test_find_only_some_columns
- assert_raises(NoMethodError) { Topic.find(1, :select => "author_name").title }
+ topic = Topic.find(1, :select => "author_name")
+ assert_raises(NoMethodError) { topic.title }
+ assert_equal "David", topic.author_name
+ assert !topic.attribute_present?("title")
+ assert !topic.respond_to?("title")
+ assert topic.attribute_present?("author_name")
+ assert topic.respond_to?("author_name")
end
def test_find_on_conditions
View
2  railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -130,7 +130,7 @@ def add_options!(opt)
)
MYSQL_SOCKET_LOCATIONS = [ "/tmp/mysql.sock", #default
- "/var/run/mysqld/mysqld.sock", #debian
+ "/var/run/mysqld/mysqld.sock", #debian/gentoo
"/var/tmp/mysql.sock", # freebsd
"/var/lib/mysql/mysql.sock" ] #fedora
end
Please sign in to comment.
Something went wrong with that request. Please try again.