aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/base.rb
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2005-10-06 23:19:55 +0000
committerMarcel Molina <marcel@vernix.org>2005-10-06 23:19:55 +0000
commitc0899bca10af443d3aba00d75c554b96d4bccdab (patch)
tree584bbc01fb922528839d62022ac8a38725479b36 /activerecord/lib/active_record/base.rb
parente8b427cdefd9ec851e2853d4bf21dd8fd676096d (diff)
downloadrails-c0899bca10af443d3aba00d75c554b96d4bccdab.tar.gz
rails-c0899bca10af443d3aba00d75c554b96d4bccdab.tar.bz2
rails-c0899bca10af443d3aba00d75c554b96d4bccdab.zip
Add convenience predicate methods on Column class. In partial fullfilment of #1236.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2482 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rwxr-xr-xactiverecord/lib/active_record/base.rb23
1 files changed, 16 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 8d7a49e46b..f6219a5a98 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -551,14 +551,19 @@ module ActiveRecord #:nodoc:
# 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
+ key = 'id'
case primary_key_prefix_type
when :table_name
- Inflector.foreign_key(class_name_of_active_record_descendant(self), false)
+ key = Inflector.foreign_key(class_name_of_active_record_descendant(self), false)
when :table_name_with_underscore
- Inflector.foreign_key(class_name_of_active_record_descendant(self))
- else
- "id"
+ key = Inflector.foreign_key(class_name_of_active_record_descendant(self))
end
+ set_primary_key(key)
+ key
end
# Defines the column name for use with single table inheritance -- can be overridden in subclasses.
@@ -643,7 +648,11 @@ module ActiveRecord #:nodoc:
# Returns an array of column objects for the table associated with this class.
def columns
- @columns ||= connection.columns(table_name, "#{name} Columns")
+ unless @columns
+ @columns = connection.columns(table_name, "#{name} Columns")
+ @columns.each {|column| column.primary = column.name == primary_key}
+ end
+ @columns
end
# Returns an array of column objects for the table associated with this class.
@@ -658,7 +667,7 @@ module ActiveRecord #:nodoc:
# Returns an array of columns objects where the primary id, all columns ending in "_id" or "_count",
# and columns used for single table inheritance has been removed.
def content_columns
- @content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
+ @content_columns ||= columns.reject { |c| c.primary || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
end
# Returns a hash of all the methods added to query each of the columns in the table with the name of the method as the key
@@ -1372,7 +1381,7 @@ module ActiveRecord #:nodoc:
def attributes_with_quotes(include_primary_key = true)
attributes.inject({}) do |quoted, (name, value)|
if column = column_for_attribute(name)
- quoted[name] = quote(value, column) unless !include_primary_key && name == self.class.primary_key
+ quoted[name] = quote(value, column) unless !include_primary_key && column.primary
end
quoted
end