Skip to content
Browse files

Add convenience predicate methods on Column class. In partial fullfil…

…ment of #1236.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2482 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent e8b427c commit c0899bca10af443d3aba00d75c554b96d4bccdab Marcel Molina committed
View
2 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 <chad@chadfowler.com>, Nicholas Seckar]
* Added :force option to create_table that'll try to drop the table if it already exists before creating
View
23 activerecord/lib/active_record/base.rb
@@ -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
View
14 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -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.
View
3 activerecord/test/pk_test.rb
@@ -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

0 comments on commit c0899bc

Please sign in to comment.
Something went wrong with that request. Please try again.