From c0899bca10af443d3aba00d75c554b96d4bccdab Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Thu, 6 Oct 2005 23:19:55 +0000 Subject: 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 --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 23 +++++++++++++++------- .../abstract/schema_definitions.rb | 14 ++++++++++++- activerecord/test/pk_test.rb | 3 +++ 4 files changed, 34 insertions(+), 8 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index ad085641fb..7100989b1e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add convenience predicate methods on Column class. In partial fullfilment of #1236. [skaes@web.de] + * Raise errors when invalid hash keys are passed to ActiveRecord::Base.find. #2363 [Chad Fowler , Nicholas Seckar] * Added :force option to create_table that'll try to drop the table if it already exists before creating 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 diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 36f282d22a..1a633dcc00 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -5,6 +5,7 @@ module ActiveRecord # An abstract definition of a column in a table. class Column attr_reader :name, :default, :type, :limit, :null + attr_accessor :primary # Instantiates a new column in the table. # @@ -16,7 +17,18 @@ module ActiveRecord @name, @type, @null = name, simplified_type(sql_type), null # have to do this one separately because type_cast depends on #type @default = type_cast(default) - @limit = extract_limit(sql_type) unless sql_type.nil? + @limit = extract_limit(sql_type) unless sql_type.nil? + @primary = nil + @text = [:string, :text].include? @type + @number = [:float, :integer].include? @type + end + + def text? + @text + end + + def number? + @number end # Returns the Ruby class that corresponds to the abstract data type. diff --git a/activerecord/test/pk_test.rb b/activerecord/test/pk_test.rb index 6c70988a26..fb07194743 100644 --- a/activerecord/test/pk_test.rb +++ b/activerecord/test/pk_test.rb @@ -44,12 +44,15 @@ class PrimaryKeysTest < Test::Unit::TestCase def test_primary_key_prefix ActiveRecord::Base.primary_key_prefix_type = :table_name + Topic.reset_primary_key assert_equal "topicid", Topic.primary_key ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore + Topic.reset_primary_key assert_equal "topic_id", Topic.primary_key ActiveRecord::Base.primary_key_prefix_type = nil + Topic.reset_primary_key assert_equal "id", Topic.primary_key end end -- cgit v1.2.3