Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rails application development in API era
Search
森井ゴンザレス
September 26, 2018
How-to & DIY
0
470
Rails application development in API era
API 時代の Rails アプリケーション開発について軽い話をしました
森井ゴンザレス
September 26, 2018
Tweet
Share
More Decks by 森井ゴンザレス
See All by 森井ゴンザレス
CI/CD がなかった会社で勝手に CI/CD を始めた話 (仮)
morygonzalez
0
130
Product Manager の Job Description
morygonzalez
3
2.6k
Lokka についての LT
morygonzalez
0
340
BitBar で快適な生活
morygonzalez
1
3.4k
君も Arel おじさんになって ActiveRecord のクエリを高速化しよう / Let's become Uncle Arel
morygonzalez
1
260
gyowitter のご紹介
morygonzalez
0
34k
Other Decks in How-to & DIY
See All in How-to & DIY
地域JAWSの現状とベストプラクティス
awsjcpm
0
360
わたしと技術コミュニティとキャリア
kotomin_m
2
180
バタフライ効果/butterfly_effect
florets1
0
230
バーチャルバナナとリアルバナナ #iotlt #TouchDesigner
n0bisuke2
0
100
240420MapillaryMeetup2024Tokyo
tosseto
0
170
一番やさしいDJ入門 2024
stefafafan
6
1.8k
GPT-4oに遅刻理由を考えてもらうボタン #gpt_4o #iotlt #chatgpt
n0bisuke2
0
240
リアル登壇だから気をつけたい「マイクの使い方」のコツ
shirayanagiryuji
0
180
Using AWS to build a launchable knowledge rocket 👉 Organize knowledge, accelerate learning and understand AI in the process
dwchiang
0
130
BVEと連動する誤自宅運転台のススメ!
usashirou
0
590
Notionでの快適メモ術
ayumu11
0
990
生成AIとミニ四駆で学ぶ! MATLAB/Simulink
covao
1
120
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
20
1.1k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Designing the Hi-DPI Web
ddemaree
280
34k
Designing for humans not robots
tammielis
250
25k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Adopting Sorbet at Scale
ufuk
73
9.1k
BBQ
matthewcrist
85
9.3k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Transcript
API ࣌ͷ Rails @morygonzalez at Fukuoka.rb #107
ࣗݾհ • @morygonzalez • pyama YAMAP ͱ͍͏ձࣾͰ Rails Τϯ δχΞΛ͍ͯ͠·͢
• ৭ؾΛग़ͯ͠Πϯϑϥ͍ͬͯ·͢ @morygonzalez at Fukuoka.rb #107
@morygonzalez at Fukuoka.rb #107
Mobile ΞϓϦ͋Δ͠ F/E ͷਓ Nuxt.js ͱ͔͍͕ͨΔͷͰແཧͰ ͢ ! @morygonzalez at
Fukuoka.rb #107
Client ͕ Rails way ʹࡌͬͯ͘Εͳ͍ • iOS ΞϓϦ • Android
ΞϓϦ • Web F/E Client ͕͛ͯ͘Δ params ͕ ActiveRecord ͕ظ͢Δ͔ͨͪͰདྷ ΔͱݶΒͳ͍ɻ @morygonzalez at Fukuoka.rb #107
Majestic Monolith ™ ࣌ͷ Rails • View ·Ͱ Rails Ͱ࡞Δ͜ͱ͕લఏ
• View ͔Β ActiveRecord ͕ѻ͍͍͢͠ Parameter ͔ͬͯ͠ ͜ͳ͍ • Model ʹϦΫΤετύϥϝʔλʔΛͦͷ··ೖͯ͠ͳ ͍ @morygonzalez at Fukuoka.rb #107
ղܾํ๏ @morygonzalez at Fukuoka.rb #107
params ͍͍ײ͡ʹͯ͘͠ΕΔ܅ Client ͕͛ͯ͘Δ params Λ ActiveRecord ͕ཉ͍͔ͨͪ͠ʹม ͯ͘͠ΕΔΫϥε͕͋Δͱศརɻ͓͠ΌΕʹݴ͏ͱ Form
Object ͱ͍͏ͭɻ @morygonzalez at Fukuoka.rb #107
ղܾࡦ 1 • ฐࣾͰ app/parameters/ σΟϨΫτϦΛ۷ͬͯ UserParameter Έ͍ͨͳΫϥεΛఆٛ͠ɺͦ͜ͰΑ͠ͳʹ Δ͜ͱʹ͠·ͨ͠ɻ •
StrongParameter ͰΔ͜ͱͷ͍ͭͰʹϦΫΤετϘσΟΛ ActiveRecord ͕ظ͢Δ͔ͨͪʹม͑ͪΌ͏ ʢ Client ͕͛ͯ͘Δ͍Βͳ͍ใࣺͯΔʣ class UserParameter < ApplicationParameter attr_parameter :name, :email, :password, :provider, :oauth_token, :device_id, :code, :redirect_uri def oauth_token @params[oauth_config&.token_field_name&.to_sym] end private def oauth_config return unless @params[:provider] Settings.oauth.send(@params[:provider]) end end @morygonzalez at Fukuoka.rb #107
ղܾࡦ 2 ଞʹྺ࢙తͳࣄͰཧআͬͯͨΓ Collection ΦϒδΣΫτ ͷߋ৽Λͯ͢ DELETE INSERT ʹͨ͠Γ͠ͳ͍ͱ͍͚ͳͯ͘ɺ͢ ͯͷҋΛٵऩ͢Δ
AcceptNestedAttributesCustamizable ͱ ͍͏໊͍લͷϞδϡʔϧ͋Γ·͢… @morygonzalez at Fukuoka.rb #107
Partial Cache Ͱ͓Λͤͳ͍ @morygonzalez at Fukuoka.rb #107
Majestic Monolith ™ ࣌ͷ Rails • ! ʮ͜͜ͷ͜·͍෦ͷใΛදࣔ͢ΔͨΊʹεϩʔΫΤϦ͕ ग़ͪΌ͏ͳʙʯ •
" ʮύʔγϟϧΩϟογϡͰΑ͘Ͷʁʯ গʑεϩʔΫΤϦ͕ग़ͯ͠·ͬͨͱͯ͠ύʔγϟϧΩϟογϡ ʢ View ͷ෦Ωϟογϡʣʹಀ͛Δ͜ͱͰ͓Λ͢͜ͱ͕Ͱ͖ ͨɻ @morygonzalez at Fukuoka.rb #107
API ࣌ͷ Rails • ! ʮ͜͜ͷ͜·͍෦ͷใΛදࣔ͢ΔͨΊʹεϩʔΫΤϦ͕ ग़ͪΌ͏ͳʙʯ • " ʮrender
:json ͢Δͱ͖Ωϟογϡ͢Δͱ͔ਖ਼ؾͰ͢ ͔ʁʯ ActiveModel Serializer ͳͲΛ͍ Rails ϞσϧͷσʔλΛγϦΞ ϥΠζ͢Δ܅ͱԽͯ͠͠·ͬͨͷͰ View ͷ෦ͰదʹΩϟο γϡ͢Δ͜ͱ͕Ͱ͖ͳ͍ɻ @morygonzalez at Fukuoka.rb #107
ྫ͑ Kaminari ͷ COUNT ݁ՌΛ Ωϟογϡ͢Δ @morygonzalez at Fukuoka.rb #107
Kaminari ͱʁ • ϖʔδωʔγϣϯͷ gem ʹ༗໊ͳ gem Ͱ͢ • API
࣌ͰݱͰɺ JSON ʹ meta ใͱͯ͠ϖʔδωʔγϣ ϯ݁ՌΛ͚ͬͭͨ͘Γ͠·͢ @morygonzalez at Fukuoka.rb #107
Kaminari ͕ग़͢ COUNT จ͕͍ • Ͱ͔͍ςʔϒϧʹରͯ͠͏ͱ COUNT จ͕͘ͳΔͱ͍͏ݱ ͕͋Γɺ Issue
͕Γ্͕ͬͯ· • paginate calls slow COUNT query for large sets (when it might not need to) · Issue #545 · kaminari/kaminari @morygonzalez at Fukuoka.rb #107
͜͏ͨ͠οε Controller Ͱ render :json ͢Δͱ͖ʹ :cache_total_count Φϓ γϣϯΛड͚औΕΔΑ͏ʹͯ͠ɺΩϟογϡ͢Δ͔൱͔ΛܾΊ Δɻ
def index activities = policy_scope(Activity.active).published.with_relations.order(id: :desc) render json: activities, paginate: true, cache_total_count: true end @morygonzalez at Fukuoka.rb #107
͜͏ͨ͠οε ंྠͷ࠶ൃ໌ײ͋Δ Paginator ΫϥεΛ࡞ ͠ɺ :cache_total_count ͷϑϥάΛݟͯ Rails.cache ʹ͍ ΫΤ
Ϧ Λग़͢ total_count ͷ݁ՌΛΩϟογϡͨ͠ΓऔΓग़ͨ͠Γ͢ Δɻ ৄ͘͠ϒϩάʹॻ͖·ͨ͠ͷͰ͝ཡԼ͍͞ɻ Kaminari ͕ൃߦ͢Δ͍ COUNT ΫΤϦͷ݁ՌΛΩϟογϡ͢Δ - portal shit! @morygonzalez at Fukuoka.rb #107
·ͱΊ • ೦ͳ͕Β Rails API ࣌ʹͳͬͯҎલ΄ͲศརͰͳ͘ͳͬ ͨ gem Λ͍͔ͭ͘ೖΕͨΒԿͱͳ͘࡞Εͯͨ࣌ऴΘͬͨͬΆ
͍ • ͱ͍͑ Ruby ͷ Productivity ࠷ߴͰ͢ʢΩϝΔͱؾ͍࣋ͪ ͍ʣ • ࣃΛ৯͍ͬͯ͠ؤுΓ·͠ΐ͏ @morygonzalez at Fukuoka.rb #107