Class: ActiveModelSerializers::Model
- Inherits:
-
Object
- Object
- ActiveModelSerializers::Model
- Includes:
- ActiveModel::Model, ActiveModel::Serializers::JSON
- Defined in:
- lib/active_model_serializers/model.rb
Defined Under Namespace
Modules: DeriveAttributesFromNamesAndFixAccessors
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The only way to change the attributes of an instance is to directly mutate the attributes.
-
#errors ⇒ ActiveModel::Errors
readonly
Support for validation and other ActiveModel::Errors.
-
#updated_at ⇒ String, ...
When not set, defaults to the time the file was modified.
Class Method Summary collapse
-
.attributes(names) ⇒ Object
Easily declare instance attributes with setters and getters for each.
-
.derive_attributes_from_names_and_fix_accessors ⇒ Object
Opt-in to breaking change.
Instance Method Summary collapse
-
#attribute_names ⇒ Array<Symbol>
Declare names of attributes to be included in
attributes
hash. -
#cache_key ⇒ String
To customize model behavior, this method must be redefined.
-
#id ⇒ String, ...
Defaults to the downcased model name.
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
89 90 91 92 93 94 |
# 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 |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
The only way to change the attributes of an instance is to directly mutate the attributes.
86 87 88 |
# File 'lib/active_model_serializers/model.rb', line 86 def attributes @attributes end |
#errors ⇒ ActiveModel::Errors (readonly)
Support for validation and other ActiveModel::Errors
76 77 78 |
# File 'lib/active_model_serializers/model.rb', line 76 def errors @errors end |
#updated_at ⇒ String, ...
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 attributes[:updated_at] = Time.current
.
When not set, defaults to the time the file was modified.
116 117 118 119 120 |
# 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 |
Class Method Details
.attributes(names) ⇒ Object
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.
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).
41 42 43 44 45 46 47 |
# 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 |
.derive_attributes_from_names_and_fix_accessors ⇒ Object
Opt-in to breaking change
50 51 52 53 54 |
# 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 |
Instance Method Details
#attribute_names ⇒ Array<Symbol>
Declare names of attributes to be included in attributes
hash. Is only available as a class-method since the ActiveModel::Serialization mixin in Rails uses an attribute_names
local variable, which may conflict if we were to add instance methods here.
18 |
# File 'lib/active_model_serializers/model.rb', line 18 class_attribute :attribute_names, instance_writer: false, instance_reader: false |
#cache_key ⇒ String
To customize model behavior, this method must be redefined. However, there are other ways of setting the cache_key
a serializer uses.
125 126 127 128 129 130 |
# File 'lib/active_model_serializers/model.rb', line 125 def cache_key ActiveSupport::Cache.([ self.class.model_name.name.downcase, "#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}" ].compact) end |
#id ⇒ String, ...
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 attributes[:id] = 5
.
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 _(‘-’)_/.
104 105 106 107 108 |
# 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 |