aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb11
-rw-r--r--activerecord/test/cases/attribute_methods/read_test.rb61
-rw-r--r--activerecord/test/cases/autosave_association_test.rb10
3 files changed, 68 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 2c5db51f7f..284ae2bebc 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)
@@ -27,9 +31,8 @@ module ActiveRecord
def defined_activerecord_methods
active_record = ActiveRecord::Base
super_klass = ActiveRecord::Base.superclass
- methods = active_record.public_instance_methods - super_klass.public_instance_methods
- methods += active_record.private_instance_methods - super_klass.private_instance_methods
- methods += active_record.protected_instance_methods - super_klass.protected_instance_methods
+ methods = (active_record.instance_methods - super_klass.instance_methods) +
+ (active_record.private_instance_methods - super_klass.private_instance_methods)
methods.map {|m| m.to_s }.to_set
end
end
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..d0a9028264
--- /dev/null
+++ b/activerecord/test/cases/attribute_methods/read_test.rb
@@ -0,0 +1,61 @@
+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
+ 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
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index 0dfdaa3443..dbc5d71153 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -793,7 +793,6 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
def test_should_destroy_habtm_as_part_of_the_save_transaction_if_they_were_marked_for_destruction
2.times { |i| @pirate.parrots.create!(:name => "parrots_#{i}") }
-<<<<<<< HEAD
assert !@pirate.parrots.any? { |parrot| parrot.marked_for_destruction? }
@pirate.parrots.each { |parrot| parrot.mark_for_destruction }
@@ -809,15 +808,6 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
def test_should_skip_validation_on_habtm_if_marked_for_destruction
2.times { |i| @pirate.parrots.create!(:name => "parrots_#{i}") }
-=======
- # Stub the destroy method of the second child to raise an exception
- class << before.last
- def destroy(*args)
- super
- raise 'Oh noes!'
- end
- end
->>>>>>> 220cb107b672d65fdc0488d4ff310ab04b62b463
@pirate.parrots.each { |parrot| parrot.name = '' }
assert !@pirate.valid?