I’m pretty sure we could pass the results of this AR query to an InvoiceSerializer:
class Api::V1::RevenueController < ApplicationController
def show
render json: InvoiceSerializer.new(Invoice.unshipped_revenue(quantity = 10))
end
end
class InvoiceSerializer
include JSONAPI::Serializer
set_type :invoice
set_id :id
attributes :unshipped_revenue
end
2. Very interested to see the logic you’d recommend for handling empty query/quantity strings in params.
I would probably do something like this for quantity:
def most_items
if params[:quantity].nil? #postman is looking for a 400 status here, but this is a better user experience. Either is fine as long as it is handled in some way.
<happy path code with default limit of 5>
elsif params[:quantity].is_a?(Integer) && params[:quantity].positive?
<happy path code with limit of params[:quantity]>
else
render json: { data: [], error: 'error' }, status: 400
end
end
More generally, I’ve found this breakdown of nil?/empty?/blank?/present? really helpful in checking param values.
3. I used the custom attribute feature in my serializer to return the count. Is that the proper way to do it?
Yes!
4. I'm having trouble testing the query we went over in class. It would be nice to see an example for the request spec for it maybe.
Does anyone have a spec they wrote for the query we went over in class or any of the other business logic they'd like to share? Please add it in the comments! I will add one later as well.