diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-07-31 16:21:07 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-07-31 16:21:07 -0300 |
commit | 3de59e916d6a3d4eab202cf0c99b1f88905a3b43 (patch) | |
tree | def6d6a808ebe187be1f37f8a739fd786cc11f02 /activerecord/lib/active_record/attribute_methods/primary_key.rb | |
parent | c1cbf02e3170f1004daf4a146cbc41176c2458d3 (diff) | |
parent | 62fd1d3716b4b5fd1d91cdcc77003efe80fc5a7e (diff) | |
download | rails-3de59e916d6a3d4eab202cf0c99b1f88905a3b43.tar.gz rails-3de59e916d6a3d4eab202cf0c99b1f88905a3b43.tar.bz2 rails-3de59e916d6a3d4eab202cf0c99b1f88905a3b43.zip |
Merge commit 'rails/master'
Conflicts:
activerecord/lib/active_record/associations.rb
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/primary_key.rb')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/primary_key.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb new file mode 100644 index 0000000000..365fdeb55a --- /dev/null +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -0,0 +1,44 @@ +module ActiveRecord + module AttributeMethods + module PrimaryKey + extend ActiveSupport::Concern + + module ClassMethods + # Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the + # primary_key_prefix_type setting, though. + def primary_key + reset_primary_key + end + + def reset_primary_key #:nodoc: + key = get_primary_key(base_class.name) + set_primary_key(key) + key + end + + def get_primary_key(base_name) #:nodoc: + key = 'id' + case primary_key_prefix_type + when :table_name + key = base_name.to_s.foreign_key(false) + when :table_name_with_underscore + key = base_name.to_s.foreign_key + end + 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. + # + # class Project < ActiveRecord::Base + # set_primary_key "sysid" + # end + def set_primary_key(value = nil, &block) + define_attr_method :primary_key, value, &block + end + alias :primary_key= :set_primary_key + end + end + end +end |