From 3ab3a70b7cc2d40d8c0682f814e5f0bb0caf6213 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 22 Oct 2005 16:43:39 +0000 Subject: 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 --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 39 ++++++++++++++-------- activerecord/test/associations_test.rb | 16 +++++++++ activerecord/test/base_test.rb | 7 ++++ activerecord/test/finder_test.rb | 8 ++++- .../generators/applications/app/app_generator.rb | 2 +- 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 6478436bd0..e669b476dc 100644 --- a/activerecord/CHANGELOG +++ b/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 ] * HABTM finders shouldn't return readonly records. #2525 [Patrick Lenz ] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index aee5c4ede8..2f954eddc1 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -731,10 +731,11 @@ module ActiveRecord #:nodoc: # 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 @@ module ActiveRecord #:nodoc: 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 @@ module ActiveRecord #:nodoc: !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 @@ module ActiveRecord #:nodoc: # 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 @@ module ActiveRecord #:nodoc: # 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 @@ module ActiveRecord #:nodoc: 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 diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 2d7ebc2b42..70f0eac7ee 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1099,6 +1099,22 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase 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 diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index e4204c76cc..2a7c2ed670 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -211,6 +211,13 @@ class BasicsTest < Test::Unit::TestCase 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 diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index 073eea92ad..5ac6f2348c 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -96,7 +96,13 @@ class FinderTest < Test::Unit::TestCase 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 diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb index 57979804fd..2e26101c6a 100644 --- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb +++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb @@ -130,7 +130,7 @@ class AppGenerator < Rails::Generator::Base ) 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 -- cgit v1.2.3