From 30679bc705c74fcf1d26df275134f7b655a288a8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 18 Feb 2011 15:51:39 -0800 Subject: AR::AttributeMethods does not need to be included in an AR::Base class. --- .../lib/active_record/attribute_methods.rb | 6 ++- .../test/cases/attribute_methods/read_test.rb | 62 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 activerecord/test/cases/attribute_methods/read_test.rb (limited to 'activerecord') diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 2c5db51f7f..40ff273846 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -18,7 +18,11 @@ module ActiveRecord # method is defined by Active Record though. def instance_method_already_implemented?(method_name) method_name = method_name.to_s - @_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.instance_methods(false) | m.private_instance_methods(false) }.map {|m| m.to_s }.to_set + index = ancestors.index(ActiveRecord::Base) || ancestors.length + @_defined_class_methods ||= ancestors.first(index).map { |m| + m.instance_methods(false) | m.private_instance_methods(false) + }.flatten.map {|m| m.to_s }.to_set + @@_defined_activerecord_methods ||= defined_activerecord_methods raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name) @_defined_class_methods.include?(method_name) diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb new file mode 100644 index 0000000000..3dc69ea35e --- /dev/null +++ b/activerecord/test/cases/attribute_methods/read_test.rb @@ -0,0 +1,62 @@ +require "cases/helper" + +module ActiveRecord + module AttributeMethods + class ReadTest < ActiveRecord::TestCase + class FakeColumn < Struct.new(:name) + def type_cast_code(var) + var + end + + def type; :integer; end + end + + def setup + @klass = Class.new do + include ActiveRecord::AttributeMethods + include ActiveRecord::AttributeMethods::Read + + def self.column_names + %w{ one two three } + end + + def self.primary_key + end + + def self.columns + column_names.map { FakeColumn.new(name) } + end + + def self.columns_hash + puts caller + Hash[column_names.map { |name| + [name, FakeColumn.new(name)] + }] + end + + def self.serialized_attributes; {}; end + end + end + + def test_define_attribute_methods + instance = @klass.new + + @klass.column_names.each do |name| + assert ! instance.methods.map(&:to_s).include?(name) + end + + @klass.define_attribute_methods + + @klass.column_names.each do |name| + assert(instance.methods.map(&:to_s).include?(name), "#{name} is not defined") + end + end + + def test_attribute_methods_generated? + assert(!@klass.attribute_methods_generated?, 'attribute_methods_generated?') + @klass.define_attribute_methods + assert(@klass.attribute_methods_generated?, 'attribute_methods_generated?') + end + end + end +end -- cgit v1.2.3