ããã«ã¡ã¯ãã¡ããã¢ã¨ã³ã¸ãã¢ã®ä¿ç«ã§ãã
ã¡ããã¢ã§ã¯ããã¯ã¿ã¼åãã«éå¶ãã¦ãããMedPeerãã®iOSã¢ããªã3æ15æ¥ã«ãªãªã¼ã¹è´ãã¾ãã!!!
ä»åã¯ãiOSã¢ããªéçºã®éç¨ã§ãAPIã®å®è£
ã«ActiveModelSerializersã使ã£ãã®ã§ãããã§å¾ãç¥è¦ãæ¸ãã¾ãã
ActiveModelSerializers ã使ã£ãçç±
jsonå½¢å¼ã®ã¬ã¹ãã³ã¹ãè¿å´ããå ´åãjbuilderã使ãã±ã¼ã¹ãå¤ãã®ã§ã¯ãªãã§ããããã
ã¡ããã¢ã§ããä»ã¾ã§å¤é¨ã¸ã®APIã«ã¯jbuilderã使ç¨ãã¦ãã¾ããã
ããããiOSã¢ããªç¨ã®APIã§ã¯ã以ä¸ã®ã¡ãªãããèæ
®ãã¦ãActiveModelSerializersã使ããã¨ã«ãã¾ããã
ã¡ãªããâ è¤éãªjsonãè¿ãéã«ãActiveModelSerializersã®æ¹ããã¬ã¹ãã³ã¹ãæ©ã
ã¹ãã¼ããã©ã³ç¨ã®APIã§ã¯ããªãã¹ãå°ãªããªã¯ã¨ã¹ãã§ç»é¢ã®æç»ã«å¿
è¦ãªãã¹ã¦ã®æ
å ±ãè¿å´ããããããä½éã«ãå
¥ãåã«ãªãjsonãè¿å´ãããã¨ãå¤ãã¨æãã¾ãããã®ãããviewãã¬ã³ããªã³ã°ããjbuilderã§ã¯æ±ç¨çãªãªãã¸ã§ã¯ãã¯partialåãããã¨ãæ±ãããã¾ãããpartialãå¼ã³åºãã®ã«æéãããã£ã¦ãã¾ãã¾ãã
ä¸æ¹ãActiveModelSerializersã§ã¯ãã¬ã¹ãã³ã¹ã®å
容ãå®ç¾©ããserializerã«has_many
ãhas_one
ã使ã£ã¦ãé¢é£ãããªãã¸ã§ã¯ããæå®ãããã¨ãã§ãã¾ããããã«ãããjbuilderãããç´ æ©ãã¬ã¹ãã³ã¹ãè¿ããã¨ãã§ãã¾ãã
ã¡ãªããâ¡ DSLãªæ¸ãæ¹ãããªãã¦ãã
jbuilderã®æ¸ãæ¹ã¯ãç´æçã«åããã¥ããè¨æ³ã«ãªãã±ã¼ã¹ãããã¾ãã
json.title @post.title json.body @post.body # {"title": "ã¿ã¤ãã«", "body": "æ¬æ"}
json.post @post, :title, :body # {"post": {"title": "ã¿ã¤ãã«", "body": "æ¬æ"}}} # ãããåãå 容ãè¿ã(ãã£ã¡ã®æ¹ãå°ãåããããã) json.post do json.title @article.title json.body @article.body end
ããã«ãpartialãèªãã ããifæãªã©ã®åå²ãå
¥ãã¨ãããåããã¥ãããªãã¾ãã
ActiveModelSerializersã§ã¯ãRubyã®è¨æ³ã«åã£ã¦æ¸ãããããåãããããã³ã¼ãã«ãªãã¾ãã
以ä¸ã®ç¹ãããã¡ããã¢ã®iOSã¢ããªç¨APIã¨ãã¦ãActiveModelSerializersãæ¡ç¨ãã¾ããã
使ç¨ä¾
ActiveModelSerializerã¯ä»¥ä¸ã®ããã«ä½¿ç¨ãã¾ãã
class PostsController < ApplicationController def show @post = Post.find(params[:id]) render json: @post, serializer: PostSerializer end end
class PostSerializer < ActiveModel::Serializer attribute :title # å¥åãã¤ãããæã¯keyã使ç¨ãã attribute :created_at, key: :timestamp # has_manyãhas_oneã§é¢é£ãããªãã¸ã§ã¯ããæå®ãã has_many :comments, serializer: CommentSerializer # ã¡ã½ãããå¼ã³åºããã¨ãå¯è½ attribute :published def published object.published_at.present? end end class CommentSerializer < ActiveModel::Serializer attribute :body end # ã¬ã¹ãã³ã¹ { title: "ã¿ã¤ãã«", timestamp: "2018-01-01T00:00:00+09:00", published: true, comments: [ { body: "ã³ã¡ã³ã" } ] }
並åé¢ä¿ã®è¤æ°Modelã«ç´ã¥ãAPIãè¿å´ãããå ´åãããã¾ãã
以ä¸ã®ä¾ã¯ãã¢ããªèµ·åæãªã©ãMasterãã¼ã¿ãåå¾ããæã®ã³ã¼ãã§ãã
class MasterDataController render json: MasterData.new, serializer: MasterDataSerializer end
class MasterData def first_master_data @first_master_data ||= Master::FirstMasterData.all end def second_master_data @second_master_data ||= Master::SecondMasterData.all end end
class MasterDataSerializer has_many :first_master_data has_many :second_master_data class FirstMasterDataSerializer attributes :id, :name end class SecondMasterDataSerializer attributes :id, :name end end
使ã£ã¦ã¿ãææ³
æ¸ãã¦ãã¦ããã®ãããã¤ã¾ããã¨ãããã¨ããªãã£ãã§ãã
ActiveRecordã®å»¶é·ã®ãããªä½ããªã®ã§ãã¤ã¾ãããã¨ãªãæ¸ããã¨ãã§ãã¾ããã
ã¾ãGitHubã®ããã¥ã¡ã³ããè±å¯ãªã®ã§ãåºæ¬çã«ããããªããã¨ããã£ã¦ãããã¥ã¡ã³ããã¿ãã°è§£æ±ºãã¾ããã
ã³ã¼ãéã¯jbuilderããå¤ããªãã®ã§ããããã«å®è£
ãããããè¤éãªjsonãè¿ãå¿
è¦ããªããå ´åã¯jbuilderã§ãããã¨æãã¾ãã
éã«ãé·ãééç¨ããããæ¡å¼µããå¯è½æ§ãããAPIãç¨æããå ´åã¯ãActiveModelSerializerããããã§ã^^
ãã¾ã(Active Model Serializer 㨠jbuilder ã®ããã©ã¼ãã³ã¹æ¯è¼)
posts#showã¢ã¯ã·ã§ã³ã«ã¦ãpostsã1件ã¨has_manyé¢ä¿ã®commentsã20件ãã¬ã¹ãã³ã¹ã«è¨å®ããã±ã¼ã¹ã§æ¯è¼ãã¾ãã
jbuilder
# app/controllers/posts_controller.rb class PostsController < ApplicationController def show @post = Post.find(params[:id]).preload(:comments) end end # app/views/posts/show.json.jbuilder json.title @post.title json.page_views do json.partial! partial: 'comment', collection: @post.comments, as: :comment end # app/views/posts/_comments.json.jbuilder json.body comments.body
å®è¡çµæã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
Processing by PostsController#show as JSON Posts Load (1.0ms) SELECT `posts`.* FROM `posts` LEFT JOIN `comments` ON `posts`.`id` = `comments`.`post_id` Rendered posts/_comments.jbuilder (0.3ms) Rendered posts/_comments.jbuilder (0.3ms) Rendered posts/_comments.jbuilder (0.3ms) (commentsã®æ°ã ãç¶ã) Rendered posts/_comments.jbuilder (0.3ms) Rendered posts/show.json.jbuilder (47.2ms) Completed 200 OK in 87ms (Views: 49.6ms | ActiveRecord: 7.5ms)
Active Model Serializers
# app/controllers/posts_controller.rb class PostsController < ApplicationController def show @post = Post.find(params[:id]).preload(:comments) render json: @post, serializer: PostSerializer end end # app/serializers/posts_serializer.rb class PostSerializer < ActiveModel::Serializer attribute :title # has_manyãhas_oneã§é¢é£ãããªãã¸ã§ã¯ããæå®ãã has_many :comments, serializer: CommentSerializer end class CommentSerializer < ActiveModel::Serializer attribute :body end
å®è¡çµæã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
Processing by PostsController#show as HTML Posts Load (1.0ms) SELECT `posts`.* FROM `posts` LEFT JOIN `comments` ON `posts`.`id` = `comments`.`post_id` [active_model_serializers] Rendered PostSerializer with ActiveModelSerializers::Adapter::Attributes (11.23ms) Completed 200 OK in 41ms (Views: 9.7ms | ActiveRecord: 7.9ms)
ã¨ããããã¨ã§2å以ä¸ã®é度ã§ã¬ã¹ãã³ã¹ãè¿ããã¨ãã§ãã¾ããã
å®éã®MedPeerã¢ããªã§ã¯ã3é4éã«å
¥ãåã«ããjsonãè¿å´ãããã¨ããããããActive Model Serializers ã®æ©æµã¯è¨ãç¥ããªããã®ãããã¾ãã
(âï¸ ÕਠÕ)âï¸æ¯éèªè ã«ãªã£ã¦ãã ãã
ã¡ããã¢ã§ã¯ä¸ç·ã«åã仲éãåéãã¦ãã¾ãã ãå¿åããå¾ ã¡ãã¦ããã¾ãï¼
â åéãã¸ã·ã§ã³ã¯ãã¡ã
https://medpeer.co.jp/recruit/entry/
â éçºç°å¢ã¯ãã¡ã