Class: ActiveModelSerializers::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/active_model_serializers/model.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Model) initialize(attributes = {})

Returns a new instance of Model



11
12
13
14
15
# File 'lib/active_model_serializers/model.rb', line 11

def initialize(attributes = {})
  @attributes = attributes
  @errors = ActiveModel::Errors.new(self)
  super
end

Instance Attribute Details

- (Object) attributes (readonly)

Returns the value of attribute attributes



9
10
11
# File 'lib/active_model_serializers/model.rb', line 9

def attributes
  @attributes
end

- (Object) errors (readonly)

Returns the value of attribute errors



9
10
11
# File 'lib/active_model_serializers/model.rb', line 9

def errors
  @errors
end

Class Method Details

+ (Object) human_attribute_name(attr, _options = {})

The following methods are needed to be minimally implemented for ActiveModel::Errors :nocov:



42
43
44
# File 'lib/active_model_serializers/model.rb', line 42

def self.human_attribute_name(attr, _options = {})
  attr
end

+ (Object) lookup_ancestors



46
47
48
# File 'lib/active_model_serializers/model.rb', line 46

def self.lookup_ancestors
  [self]
end

Instance Method Details

- (Object) cache_key

Defaults to the downcased model name and updated_at



23
24
25
# File 'lib/active_model_serializers/model.rb', line 23

def cache_key
  attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime("%Y%m%d%H%M%S%9N")}" }
end

- (Object) id

Defaults to the downcased model name.



18
19
20
# File 'lib/active_model_serializers/model.rb', line 18

def id
  attributes.fetch(:id) { self.class.name.downcase }
end

- (Object) read_attribute_for_serialization(key)



32
33
34
35
36
37
38
# File 'lib/active_model_serializers/model.rb', line 32

def read_attribute_for_serialization(key)
  if key == :id || key == 'id'
    attributes.fetch(key) { id }
  else
    attributes[key]
  end
end

- (Object) updated_at

Defaults to the time the serializer file was modified.



28
29
30
# File 'lib/active_model_serializers/model.rb', line 28

def updated_at
  attributes.fetch(:updated_at) { File.mtime(__FILE__) }
end