2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-12 23:09:31 -05:00
|
|
|
require "active_support/core_ext/object/deep_dup"
|
2017-10-19 12:45:07 -04:00
|
|
|
require "active_model/attribute_set/builder"
|
|
|
|
require "active_model/attribute_set/yaml_encoder"
|
2014-06-22 17:38:55 -04:00
|
|
|
|
2017-10-19 12:45:07 -04:00
|
|
|
module ActiveModel
|
2014-06-19 13:13:19 -04:00
|
|
|
class AttributeSet # :nodoc:
|
2017-11-27 16:06:51 -05:00
|
|
|
delegate :each_value, :fetch, :except, to: :attributes
|
2016-05-31 14:44:38 -04:00
|
|
|
|
2014-06-19 13:13:19 -04:00
|
|
|
def initialize(attributes)
|
|
|
|
@attributes = attributes
|
|
|
|
end
|
|
|
|
|
2014-06-22 18:57:40 -04:00
|
|
|
def [](name)
|
|
|
|
attributes[name] || Attribute.null(name)
|
|
|
|
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 []=(name, value)
|
|
|
|
attributes[name] = value
|
|
|
|
end
|
|
|
|
|
2014-06-21 12:52:19 -04:00
|
|
|
def values_before_type_cast
|
2014-06-29 17:31:35 -04:00
|
|
|
attributes.transform_values(&:value_before_type_cast)
|
2014-06-21 12:52:19 -04:00
|
|
|
end
|
|
|
|
|
2014-06-21 15:09:48 -04:00
|
|
|
def to_hash
|
2014-06-29 17:31:35 -04:00
|
|
|
initialized_attributes.transform_values(&:value)
|
2014-06-21 15:09:48 -04:00
|
|
|
end
|
|
|
|
alias_method :to_h, :to_hash
|
|
|
|
|
2014-07-11 09:50:16 -04:00
|
|
|
def key?(name)
|
|
|
|
attributes.key?(name) && self[name].initialized?
|
2014-06-22 17:38:55 -04:00
|
|
|
end
|
|
|
|
|
2014-11-14 13:20:28 -05:00
|
|
|
def keys
|
2015-01-31 19:28:04 -05:00
|
|
|
attributes.each_key.select { |name| self[name].initialized? }
|
2014-11-14 13:20:28 -05:00
|
|
|
end
|
|
|
|
|
2017-10-24 10:49:21 -04:00
|
|
|
def fetch_value(name, &block)
|
|
|
|
self[name].value(&block)
|
2014-06-22 17:38:55 -04:00
|
|
|
end
|
|
|
|
|
2014-06-22 18:25:40 -04:00
|
|
|
def write_from_database(name, value)
|
|
|
|
attributes[name] = self[name].with_value_from_database(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_from_user(name, value)
|
|
|
|
attributes[name] = self[name].with_value_from_user(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
|
|
|
def write_cast_value(name, value)
|
|
|
|
attributes[name] = self[name].with_cast_value(value)
|
|
|
|
end
|
|
|
|
|
2014-06-19 13:13:19 -04:00
|
|
|
def freeze
|
|
|
|
@attributes.freeze
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-09-28 16:17:08 -04:00
|
|
|
def deep_dup
|
2017-05-25 18:14:33 -04:00
|
|
|
self.class.allocate.tap do |copy|
|
|
|
|
copy.instance_variable_set(:@attributes, attributes.deep_dup)
|
|
|
|
end
|
2015-09-28 16:17:08 -04:00
|
|
|
end
|
|
|
|
|
2014-06-19 13:13:19 -04:00
|
|
|
def initialize_dup(_)
|
2015-09-28 16:17:08 -04:00
|
|
|
@attributes = attributes.dup
|
2014-06-19 13:13:19 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_clone(_)
|
|
|
|
@attributes = attributes.clone
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2014-07-02 09:00:26 -04:00
|
|
|
def reset(key)
|
2014-07-11 09:50:16 -04:00
|
|
|
if key?(key)
|
2014-07-02 09:00:26 -04:00
|
|
|
write_from_database(key, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-20 16:09:53 -05:00
|
|
|
def accessed
|
|
|
|
attributes.select { |_, attr| attr.has_been_read? }.keys
|
|
|
|
end
|
|
|
|
|
2015-09-24 13:50:11 -04:00
|
|
|
def map(&block)
|
|
|
|
new_attributes = attributes.transform_values(&block)
|
|
|
|
AttributeSet.new(new_attributes)
|
|
|
|
end
|
|
|
|
|
2015-10-06 10:51:14 -04:00
|
|
|
def ==(other)
|
|
|
|
attributes == other.attributes
|
|
|
|
end
|
|
|
|
|
2014-06-19 13:13:19 -04:00
|
|
|
protected
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
attr_reader :attributes
|
2014-06-22 17:38:55 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def initialized_attributes
|
|
|
|
attributes.select { |_, attr| attr.initialized? }
|
|
|
|
end
|
2014-06-19 13:13:19 -04:00
|
|
|
end
|
|
|
|
end
|