aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-02-06 11:05:38 -0700
committerSean Griffin <sean@thoughtbot.com>2015-02-06 11:51:13 -0700
commit101c19f55f5f1d86d35574b805278f11e9a1a48e (patch)
treecf5821f06acafe416a0cea205967f243f08ec9ef /activerecord/lib/active_record
parentd7318599160dbc6d362793673e8c6db926eeb7b8 (diff)
downloadrails-101c19f55f5f1d86d35574b805278f11e9a1a48e.tar.gz
rails-101c19f55f5f1d86d35574b805278f11e9a1a48e.tar.bz2
rails-101c19f55f5f1d86d35574b805278f11e9a1a48e.zip
Allow a symbol to be passed to `attribute`, in place of a type object
The same is not true of `define_attribute`, which is meant to be the low level no-magic API that sits underneath. The differences between the two APIs are: - `attribute` - Lazy (the attribute will be defined after the schema has loaded) - Allows either a type object or a symbol - `define_attribute` - Runs immediately (might get trampled by schema loading) - Requires a type object This was the last blocker in terms of public interface requirements originally discussed for this feature back in May. All the implementation blockers have been cleared, so this feature is probably ready for release (pending one more look-over by me).
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/attributes.rb13
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/quoting.rb21
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb4
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb8
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb41
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb4
7 files changed, 93 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index 7cb6b075a0..f34e6cf912 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -44,7 +44,7 @@ module ActiveRecord
# store_listing.price_in_cents # => BigDecimal.new(10.1)
#
# class StoreListing < ActiveRecord::Base
- # attribute :price_in_cents, Type::Integer.new
+ # attribute :price_in_cents, :integer
# end
#
# # after
@@ -77,7 +77,10 @@ module ActiveRecord
name = name.to_s
reload_schema_from_cache
- self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(name => [cast_type, options])
+ self.attributes_to_define_after_schema_loads =
+ attributes_to_define_after_schema_loads.merge(
+ name => [cast_type, options]
+ )
end
def define_attribute(
@@ -93,7 +96,11 @@ module ActiveRecord
def load_schema!
super
attributes_to_define_after_schema_loads.each do |name, (type, options)|
- define_attribute(name, type, **options)
+ if type.is_a?(Symbol)
+ type = connection.type_for_attribute_options(type, **options.except(:default))
+ end
+
+ define_attribute(name, type, **options.slice(:default))
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
index 2c013a074a..55d3360070 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
@@ -134,8 +134,29 @@ module ActiveRecord
binds.map(&:value_for_database)
end
+ def type_for_attribute_options(type_name, **options)
+ klass = type_classes_with_standard_constructor.fetch(type_name, Type::Value)
+ klass.new(**options)
+ end
+
private
+ def type_classes_with_standard_constructor
+ {
+ big_integer: Type::BigInteger,
+ binary: Type::Binary,
+ boolean: Type::Boolean,
+ date: Type::Date,
+ date_time: Type::DateTime,
+ decimal: Type::Decimal,
+ float: Type::Float,
+ integer: Type::Integer,
+ string: Type::String,
+ text: Type::Text,
+ time: Type::Time,
+ }
+ end
+
def types_which_need_no_typecasting
[nil, Numeric, String]
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 5c8c4b883a..61bac6741f 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -947,6 +947,10 @@ module ActiveRecord
end
end
end
+
+ def type_classes_with_standard_constructor
+ super.merge(string: MysqlString, date_time: MysqlDateTime)
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
index e45a2f59d9..0a0a7fdbb3 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
@@ -48,6 +48,12 @@ module ActiveRecord
end
end
+ def ==(other)
+ other.is_a?(Array) &&
+ subtype == other.subtype &&
+ delimiter == other.delimiter
+ end
+
private
def type_cast_array(value, method)
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
index 3adfb8b9d8..2a5a59fbc6 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
@@ -7,7 +7,7 @@ module ActiveRecord
class Range < Type::Value # :nodoc:
attr_reader :subtype, :type
- def initialize(subtype, type)
+ def initialize(subtype, type = :range)
@subtype = subtype
@type = type
end
@@ -40,6 +40,12 @@ module ActiveRecord
end
end
+ def ==(other)
+ other.is_a?(Range) &&
+ other.subtype == subtype &&
+ other.type == type
+ end
+
private
def type_cast_single(value)
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
index 464adb4e23..11114f32fe 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -65,6 +65,23 @@ module ActiveRecord
type_map.lookup(column.oid, column.fmod, column.sql_type)
end
+ def type_for_attribute_options(
+ type_name,
+ array: false,
+ range: false,
+ **options
+ )
+ if array
+ subtype = type_for_attribute_options(type_name, **options)
+ OID::Array.new(subtype)
+ elsif range
+ subtype = type_for_attribute_options(type_name, **options)
+ OID::Range.new(subtype)
+ else
+ super(type_name, **options)
+ end
+ end
+
private
def _quote(value)
@@ -103,6 +120,30 @@ module ActiveRecord
super
end
end
+
+ def type_classes_with_standard_constructor
+ super.merge(
+ bit: OID::Bit,
+ bit_varying: OID::BitVarying,
+ binary: OID::Bytea,
+ cidr: OID::Cidr,
+ date: OID::Date,
+ date_time: OID::DateTime,
+ decimal: OID::Decimal,
+ enum: OID::Enum,
+ float: OID::Float,
+ hstore: OID::Hstore,
+ inet: OID::Inet,
+ json: OID::Json,
+ jsonb: OID::Jsonb,
+ money: OID::Money,
+ point: OID::Point,
+ time: OID::Time,
+ uuid: OID::Uuid,
+ vector: OID::Vector,
+ xml: OID::Xml,
+ )
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index c06213a7bf..edd060248f 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -240,6 +240,10 @@ module ActiveRecord
end
end
+ def type_classes_with_standard_constructor
+ super.merge(binary: SQLite3Binary)
+ end
+
def quote_string(s) #:nodoc:
@connection.class.quote(s)
end