diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/primary_key.rb | 46 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 28 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/primary_keys_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/models/country.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/dashboard.rb | 4 | ||||
-rw-r--r-- | activerecord/test/models/keyboard.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/minivan.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/mixed_case_monkey.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/owner.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/pet.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/speedometer.rb | 4 | ||||
-rw-r--r-- | activerecord/test/models/string_key_object.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/subscriber.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/toy.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/treaty.rb | 2 |
18 files changed, 71 insertions, 43 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index be2c761faf..f6a00ab0fe 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -5,6 +5,7 @@ * `set_table_name` * `set_inheritance_column` * `set_sequence_name` + * `set_primary_key` Use an assignment method instead. For example, instead of `set_table_name`, use `self.table_name=`: diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index a404a5edd7..93dae3ff86 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -22,11 +22,11 @@ module ActiveRecord end def reset_primary_key #:nodoc: - key = self == base_class ? get_primary_key(base_class.name) : - base_class.primary_key - - set_primary_key(key) - key + if self == base_class + self.primary_key = get_primary_key(base_class.name) + else + self.primary_key = base_class.primary_key + end end def get_primary_key(base_name) #:nodoc: @@ -46,27 +46,33 @@ module ActiveRecord end end - attr_accessor :original_primary_key - - # Attribute writer for the primary key column - def primary_key=(value) - @quoted_primary_key = nil - @primary_key = value + def original_primary_key #:nodoc: + deprecated_original_property_getter :primary_key end - # Sets the name of the primary key column to use to the given value, - # or (if the value is nil or false) to the value returned by the given - # block. + # Sets the name of the primary key column. # # class Project < ActiveRecord::Base - # set_primary_key "sysid" + # self.primary_key = "sysid" # end - def set_primary_key(value = nil, &block) + # + # You can also define the primary_key method yourself: + # + # class Project < ActiveRecord::Base + # def self.primary_key + # "foo_" + super + # end + # end + # Project.primary_key # => "foo_id" + def primary_key=(value) + @original_primary_key = @primary_key if defined?(@primary_key) + @primary_key = value && value.to_s + @quoted_primary_key = nil + end + + def set_primary_key(value = nil, &block) #:nodoc: + deprecated_property_setter :primary_key, value, block @quoted_primary_key = nil - @primary_key ||= '' - self.original_primary_key = @primary_key - value &&= value.to_s - self.primary_key = block_given? ? instance_eval(&block) : value end end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index faa42e2d19..ce4c5a1383 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -114,7 +114,7 @@ module ActiveRecord # Defaults to +id+. If <tt>:id</tt> is false this option is ignored. # # Also note that this just sets the primary key in the table. You additionally - # need to configure the primary key in the model via the +set_primary_key+ macro. + # need to configure the primary key in the model via +self.primary_key=+. # Models do NOT auto-detect the primary key from their table definition. # # [<tt>:options</tt>] diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 062cac7d52..8d16103672 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1498,17 +1498,41 @@ class BasicsTest < ActiveRecord::TestCase k = Class.new( ActiveRecord::Base ) k.primary_key = "foo" assert_equal "foo", k.primary_key - k.set_primary_key "bar" + + assert_deprecated do + k.set_primary_key "bar" + end assert_equal "bar", k.primary_key end def test_set_primary_key_with_block k = Class.new( ActiveRecord::Base ) k.primary_key = 'id' - k.set_primary_key { "sys_" + original_primary_key } + + assert_deprecated do + k.set_primary_key { "sys_" + original_primary_key } + end assert_equal "sys_id", k.primary_key end + def test_original_primary_key + k = Class.new(ActiveRecord::Base) + def k.name; "Foo"; end + k.primary_key = "bar" + + assert_deprecated do + assert_equal "id", k.original_primary_key + end + + k = Class.new(ActiveRecord::Base) + k.primary_key = "omg" + k.primary_key = "wtf" + + assert_deprecated do + assert_equal "omg", k.original_primary_key + end + end + def test_set_inheritance_column_with_value k = Class.new( ActiveRecord::Base ) k.inheritance_column = "foo" diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 05c4b15407..4514a26e57 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -1163,7 +1163,7 @@ class FinderTest < ActiveRecord::TestCase end def test_find_one_message_with_custom_primary_key - Toy.set_primary_key :name + Toy.primary_key = :name begin Toy.find 'Hello World!' rescue ActiveRecord::RecordNotFound => e diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 4bb5752096..cc8ffb5f27 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -142,8 +142,6 @@ class PrimaryKeysTest < ActiveRecord::TestCase assert_equal k.connection.quote_column_name("id"), k.quoted_primary_key k.primary_key = "foo" assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key - k.set_primary_key "bar" - assert_equal k.connection.quote_column_name("bar"), k.quoted_primary_key end end @@ -155,9 +153,8 @@ class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase connection = ActiveRecord::Base.remove_connection - model = Class.new(ActiveRecord::Base) do - set_primary_key 'foo' - end + model = Class.new(ActiveRecord::Base) + model.primary_key = 'foo' assert_equal 'foo', model.primary_key diff --git a/activerecord/test/models/country.rb b/activerecord/test/models/country.rb index 15e3a1de0b..7db9a4e731 100644 --- a/activerecord/test/models/country.rb +++ b/activerecord/test/models/country.rb @@ -1,6 +1,6 @@ class Country < ActiveRecord::Base - set_primary_key :country_id + self.primary_key = :country_id has_and_belongs_to_many :treaties diff --git a/activerecord/test/models/dashboard.rb b/activerecord/test/models/dashboard.rb index a8a25834b1..1b3b54545f 100644 --- a/activerecord/test/models/dashboard.rb +++ b/activerecord/test/models/dashboard.rb @@ -1,3 +1,3 @@ class Dashboard < ActiveRecord::Base - set_primary_key :dashboard_id -end
\ No newline at end of file + self.primary_key = :dashboard_id +end diff --git a/activerecord/test/models/keyboard.rb b/activerecord/test/models/keyboard.rb index 32a4a7fad0..39347e274e 100644 --- a/activerecord/test/models/keyboard.rb +++ b/activerecord/test/models/keyboard.rb @@ -1,3 +1,3 @@ class Keyboard < ActiveRecord::Base - set_primary_key 'key_number' + self.primary_key = 'key_number' end diff --git a/activerecord/test/models/minivan.rb b/activerecord/test/models/minivan.rb index 830cdb5796..4fe79720ad 100644 --- a/activerecord/test/models/minivan.rb +++ b/activerecord/test/models/minivan.rb @@ -1,5 +1,5 @@ class Minivan < ActiveRecord::Base - set_primary_key :minivan_id + self.primary_key = :minivan_id belongs_to :speedometer has_one :dashboard, :through => :speedometer diff --git a/activerecord/test/models/mixed_case_monkey.rb b/activerecord/test/models/mixed_case_monkey.rb index 853f2682b3..763baefd91 100644 --- a/activerecord/test/models/mixed_case_monkey.rb +++ b/activerecord/test/models/mixed_case_monkey.rb @@ -1,3 +1,3 @@ class MixedCaseMonkey < ActiveRecord::Base - set_primary_key 'monkeyID' + self.primary_key = 'monkeyID' end diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb index 5760b991ec..fea55f4535 100644 --- a/activerecord/test/models/owner.rb +++ b/activerecord/test/models/owner.rb @@ -1,5 +1,5 @@ class Owner < ActiveRecord::Base - set_primary_key :owner_id + self.primary_key = :owner_id has_many :pets has_many :toys, :through => :pets end diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb index 113826756a..3cd5bceed5 100644 --- a/activerecord/test/models/pet.rb +++ b/activerecord/test/models/pet.rb @@ -2,7 +2,7 @@ class Pet < ActiveRecord::Base attr_accessor :current_user - set_primary_key :pet_id + self.primary_key = :pet_id belongs_to :owner, :touch => true has_many :toys diff --git a/activerecord/test/models/speedometer.rb b/activerecord/test/models/speedometer.rb index 94743eff8e..0a7d38d8ec 100644 --- a/activerecord/test/models/speedometer.rb +++ b/activerecord/test/models/speedometer.rb @@ -1,4 +1,4 @@ class Speedometer < ActiveRecord::Base - set_primary_key :speedometer_id + self.primary_key = :speedometer_id belongs_to :dashboard -end
\ No newline at end of file +end diff --git a/activerecord/test/models/string_key_object.rb b/activerecord/test/models/string_key_object.rb index f8d4c6e0e4..f084ec1bdc 100644 --- a/activerecord/test/models/string_key_object.rb +++ b/activerecord/test/models/string_key_object.rb @@ -1,3 +1,3 @@ class StringKeyObject < ActiveRecord::Base - set_primary_key :id + self.primary_key = :id end diff --git a/activerecord/test/models/subscriber.rb b/activerecord/test/models/subscriber.rb index 5b78014e6f..76e85a0cd3 100644 --- a/activerecord/test/models/subscriber.rb +++ b/activerecord/test/models/subscriber.rb @@ -1,5 +1,5 @@ class Subscriber < ActiveRecord::Base - set_primary_key 'nick' + self.primary_key = 'nick' has_many :subscriptions has_many :books, :through => :subscriptions end diff --git a/activerecord/test/models/toy.rb b/activerecord/test/models/toy.rb index 6c45e99671..0377e50011 100644 --- a/activerecord/test/models/toy.rb +++ b/activerecord/test/models/toy.rb @@ -1,5 +1,5 @@ class Toy < ActiveRecord::Base - set_primary_key :toy_id + self.primary_key = :toy_id belongs_to :pet scope :with_pet, joins(:pet) diff --git a/activerecord/test/models/treaty.rb b/activerecord/test/models/treaty.rb index b46537f0d2..41fd1350f3 100644 --- a/activerecord/test/models/treaty.rb +++ b/activerecord/test/models/treaty.rb @@ -1,6 +1,6 @@ class Treaty < ActiveRecord::Base - set_primary_key :treaty_id + self.primary_key = :treaty_id has_and_belongs_to_many :countries |