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
330
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
音に負けない!子どもが騒いでいる脇でも快適オンラインMTGの秘伝
kaitou
0
300
本気でコミュニティを成功させたいなら_株式会社コミュカル Mitz
comucal
PRO
0
810
GPTsとラズパイ5で監視カメラを作ってみた #iotlt #chatgpt #raspberrypi
n0bisuke2
0
470
ドラムスティックケースを生まれ変わらせて一歩踏み出した話
scbc1167
0
160
【潔能講堂】 綠能科技動手做-磁生電燈 / 李薇平老師
learnenergy2
0
440
Invitation to Okinawa.rb in 2024
yasslab
PRO
1
670
drumstick_jacket.pdf
lyh125
0
500
LT(Lightning Talk)のドキドキ感を共有する IoT ぼっとキーホルダ!
scbc1167
0
250
Notionでの快適メモ術
ayumu11
0
960
What I Talk About When I Talk About Social Media
idealhack
0
110
こんなにあるの? 最近のIPAトレンドを ざっくりまとめてみた
watany
3
600
How to get hundreds of organic backlinks through statistics link building
ronishehu
0
130
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
32
2.4k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Gamification - CAS2011
davidbonilla
80
5k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
What's in a price? How to price your products and services
michaelherold
243
12k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Documentation Writing (for coders)
carmenintech
65
4.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
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