From 1a474cc8e41522ae079871d297c0e61ee4f6ef35 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 29 Nov 2011 18:58:41 +0000 Subject: Deprecate set_primary_key in favour of self.primary_key= --- .../active_record/attribute_methods/primary_key.rb | 46 ++++++++++++---------- .../abstract/schema_statements.rb | 2 +- 2 files changed, 27 insertions(+), 21 deletions(-) (limited to 'activerecord/lib') 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 :id 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. # # [:options] -- cgit v1.2.3