2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-10 00:25:16 -05:00
|
|
|
require "active_support/core_ext/object/duplicable"
|
|
|
|
|
2017-10-19 12:45:07 -04:00
|
|
|
module ActiveModel
|
2014-06-07 15:46:22 -04:00
|
|
|
class Attribute # :nodoc:
|
|
|
|
class << self
|
2020-06-13 01:36:22 -04:00
|
|
|
def from_database(name, value_before_type_cast, type, value = nil)
|
|
|
|
FromDatabase.new(name, value_before_type_cast, type, nil, value)
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 01:36:22 -04:00
|
|
|
def from_user(name, value_before_type_cast, type, original_attribute = nil)
|
|
|
|
FromUser.new(name, value_before_type_cast, type, original_attribute)
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
2014-06-22 17:38:55 -04:00
|
|
|
|
2020-06-13 01:36:22 -04:00
|
|
|
def with_cast_value(name, value_before_type_cast, type)
|
|
|
|
WithCastValue.new(name, value_before_type_cast, type)
|
`update_column` take ruby-land input, not database-land input
In the case of serialized columns, we would expect the unserialized
value as input, not the serialized value. The original issue which made
this distinction, #14163, introduced a bug. If you passed serialized
input to the method, it would double serialize when it was sent to the
database. You would see the wrong input upon reloading, or get an error
if you had a specific type on the serialized column.
To put it another way, `update_column` is a special case of
`update_all`, which would take `['a']` and not `['a'].to_yaml`, but you
would not pass data from `params` to it.
Fixes #18037
2014-12-16 17:19:47 -05:00
|
|
|
end
|
|
|
|
|
2014-06-22 18:57:40 -04:00
|
|
|
def null(name)
|
|
|
|
Null.new(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninitialized(name, type)
|
|
|
|
Uninitialized.new(name, type)
|
2014-06-22 17:38:55 -04:00
|
|
|
end
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
2014-06-22 18:57:40 -04:00
|
|
|
attr_reader :name, :value_before_type_cast, :type
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
# This method should not be called directly.
|
|
|
|
# Use #from_database or #from_user
|
2020-06-13 01:36:22 -04:00
|
|
|
def initialize(name, value_before_type_cast, type, original_attribute = nil, value = nil)
|
2014-06-22 18:57:40 -04:00
|
|
|
@name = name
|
2014-06-07 15:46:22 -04:00
|
|
|
@value_before_type_cast = value_before_type_cast
|
|
|
|
@type = type
|
2015-09-28 17:12:28 -04:00
|
|
|
@original_attribute = original_attribute
|
2020-06-13 01:36:22 -04:00
|
|
|
@value = value unless value.nil?
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def value
|
|
|
|
# `defined?` is cheaper than `||=` when we get back falsy values
|
2015-09-28 16:10:23 -04:00
|
|
|
@value = type_cast(value_before_type_cast) unless defined?(@value)
|
2014-06-07 15:46:22 -04:00
|
|
|
@value
|
|
|
|
end
|
|
|
|
|
2015-09-28 17:12:28 -04:00
|
|
|
def original_value
|
|
|
|
if assigned?
|
|
|
|
original_attribute.original_value
|
|
|
|
else
|
|
|
|
type_cast(value_before_type_cast)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-07 15:46:22 -04:00
|
|
|
def value_for_database
|
2015-02-17 15:35:23 -05:00
|
|
|
type.serialize(value)
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
2015-09-28 17:12:28 -04:00
|
|
|
def changed?
|
|
|
|
changed_from_assignment? || changed_in_place?
|
|
|
|
end
|
|
|
|
|
|
|
|
def changed_in_place?
|
|
|
|
has_been_read? && type.changed_in_place?(original_value_for_database, value)
|
2014-06-16 13:20:55 -04:00
|
|
|
end
|
|
|
|
|
2015-09-28 17:12:28 -04:00
|
|
|
def forgetting_assignment
|
|
|
|
with_value_from_database(value_for_database)
|
2014-06-16 13:20:55 -04:00
|
|
|
end
|
|
|
|
|
2014-06-22 18:25:40 -04:00
|
|
|
def with_value_from_user(value)
|
2015-09-24 13:50:11 -04:00
|
|
|
type.assert_valid_value(value)
|
2016-08-14 17:39:04 -04:00
|
|
|
self.class.from_user(name, value, type, original_attribute || self)
|
2014-06-22 18:25:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_value_from_database(value)
|
2014-06-22 18:57:40 -04:00
|
|
|
self.class.from_database(name, value, type)
|
2014-06-22 18:25:40 -04:00
|
|
|
end
|
|
|
|
|
`update_column` take ruby-land input, not database-land input
In the case of serialized columns, we would expect the unserialized
value as input, not the serialized value. The original issue which made
this distinction, #14163, introduced a bug. If you passed serialized
input to the method, it would double serialize when it was sent to the
database. You would see the wrong input upon reloading, or get an error
if you had a specific type on the serialized column.
To put it another way, `update_column` is a special case of
`update_all`, which would take `['a']` and not `['a'].to_yaml`, but you
would not pass data from `params` to it.
Fixes #18037
2014-12-16 17:19:47 -05:00
|
|
|
def with_cast_value(value)
|
|
|
|
self.class.with_cast_value(name, value, type)
|
|
|
|
end
|
|
|
|
|
Attribute assignment and type casting has nothing to do with columns
It's finally finished!!!!!!! The reason the Attributes API was kept
private in 4.2 was due to some publicly visible implementation details.
It was previously implemented by overloading `columns` and
`columns_hash`, to make them return column objects which were modified
with the attribute information.
This meant that those methods LIED! We didn't change the database
schema. We changed the attribute information on the class. That is
wrong! It should be the other way around, where schema loading just
calls the attributes API for you. And now it does!
Yes, this means that there is nothing that happens in automatic schema
loading that you couldn't manually do yourself. (There's still some
funky cases where we hit the connection adapter that I need to handle,
before we can turn off automatic schema detection entirely.)
There were a few weird test failures caused by this that had to be
fixed. The main source came from the fact that the attribute methods are
now defined in terms of `attribute_names`, which has a clause like
`return [] unless table_exists?`. I don't *think* this is an issue,
since the only place this caused failures were in a fake adapter which
didn't override `table_exists?`.
Additionally, there were a few cases where tests were failing because a
migration was run, but the model was not reloaded. I'm not sure why
these started failing from this change, I might need to clear an
additional cache in `reload_schema_from_cache`. Again, since this is not
normal usage, and it's expected that `reset_column_information` will be
called after the table is modified, I don't think it's a problem.
Still, test failures that were unrelated to the change are worrying, and
I need to dig into them further.
Finally, I spent a lot of time debugging issues with the mutex used in
`define_attribute_methods`. I think we can just remove that method
entirely, and define the attribute methods *manually* in the call to
`define_attribute`, which would simplify the code *tremendously*.
Ok. now to make this damn thing public, and work on moving it up to
Active Model.
2015-01-30 16:03:36 -05:00
|
|
|
def with_type(type)
|
2016-06-02 11:06:01 -04:00
|
|
|
if changed_in_place?
|
|
|
|
with_value_from_user(value).with_type(type)
|
|
|
|
else
|
|
|
|
self.class.new(name, value_before_type_cast, type, original_attribute)
|
|
|
|
end
|
Attribute assignment and type casting has nothing to do with columns
It's finally finished!!!!!!! The reason the Attributes API was kept
private in 4.2 was due to some publicly visible implementation details.
It was previously implemented by overloading `columns` and
`columns_hash`, to make them return column objects which were modified
with the attribute information.
This meant that those methods LIED! We didn't change the database
schema. We changed the attribute information on the class. That is
wrong! It should be the other way around, where schema loading just
calls the attributes API for you. And now it does!
Yes, this means that there is nothing that happens in automatic schema
loading that you couldn't manually do yourself. (There's still some
funky cases where we hit the connection adapter that I need to handle,
before we can turn off automatic schema detection entirely.)
There were a few weird test failures caused by this that had to be
fixed. The main source came from the fact that the attribute methods are
now defined in terms of `attribute_names`, which has a clause like
`return [] unless table_exists?`. I don't *think* this is an issue,
since the only place this caused failures were in a fake adapter which
didn't override `table_exists?`.
Additionally, there were a few cases where tests were failing because a
migration was run, but the model was not reloaded. I'm not sure why
these started failing from this change, I might need to clear an
additional cache in `reload_schema_from_cache`. Again, since this is not
normal usage, and it's expected that `reset_column_information` will be
called after the table is modified, I don't think it's a problem.
Still, test failures that were unrelated to the change are worrying, and
I need to dig into them further.
Finally, I spent a lot of time debugging issues with the mutex used in
`define_attribute_methods`. I think we can just remove that method
entirely, and define the attribute methods *manually* in the call to
`define_attribute`, which would simplify the code *tremendously*.
Ok. now to make this damn thing public, and work on moving it up to
Active Model.
2015-01-30 16:03:36 -05:00
|
|
|
end
|
|
|
|
|
2014-07-12 20:30:49 -04:00
|
|
|
def type_cast(*)
|
2014-06-07 15:46:22 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2014-06-22 17:38:55 -04:00
|
|
|
def initialized?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-01-14 19:08:25 -05:00
|
|
|
def came_from_user?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-01-20 16:09:53 -05:00
|
|
|
def has_been_read?
|
|
|
|
defined?(@value)
|
|
|
|
end
|
|
|
|
|
2014-08-15 15:37:53 -04:00
|
|
|
def ==(other)
|
|
|
|
self.class == other.class &&
|
|
|
|
name == other.name &&
|
|
|
|
value_before_type_cast == other.value_before_type_cast &&
|
|
|
|
type == other.type
|
|
|
|
end
|
2015-01-27 15:42:02 -05:00
|
|
|
alias eql? ==
|
|
|
|
|
|
|
|
def hash
|
|
|
|
[self.class, name, value_before_type_cast, type].hash
|
|
|
|
end
|
2014-08-15 15:37:53 -04:00
|
|
|
|
2016-05-31 14:44:38 -04:00
|
|
|
def init_with(coder)
|
|
|
|
@name = coder["name"]
|
|
|
|
@value_before_type_cast = coder["value_before_type_cast"]
|
|
|
|
@type = coder["type"]
|
|
|
|
@original_attribute = coder["original_attribute"]
|
|
|
|
@value = coder["value"] if coder.map.key?("value")
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_with(coder)
|
|
|
|
coder["name"] = name
|
2017-06-22 10:35:40 -04:00
|
|
|
coder["value_before_type_cast"] = value_before_type_cast unless value_before_type_cast.nil?
|
2016-05-31 14:44:38 -04:00
|
|
|
coder["type"] = type if type
|
|
|
|
coder["original_attribute"] = original_attribute if original_attribute
|
|
|
|
coder["value"] = value if defined?(@value)
|
|
|
|
end
|
|
|
|
|
2020-05-17 20:35:43 -04:00
|
|
|
def original_value_for_database
|
|
|
|
if assigned?
|
|
|
|
original_attribute.original_value_for_database
|
|
|
|
else
|
|
|
|
_original_value_for_database
|
2016-12-23 01:51:11 -05:00
|
|
|
end
|
2020-05-17 20:35:43 -04:00
|
|
|
end
|
2016-12-23 01:51:11 -05:00
|
|
|
|
|
|
|
private
|
2018-02-25 16:14:17 -05:00
|
|
|
attr_reader :original_attribute
|
|
|
|
alias :assigned? :original_attribute
|
|
|
|
|
2016-12-24 12:46:12 -05:00
|
|
|
def initialize_dup(other)
|
2016-08-06 13:55:02 -04:00
|
|
|
if defined?(@value) && @value.duplicable?
|
|
|
|
@value = @value.dup
|
|
|
|
end
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
2015-09-28 17:12:28 -04:00
|
|
|
|
2016-12-24 12:46:12 -05:00
|
|
|
def changed_from_assignment?
|
2016-08-06 13:55:02 -04:00
|
|
|
assigned? && type.changed?(original_value, value, value_before_type_cast)
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
|
|
|
|
2016-12-24 12:46:12 -05:00
|
|
|
def _original_value_for_database
|
2016-08-06 13:55:02 -04:00
|
|
|
type.serialize(original_value)
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class FromDatabase < Attribute # :nodoc:
|
|
|
|
def type_cast(value)
|
|
|
|
type.deserialize(value)
|
|
|
|
end
|
2015-01-14 19:08:25 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def _original_value_for_database
|
|
|
|
value_before_type_cast
|
|
|
|
end
|
2020-05-17 20:35:43 -04:00
|
|
|
private :_original_value_for_database
|
2015-01-14 19:08:25 -05:00
|
|
|
end
|
2014-06-16 13:20:55 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class FromUser < Attribute # :nodoc:
|
|
|
|
def type_cast(value)
|
|
|
|
type.cast(value)
|
|
|
|
end
|
`update_column` take ruby-land input, not database-land input
In the case of serialized columns, we would expect the unserialized
value as input, not the serialized value. The original issue which made
this distinction, #14163, introduced a bug. If you passed serialized
input to the method, it would double serialize when it was sent to the
database. You would see the wrong input upon reloading, or get an error
if you had a specific type on the serialized column.
To put it another way, `update_column` is a special case of
`update_all`, which would take `['a']` and not `['a'].to_yaml`, but you
would not pass data from `params` to it.
Fixes #18037
2014-12-16 17:19:47 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def came_from_user?
|
2017-07-17 11:14:11 -04:00
|
|
|
!type.value_constructed_by_mass_assignment?(value_before_type_cast)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
`update_column` take ruby-land input, not database-land input
In the case of serialized columns, we would expect the unserialized
value as input, not the serialized value. The original issue which made
this distinction, #14163, introduced a bug. If you passed serialized
input to the method, it would double serialize when it was sent to the
database. You would see the wrong input upon reloading, or get an error
if you had a specific type on the serialized column.
To put it another way, `update_column` is a special case of
`update_all`, which would take `['a']` and not `['a'].to_yaml`, but you
would not pass data from `params` to it.
Fixes #18037
2014-12-16 17:19:47 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class WithCastValue < Attribute # :nodoc:
|
|
|
|
def type_cast(value)
|
|
|
|
value
|
|
|
|
end
|
2014-06-22 17:38:55 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def changed_in_place?
|
|
|
|
false
|
|
|
|
end
|
2014-06-22 17:38:55 -04:00
|
|
|
end
|
2014-06-26 11:36:40 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class Null < Attribute # :nodoc:
|
|
|
|
def initialize(name)
|
2016-08-25 13:07:44 -04:00
|
|
|
super(name, nil, Type.default_value)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
Attribute assignment and type casting has nothing to do with columns
It's finally finished!!!!!!! The reason the Attributes API was kept
private in 4.2 was due to some publicly visible implementation details.
It was previously implemented by overloading `columns` and
`columns_hash`, to make them return column objects which were modified
with the attribute information.
This meant that those methods LIED! We didn't change the database
schema. We changed the attribute information on the class. That is
wrong! It should be the other way around, where schema loading just
calls the attributes API for you. And now it does!
Yes, this means that there is nothing that happens in automatic schema
loading that you couldn't manually do yourself. (There's still some
funky cases where we hit the connection adapter that I need to handle,
before we can turn off automatic schema detection entirely.)
There were a few weird test failures caused by this that had to be
fixed. The main source came from the fact that the attribute methods are
now defined in terms of `attribute_names`, which has a clause like
`return [] unless table_exists?`. I don't *think* this is an issue,
since the only place this caused failures were in a fake adapter which
didn't override `table_exists?`.
Additionally, there were a few cases where tests were failing because a
migration was run, but the model was not reloaded. I'm not sure why
these started failing from this change, I might need to clear an
additional cache in `reload_schema_from_cache`. Again, since this is not
normal usage, and it's expected that `reset_column_information` will be
called after the table is modified, I don't think it's a problem.
Still, test failures that were unrelated to the change are worrying, and
I need to dig into them further.
Finally, I spent a lot of time debugging issues with the mutex used in
`define_attribute_methods`. I think we can just remove that method
entirely, and define the attribute methods *manually* in the call to
`define_attribute`, which would simplify the code *tremendously*.
Ok. now to make this damn thing public, and work on moving it up to
Active Model.
2015-01-30 16:03:36 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def type_cast(*)
|
|
|
|
nil
|
|
|
|
end
|
2014-06-22 17:38:55 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def with_type(type)
|
|
|
|
self.class.with_cast_value(name, nil, type)
|
|
|
|
end
|
2016-06-02 11:32:05 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def with_value_from_database(value)
|
|
|
|
raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{name}`"
|
|
|
|
end
|
|
|
|
alias_method :with_value_from_user, :with_value_from_database
|
2018-10-30 14:52:23 -04:00
|
|
|
alias_method :with_cast_value, :with_value_from_database
|
2014-06-22 17:38:55 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class Uninitialized < Attribute # :nodoc:
|
|
|
|
UNINITIALIZED_ORIGINAL_VALUE = Object.new
|
|
|
|
|
|
|
|
def initialize(name, type)
|
|
|
|
super(name, nil, type)
|
2014-06-22 18:57:40 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def value
|
|
|
|
if block_given?
|
|
|
|
yield name
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:32:05 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def original_value
|
|
|
|
UNINITIALIZED_ORIGINAL_VALUE
|
|
|
|
end
|
2014-06-22 17:38:55 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def value_for_database
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialized?
|
|
|
|
false
|
|
|
|
end
|
2016-05-31 14:44:38 -04:00
|
|
|
|
2017-05-09 06:04:22 -04:00
|
|
|
def forgetting_assignment
|
|
|
|
dup
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def with_type(type)
|
|
|
|
self.class.new(name, type)
|
|
|
|
end
|
2016-05-31 14:44:38 -04:00
|
|
|
end
|
2017-10-19 12:45:07 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
private_constant :FromDatabase, :FromUser, :Null, :Uninitialized, :WithCastValue
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
end
|