LoginSignup
0
0

More than 3 years have passed since last update.

Gem ~ active_model_serializers ~

Posted at

公式

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

Serializer の作成

be rails g serializer api::v1::articles_preview_serializer

作成されたファイル

app/serializers/api/v1/articles_preview_serializers.rb

class Api::V1::ArticlesPreviewSerializer < ActiveModel::Serializer
  # 出力したい値を指定
  attributes :id, :title, :updated_at
end

id, title, updated_at を出力するよう指定。

コントローラ

app/controllers/api/v1/articles_controllers.rb

module Api::V1
  class ArticlesController < BaseApiController
    def index
      articles = Article.all.order(updated_at: "DESC")
            # レスポンスの値が複数の場合、 each_serializer を使用する。
      render json: articles, each_serializer: Api::V1::ArticlesPreviewSerializer
    end
  end
end

serializer と同じ階層構造にすること。

作成した article を updated_at 順に昇順にするよう指定。

同階層に base_api_controllers.rb

class Api::V1::BaseApiController < ApplicationController
end

Request spec

とりあえずダミーデータを作成して参照する。

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 "記事の一覧が取得できる" do
      subject
    end
  end
end

Request spec についてはまた後日。

実行結果

[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"}]

UserSerializer の追加

class Api::V1::UserSerializer < ActiveModel::Serializer
  # 出力したい値を指定
  attributes :id, :name, :email
end

ArticlesPreviewSerializer の修正

class Api::V1::ArticlesPreviewSerializer < ActiveModel::Serializer
  # 出力したい値を指定
  attributes :id, :title, :updated_at
  belongs_to :user, serializer: Api::V1::UserSerializer
end

実行結果

[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"=>"2_janella@renner-dach.org"}},
 {"id"=>60,
  "title"=>"Cumque aut repudiandae numquam.",
  "updated_at"=>"2020-10-18T00:06:27.377Z",
  "user"=>{"id"=>70, "name"=>"Collen Stark Brakus", "email"=>"3_hung@wintheiser.org"}},
 {"id"=>58,
  "title"=>"Corporis molestiae dolor odit.",
  "updated_at"=>"2020-10-16T00:06:26.556Z",
  "user"=>{"id"=>68, "name"=>"Fr. Pauline Sporer Greenfelder", "email"=>"1_wendy@goldner.net"}}]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0