Class: Cuprum::Rails::Serializers::Json::ActiveRecordSerializer

Inherits:
BaseSerializer
  • Object
show all
Defined in:
lib/cuprum/rails/serializers/json/active_record_serializer.rb

Overview

Converts ActiveRecord record to JSON using the #as_json method.

Instance Method Summary collapse

Methods inherited from BaseSerializer

instance

Instance Method Details

#call(record, **_) ⇒ Hash

Converts the ActiveRecord record to JSON.

Calls and returns the #as_json method of the record.

Parameters:

  • record (ActiveRecord::Base)

    The record to convert to JSON.

Returns:

  • (Hash)

    a JSON-compatible representation of the record.



16
17
18
19
20
21
22
# File 'lib/cuprum/rails/serializers/json/active_record_serializer.rb', line 16

def call(record, **_)
  unless record.is_a?(ActiveRecord::Base)
    raise ArgumentError, 'object must be an ActiveRecord record'
  end

  record.as_json
end