提交 b8128854 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #29378 from kamipo/avoid_overwriting_id_attribute_methods

Avoid overwriting the methods of `AttributeMethods::PrimaryKey`
...@@ -57,16 +57,12 @@ def attribute_method?(attr_name) ...@@ -57,16 +57,12 @@ def attribute_method?(attr_name)
end end
module ClassMethods module ClassMethods
def define_method_attribute(attr_name) ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast id_was id_in_database).to_set
super
if attr_name == primary_key && attr_name != "id" def instance_method_already_implemented?(method_name)
generated_attribute_methods.send(:alias_method, :id, primary_key) super || primary_key && ID_ATTRIBUTE_METHODS.include?(method_name)
end
end end
ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast id_was id_in_database).to_set
def dangerous_attribute_method?(method_name) def dangerous_attribute_method?(method_name)
super && !ID_ATTRIBUTE_METHODS.include?(method_name) super && !ID_ATTRIBUTE_METHODS.include?(method_name)
end end
......
...@@ -46,7 +46,7 @@ def test_integer_key ...@@ -46,7 +46,7 @@ def test_integer_key
topic = Topic.new topic = Topic.new
topic.title = "New Topic" topic.title = "New Topic"
assert_nil topic.id assert_nil topic.id
assert_nothing_raised { topic.save! } topic.save!
id = topic.id id = topic.id
topicReloaded = Topic.find(id) topicReloaded = Topic.find(id)
...@@ -56,23 +56,30 @@ def test_integer_key ...@@ -56,23 +56,30 @@ def test_integer_key
def test_customized_primary_key_auto_assigns_on_save def test_customized_primary_key_auto_assigns_on_save
Keyboard.delete_all Keyboard.delete_all
keyboard = Keyboard.new(name: "HHKB") keyboard = Keyboard.new(name: "HHKB")
assert_nothing_raised { keyboard.save! } keyboard.save!
assert_equal keyboard.id, Keyboard.find_by_name("HHKB").id assert_equal keyboard.id, Keyboard.find_by_name("HHKB").id
end end
def test_customized_primary_key_can_be_get_before_saving def test_customized_primary_key_can_be_get_before_saving
keyboard = Keyboard.new keyboard = Keyboard.new
assert_nil keyboard.id assert_nil keyboard.id
assert_nothing_raised { assert_nil keyboard.key_number } assert_nil keyboard.key_number
end end
def test_customized_string_primary_key_settable_before_save def test_customized_string_primary_key_settable_before_save
subscriber = Subscriber.new subscriber = Subscriber.new
assert_nothing_raised { subscriber.id = "webster123" } subscriber.id = "webster123"
assert_equal "webster123", subscriber.id assert_equal "webster123", subscriber.id
assert_equal "webster123", subscriber.nick assert_equal "webster123", subscriber.nick
end end
def test_update_with_non_primary_key_id_column
subscriber = Subscriber.first
subscriber.update(update_count: 1)
subscriber.reload
assert_equal 1, subscriber.update_count
end
def test_string_key def test_string_key
subscriber = Subscriber.find(subscribers(:first).nick) subscriber = Subscriber.find(subscribers(:first).nick)
assert_equal(subscribers(:first).name, subscriber.name) assert_equal(subscribers(:first).name, subscriber.name)
...@@ -83,7 +90,7 @@ def test_string_key ...@@ -83,7 +90,7 @@ def test_string_key
subscriber.id = "jdoe" subscriber.id = "jdoe"
assert_equal("jdoe", subscriber.id) assert_equal("jdoe", subscriber.id)
subscriber.name = "John Doe" subscriber.name = "John Doe"
assert_nothing_raised { subscriber.save! } subscriber.save!
assert_equal("jdoe", subscriber.id) assert_equal("jdoe", subscriber.id)
subscriberReloaded = Subscriber.find("jdoe") subscriberReloaded = Subscriber.find("jdoe")
......
...@@ -320,7 +320,7 @@ def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added ...@@ -320,7 +320,7 @@ def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
output = standard_dump output = standard_dump
assert_match %r{create_table "subscribers", id: false}, output assert_match %r{create_table "string_key_objects", id: false}, output
end end
if ActiveRecord::Base.connection.supports_foreign_keys? if ActiveRecord::Base.connection.supports_foreign_keys?
......
...@@ -807,16 +807,18 @@ ...@@ -807,16 +807,18 @@
t.string :sponsorable_type t.string :sponsorable_type
end end
create_table :string_key_objects, id: false, primary_key: :id, force: true do |t| create_table :string_key_objects, id: false, force: true do |t|
t.string :id t.string :id, null: false
t.string :name t.string :name
t.integer :lock_version, null: false, default: 0 t.integer :lock_version, null: false, default: 0
t.index :id, unique: true
end end
create_table :subscribers, force: true, id: false do |t| create_table :subscribers, force: true do |t|
t.string :nick, null: false t.string :nick, null: false
t.string :name t.string :name
t.column :books_count, :integer, null: false, default: 0 t.integer :books_count, null: false, default: 0
t.integer :update_count, null: false, default: 0
t.index :nick, unique: true t.index :nick, unique: true
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册