module ActiveModel::Serializer::Attributes::ClassMethods

Public Instance Methods

_attributes() click to toggle source

@api private keys of attributes @see Serializer::attribute

# File lib/active_model/serializer/concerns/attributes.rb, line 64
def _attributes
  _attributes_data.keys
end
_attributes_keys() click to toggle source

@api private maps attribute value to explicit key name @see Serializer::attribute @see FragmentCache#fragment_serializer

# File lib/active_model/serializer/concerns/attributes.rb, line 72
def _attributes_keys
  _attributes_data
    .each_with_object({}) do |(key, attr), hash|
      next if key == attr.name
      hash[attr.name] = { key: key }
    end
end
attribute(attr, options = {}, &block) click to toggle source

@example

class AdminAuthorSerializer < ActiveModel::Serializer
  attributes :id, :recent_edits
  attribute :name, key: :title

  attribute :full_name do
    "#{object.first_name} #{object.last_name}"
  end

  def recent_edits
    object.edits.last(5)
  end
# File lib/active_model/serializer/concerns/attributes.rb, line 56
def attribute(attr, options = {}, &block)
  key = options.fetch(:key, attr)
  _attributes_data[key] = Attribute.new(attr, options, block)
end
attributes(*attrs) click to toggle source

@example

class AdminAuthorSerializer < ActiveModel::Serializer
  attributes :id, :name, :recent_edits
# File lib/active_model/serializer/concerns/attributes.rb, line 36
def attributes(*attrs)
  attrs = attrs.first if attrs.first.class == Array

  attrs.each do |attr|
    attribute(attr)
  end
end
inherited(base) click to toggle source
Calls superclass method
# File lib/active_model/serializer/concerns/attributes.rb, line 28
def inherited(base)
  super
  base._attributes_data = _attributes_data.dup
end