Danny Sullivan
Danny Sullivan

Reputation: 277

Active Model Serializer - Customizing Response

I am trying to customize a serializer for the following:

# in DB
{
  "id": 1,
  "measurement": 10.5,
  "sequence": 1,
  "time_stamp": "2016-05-25T04:53:38.000Z",
  "device_id": 2,
  "patient_id": 3,
  "measurement_type": "ecg"
},
{
  "id": 2,
  "measurement": 20.5,
  "sequence": 2,
  "time_stamp": "2016-05-25T04:53:38.000Z",
  "device_id": 2,
  "patient_id": 3,
  "measurement_type": "ecg"
}
{
  "id": 3,
  "measurement": 30.5,
  "sequence": 3,
  "time_stamp": "2016-05-25T04:53:38.000Z",
  "device_id": 2,
  "patient_id": 3,
  "measurement_type": "ecg"
}

# need to return this structure via GET
{
  "max_timestamp": "2020-05-25T04:53:38.000Z",
  "measurement_type": "ecg",
  "measurements": [
    { measurement: 10.5, sequence: 1, time_stamp: "2020-05-25T04:53:38.000Z" },
    { measurement: 20.5, sequence: 2, time_stamp: "2016-05-25T04:53:38.000Z" },
    { measurement: 30.5, sequence: 3, time_stamp: "2012-05-25T04:53:38.000Z" }
  ]
}

Currently I have this:

class PatchDataSerializer < ActiveModel::Serializer
  attributes :max_timestamp, :measurement_type, :measurements

  def measurements
    [{ :measurement => object.measurement, :sequence => object.sequence, :time_stamp => object.time_stamp.to_i }]
  end

  def max_timestamp
  end

end

Which is returning:

[
  {
    "max_timestamp": null,
    "measurement_type": "ecg",
    "measurements": [
      {
        "measurement": 20.5,
        "sequence": 5,
        "time_stamp": 1464152018
      }
    ]
  },
  {
    "max_timestamp": null,
    "measurement_type": "ecg",
    "measurements": [
      {
        "measurement": 20.5,
        "sequence": 5,
        "time_stamp": 1464152018
      }
    ]
  }
]

As you can see this is not working. My biggest question is how do I push the measurements for each row into one array? I also need to return the max_timestamp once for the entire request, not for each row.

Any help is greatly appreciated.

Upvotes: 0

Views: 384

Answers (2)

Leo Correa
Leo Correa

Reputation: 19789

It seems to me you could create a new class that handles the aggregation of the data from measurements and then create a serializer for that class.

Depending on what version of AMS you're using it can be done differently but the gist is pretty much

class MeasurementsAggregation
  attr_reader :measurements, :type, :max_timestamp

  def initialize(type, measurements)
    @measurements = measurements
    @max_timestamp = measurements.max_by(&:updated_at).updated_at
    @type = type
  end
end

then your serializer would just be

class MeasurementsAggregationSerializer < ActiveModel::Serializer
  attributes :max_timestamp, :type

  has_many :measurements, serializer: MeasurementSerializer
end

and your measurement serializer would be

class MeasurementSerializer < ActiveModel::Serializer
  attributes :measurement, :sequence, :time_stamp
end

You may have to do some tweaking to get the PORO to work with AMS but that should be the gist of it.

Upvotes: 1

Juan Fuentes
Juan Fuentes

Reputation: 1771

You could create a custom class method for the class your trying to serialize, something like this (I'm assuming you're filtering by type?)

def self.measurements_of_type(type)
  {
    max_timestamp: Class.maximum(:time_stamp),
    measurement_type: type
    measurements: Class.where(type: type).select(:measurement, :sequence, :time_stamp)
  }
end

Upvotes: 0

Related Questions