Module: ActiveModelSerializers::Model::DeriveAttributesFromNamesAndFixAccessors

Defined in:
lib/active_model_serializers/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


55
56
57
58
# File 'lib/active_model_serializers/model.rb', line 55

def self.included(base)
  # NOTE that +id+ will always be in +attributes+.
  base.attributes :id
end

Instance Method Details

#attributesObject

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.


65
66
67
68
69
# File 'lib/active_model_serializers/model.rb', line 65

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