Module: HasHelpers::Serializer

Defined in:
lib/has_helpers/serializer.rb

Overview

typed: true frozen_string_literal: true

Class Method Summary collapse

Class Method Details

.serialize(record) ⇒ Object

Serializes ActiveRecord objects, denormalizing constants.

Example

::HQ::Serializer.serialize(person) => { first_name: "Joe" last_name: "Smith" status: "Active" # constant association }



14
15
16
# File 'lib/has_helpers/serializer.rb', line 14

def self.serialize(record)
  record.attributes.reject { |key, _value| constant_foreign_keys(record.class).include?(key) }.merge(constant_attributes(record))
end

.serialize_array(array) ⇒ Object



18
19
20
# File 'lib/has_helpers/serializer.rb', line 18

def self.serialize_array(array)
  array.map { |record| serialize(record) }
end