class ActiveModelSerializers::Model
Attributes
The only way to change the attributes of an instance is to directly mutate the attributes. @example
model.attributes[:foo] = :bar
@return [Hash]
Support for validation and other ActiveModel::Errors @return [ActiveModel::Errors]
(see updated_at
)
Public Class Methods
Easily declare instance attributes with setters and getters for each.
To initialize an instance, all attributes must have setters. However, the hash returned by attributes
instance method will ALWAYS be the value of the initial attributes, regardless of what accessors are defined. The only way to change the change the attributes after initialization is to mutate the attributes
directly. Accessor methods do NOT mutate the attributes. (This is a bug).
@note For now, the Model
only supports the notion of 'attributes'.
In the tests, there is a special Model that also supports 'associations'. This is important so that we can add accessors for values that should not appear in the attributes hash when modeling associations. It is not yet clear if it makes sense for a PORO to have associations outside of the tests.
@overload attributes(names)
@param names [Array<String, Symbol>] @param name [String, Symbol]
# File lib/active_model_serializers/model.rb, line 41 def self.attributes(*names) self.attribute_names |= names.map(&:to_sym) # Silence redefinition of methods warnings ActiveModelSerializers.silence_warnings do attr_accessor(*names) end end
Opt-in to breaking change
# File lib/active_model_serializers/model.rb, line 50 def self.derive_attributes_from_names_and_fix_accessors unless included_modules.include?(DeriveAttributesFromNamesAndFixAccessors) prepend(DeriveAttributesFromNamesAndFixAccessors) end end
@param attributes [Hash]
# File lib/active_model_serializers/model.rb, line 89 def initialize(attributes = {}) attributes ||= {} # protect against nil @attributes = attributes.symbolize_keys.with_indifferent_access @errors = ActiveModel::Errors.new(self) super end
Public Instance Methods
To customize model behavior, this method must be redefined. However, there are other ways of setting the cache_key
a serializer uses. @return [String]
# File lib/active_model_serializers/model.rb, line 125 def cache_key ActiveSupport::Cache.expand_cache_key([ self.class.model_name.name.downcase, "#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}" ].compact) end
Defaults to the downcased model name. This probably isn't a good default, since it's not a unique instance identifier, but that's what is currently implemented _('-')_/.
@note Though id
is defined, it will only show up
in +attributes+ when it is passed in to the initializer or added to +attributes+, such as <tt>attributes[:id] = 5</tt>.
@return [String, Numeric, Symbol]
# File lib/active_model_serializers/model.rb, line 104 def id attributes.fetch(:id) do defined?(@id) ? @id : self.class.model_name.name && self.class.model_name.name.downcase end end
When not set, defaults to the time the file was modified.
@note Though updated_at
and updated_at=
are defined, it will only show up
in +attributes+ when it is passed in to the initializer or added to +attributes+, such as <tt>attributes[:updated_at] = Time.current</tt>.
@return [String, Numeric, Time]
# File lib/active_model_serializers/model.rb, line 116 def updated_at attributes.fetch(:updated_at) do defined?(@updated_at) ? @updated_at : File.mtime(__FILE__) end end