class ActiveModelSerializers::Model
Attributes
errors[R]
updated_at[W]
NOTE that updated_at
isn't included in attribute_names
, which means it won't show up in attributes
unless a subclass has either attributes :updated_at
which will redefine the methods or attribute_names << :updated_at
.
Public Class Methods
attributes(*names)
click to toggle source
# File lib/active_model_serializers/model.rb, line 14 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
human_attribute_name(attr, _options = {})
click to toggle source
The following methods are needed to be minimally implemented for ActiveModel::Errors :nocov:
# File lib/active_model_serializers/model.rb, line 62 def self.human_attribute_name(attr, _options = {}) attr end
lookup_ancestors()
click to toggle source
# File lib/active_model_serializers/model.rb, line 66 def self.lookup_ancestors [self] end
new(attributes = {})
click to toggle source
Calls superclass method
# File lib/active_model_serializers/model.rb, line 31 def initialize(attributes = {}) @errors = ActiveModel::Errors.new(self) super end
Public Instance Methods
attributes()
click to toggle source
The the fields in attribute_names
determines the returned hash. attributes
are returned frozen to prevent any expectations that mutation affects the actual values in the model.
# File lib/active_model_serializers/model.rb, line 39 def attributes attribute_names.each_with_object({}) do |attribute_name, result| result[attribute_name] = public_send(attribute_name).freeze end.with_indifferent_access.freeze end
cache_key()
click to toggle source
To customize model behavior, this method must be redefined. However, there are other ways of setting the cache_key
a serializer uses.
# File lib/active_model_serializers/model.rb, line 47 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
updated_at()
click to toggle source
When no set, defaults to the time the file was modified. See NOTE by attr_writer :updated_at
# File lib/active_model_serializers/model.rb, line 56 def updated_at defined?(@updated_at) ? @updated_at : File.mtime(__FILE__) end