module ActiveModelSerializers::Model::DeriveAttributesFromNamesAndFixAccessors

Public Class Methods

included(base) click to toggle source
# File lib/active_model_serializers/model.rb, line 57
def self.included(base)
  # NOTE that +id+ will always be in +attributes+.
  base.attributes :id
end

Public Instance Methods

attributes() click to toggle source

Override the attributes method so that the hash is derived from attribute_names.

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 67
def attributes
  self.class.attribute_names.each_with_object({}) do |attribute_name, result|
    result[attribute_name] = public_send(attribute_name).freeze
  end.with_indifferent_access.freeze
end