Gem ~ active_model_serializers ~

official

https://github.com/rails-api/active_model_serializers/tree/v0.10.6/docs

Creating a Serializer

be rails g serializer api::v1::articles_preview_serializer

Created file

app/serializers/api/v1/articles_preview_serializers.rb

class Api::V1::ArticlesPreviewSerializer < ActiveModel::Serializer
  #Specify the value you want to output
  attributes :id, :title, :updated_at
end

Specify to output id, title, updated_at.

controller

app/controllers/api/v1/articles_controllers.rb

module Api::V1
  class ArticlesController < BaseApiController
    def index
      articles = Article.all.order(updated_at: "DESC")
			#If there are multiple response values, each_Use serializer.
      render json: articles, each_serializer: Api::V1::ArticlesPreviewSerializer
    end
  end
end

Make it the same hierarchical structure as serializer.

Specify that the created articles are sorted in ascending order in updated_at order.

Base_api_controllers.rb in the same hierarchy

class Api::V1::BaseApiController < ApplicationController
end

Request spec

For the time being, create dummy data and refer to it.

spec/requests/api/v1/article_request_spec.rb

require 'rails_helper'

RSpec.describe "Api::V1::Articles", type: :request do
  describe " GET /api/v1/article " do
    subject { get(api_v1_articles_path) }

    before { create(:article, updated_at: 3.days.ago ) }
    before { create(:article) }
    before { create(:article, updated_at: 1.days.ago ) }
    it "You can get a list of articles" do
      subject
    end
  end
end

More on Request specs at a later date.

Execution result

[1] pry(#<RSpec::ExampleGroups::ApiV1Articles::GETApiV1Article>)> res = JSON.parse(response.body)
=> [{"id"=>56, "title"=>"Consequuntur quia corporis perspiciatis.", "updated_at"=>"2020-10-17T22:19:32.120Z"},
 {"id"=>57, "title"=>"Molestiae tempore recusandae qui.", "updated_at"=>"2020-10-16T22:19:32.122Z"},
 {"id"=>55, "title"=>"Consectetur nam odio voluptatibus.", "updated_at"=>"2020-10-14T22:19:31.192Z"}]

Add UserSerializer

class Api::V1::UserSerializer < ActiveModel::Serializer
  #Specify the value you want to output
  attributes :id, :name, :email
end

Fixed ArticlesPreviewSerializer

class Api::V1::ArticlesPreviewSerializer < ActiveModel::Serializer
  #Specify the value you want to output
  attributes :id, :title, :updated_at
  belongs_to :user, serializer: Api::V1::UserSerializer
end

Execution result

[1] pry(#<RSpec::ExampleGroups::ApiV1Articles::GETApiV1Article>)> res = JSON.parse(response.body)
=> [{"id"=>59,
  "title"=>"Esse facere cum rerum.",
  "updated_at"=>"2020-10-19T00:06:27.375Z",
  "user"=>{"id"=>69, "name"=>"Eduardo Kohler Haley", "email"=>"[email protected]"}},
 {"id"=>60,
  "title"=>"Cumque aut repudiandae numquam.",
  "updated_at"=>"2020-10-18T00:06:27.377Z",
  "user"=>{"id"=>70, "name"=>"Collen Stark Brakus", "email"=>"[email protected]"}},
 {"id"=>58,
  "title"=>"Corporis molestiae dolor odit.",
  "updated_at"=>"2020-10-16T00:06:26.556Z",
  "user"=>{"id"=>68, "name"=>"Fr. Pauline Sporer Greenfelder", "email"=>"[email protected]"}}]

Recommended Posts

Gem ~ active_model_serializers ~
Ruby and Gem