aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-17 15:39:13 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-17 16:02:53 -0600
commit4d3e88fc757a1b6f6c468418af01dca677e41edf (patch)
tree82e1d7dc1267da9f65cdd9915459a508324e4bb3 /activerecord/lib
parent9f86780226c86fae30d59d04bd53449b8c7a1ad8 (diff)
downloadrails-4d3e88fc757a1b6f6c468418af01dca677e41edf.tar.gz
rails-4d3e88fc757a1b6f6c468418af01dca677e41edf.tar.bz2
rails-4d3e88fc757a1b6f6c468418af01dca677e41edf.zip
Don't type cast the default on the column
If we want to have type decorators mess with the attribute, but not the column, we need to stop type casting on the column. Where possible, we changed the tests to test the value of `column_defaults`, which is public API. `Column#default` is not.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb12
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb13
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb9
-rw-r--r--activerecord/lib/active_record/model_schema.rb10
4 files changed, 25 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
index 5a0efe49c7..9bd0401e40 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
@@ -23,7 +23,8 @@ module ActiveRecord
spec[:precision] = column.precision.inspect if column.precision
spec[:scale] = column.scale.inspect if column.scale
spec[:null] = 'false' unless column.null
- spec[:default] = column.type_cast_for_schema(column.default) if column.has_default?
+ spec[:default] = schema_default(column) if column.has_default?
+ spec.delete(:default) if spec[:default].nil?
spec
end
@@ -31,6 +32,15 @@ module ActiveRecord
def migration_keys
[:name, :limit, :precision, :scale, :default, :null]
end
+
+ private
+
+ def schema_default(column)
+ default = column.type_cast_from_database(column.default)
+ unless default.nil?
+ column.type_cast_for_schema(default)
+ end
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index a9b97d5919..3ef8878ad1 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -62,15 +62,14 @@ module ActiveRecord
@extra = extra
super(name, default, cast_type, sql_type, null)
assert_valid_default(default)
+ extract_default
end
- def default
- @default ||= if blob_or_text_column?
- null || strict ? nil : ''
- elsif missing_default_forged_as_empty_string?(@original_default)
- nil
- else
- super
+ def extract_default
+ if blob_or_text_column?
+ @default = null || strict ? nil : ''
+ elsif missing_default_forged_as_empty_string?(@default)
+ @default = nil
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index d629fca911..8be4678ace 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -13,7 +13,7 @@ module ActiveRecord
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
end
- attr_reader :name, :cast_type, :null, :sql_type, :default_function
+ attr_reader :name, :cast_type, :null, :sql_type, :default, :default_function
delegate :type, :precision, :scale, :limit, :klass, :accessor,
:text?, :number?, :binary?, :changed?,
@@ -35,7 +35,7 @@ module ActiveRecord
@cast_type = cast_type
@sql_type = sql_type
@null = null
- @original_default = default
+ @default = default
@default_function = nil
end
@@ -51,13 +51,8 @@ module ActiveRecord
Base.human_attribute_name(@name)
end
- def default
- @default ||= type_cast_from_database(@original_default)
- end
-
def with_type(type)
dup.tap do |clone|
- clone.instance_variable_set('@default', nil)
clone.instance_variable_set('@cast_type', type)
end
end
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index b9558b0752..fad7eae461 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -230,14 +230,16 @@ module ActiveRecord
# Returns a hash where the keys are column names and the values are
# default values when instantiating the AR object for this table.
def column_defaults
- @column_defaults ||= Hash[columns.map { |c| [c.name, c.default] }]
+ @column_defaults ||= Hash[columns_hash.map { |name, column|
+ [name, column.type_cast_from_database(column.default)]
+ }]
end
# Returns a hash where the keys are the column names and the values
# are the default values suitable for use in `@raw_attriubtes`
def raw_column_defaults # :nodoc:
- @raw_column_defauts ||= Hash[column_defaults.map { |name, default|
- [name, columns_hash[name].type_cast_for_database(default)]
+ @raw_column_defaults ||= Hash[columns_hash.map { |name, column|
+ [name, column.default]
}]
end
@@ -285,7 +287,7 @@ module ActiveRecord
@arel_engine = nil
@column_defaults = nil
- @raw_column_defauts = nil
+ @raw_column_defaults = nil
@column_names = nil
@column_types = nil
@content_columns = nil