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
140
Product Manager の Job Description
morygonzalez
3
2.6k
Lokka についての LT
morygonzalez
0
340
BitBar で快適な生活
morygonzalez
1
3.5k
君も Arel おじさんになって ActiveRecord のクエリを高速化しよう / Let's become Uncle Arel
morygonzalez
1
270
gyowitter のご紹介
morygonzalez
0
34k
Other Decks in How-to & DIY
See All in How-to & DIY
【技術カンファレンス運営の裏側】Iwaken Lab 技術好き学生の近況報告 & ことみんさんに技術カンファレンス運営の裏側を聞いちゃう会
kotomin_m
4
240
「AITRIOS」でトカゲの活動量を可視化
hoshinoresearch
0
320
新婚19年目から学ぶ夫婦円満の正しい歩き方 / Life is beautiful
soudai
11
4.1k
DroidKaigi 2024 - 海外就職というキャリアの選択肢
iyotetsuya
1
510
Terra Charge|EVコンセントご利用ガイドブック / Terra Charge EV Charger Guidebook
contents
0
1.1k
240420MapillaryMeetup2024Tokyo
tosseto
0
180
静岡県のお相撲さん20240509/sumo_wrestler_from_shizuoka_prefecture_20240509
nicepapa_hirano
0
110
データエンジニアのためのテクニカルサポートとLLM
yutomatsunoo
0
1k
[너구리랑! 회고 밋업 2023] CTO 1년 회고와 회고를 바탕으로 만든 프로젝트에 대한 회고 - 전문가가 되는 방법 // 한날 님
develop_neoguri
0
150
メイカーフェア東京、深セン、台北、サンフランシスコにみるFactory Automationみ/製造業み
takasumasakazu
0
1k
Career Opportunities In WordPress
wchk2023
0
240
骨折と入院とIoT #iotlt
n0bisuke2
1
230
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
33
1.5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
KATA
mclloyd
29
14k
A Tale of Four Properties
chriscoyier
157
23k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
The Cost Of JavaScript in 2023
addyosmani
45
7k
We Have a Design System, Now What?
morganepeng
51
7.3k
BBQ
matthewcrist
85
9.4k
For a Future-Friendly Web
brad_frost
175
9.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