mirror of
https://github.com/github/rails.git
synced 2026-01-29 16:28:09 -05:00
Ensure that custom mutators aren't redefined by define_attribute_methods. [Koz]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7500 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -58,7 +58,7 @@ module ActiveRecord
|
||||
def define_attribute_methods
|
||||
return if generated_methods?
|
||||
columns_hash.each do |name, column|
|
||||
unless instance_methods.include?(name)
|
||||
unless instance_method_already_defined?(name)
|
||||
if self.serialized_attributes[name]
|
||||
define_read_method_for_serialized_attribute(name)
|
||||
else
|
||||
@@ -66,15 +66,22 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
unless instance_methods.include?("#{name}=")
|
||||
unless instance_method_already_defined?("#{name}=")
|
||||
define_write_method(name.to_sym)
|
||||
end
|
||||
|
||||
unless instance_methods.include?("#{name}?")
|
||||
unless instance_method_already_defined?("#{name}?")
|
||||
define_question_method(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def instance_method_already_defined?(method_name)
|
||||
method_defined?(method_name) ||
|
||||
private_method_defined?(method_name) ||
|
||||
protected_method_defined?(method_name)
|
||||
end
|
||||
|
||||
alias :define_read_methods :define_attribute_methods
|
||||
|
||||
|
||||
|
||||
@@ -394,6 +394,13 @@ class BasicsTest < Test::Unit::TestCase
|
||||
assert_equal 9900, Topic.find(2).written_on.usec
|
||||
end
|
||||
end
|
||||
|
||||
def test_custom_mutator
|
||||
topic = Topic.find(1)
|
||||
# This mutator is protected in the class definition
|
||||
topic.send(:approved=, true)
|
||||
assert topic.instance_variable_get("@custom_approved")
|
||||
end
|
||||
|
||||
def test_destroy
|
||||
topic = Topic.find(1)
|
||||
|
||||
6
activerecord/test/fixtures/topic.rb
vendored
6
activerecord/test/fixtures/topic.rb
vendored
@@ -13,8 +13,14 @@ class Topic < ActiveRecord::Base
|
||||
def topic_id
|
||||
id
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
def approved=(val)
|
||||
@custom_approved = val
|
||||
write_attribute(:approved, val)
|
||||
end
|
||||
|
||||
def default_written_on
|
||||
self.written_on = Time.now unless attribute_present?("written_on")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user