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
This commit is contained in:
Marcel Molina 2005-10-06 23:19:55 +00:00
parent e8b427cdef
commit c0899bca10
4 changed files with 34 additions and 8 deletions

@ -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 <chad@chadfowler.com>, Nicholas Seckar]
* Added :force option to create_table that'll try to drop the table if it already exists before creating

@ -551,14 +551,19 @@ def table_name
# 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 @@ def class_name(table_name = table_name) # :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 @@ def column_names
# 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 @@ def attributes_protected_by_default
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

@ -5,6 +5,7 @@ module ConnectionAdapters #:nodoc:
# 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 @@ def initialize(name, default, sql_type = nil, null = true)
@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.

@ -44,12 +44,15 @@ def test_find_with_more_than_one_string_key
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