Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails application development in API era

Rails application development in API era

API 時代の Rails アプリケーション開発について軽い話をしました

森井ゴンザレス

September 26, 2018
Tweet

More Decks by 森井ゴンザレス

Other Decks in How-to & DIY

Transcript

  1. ࣗݾ঺հ • @morygonzalez • pyama YAMAP ͱ͍͏ձࣾͰ Rails Τϯ δχΞΛ͍ͯ͠·͢

    • ৭ؾΛग़ͯ͠Πϯϑϥ΋΍͍ͬͯ·͢ @morygonzalez at Fukuoka.rb #107
  2. Client ͕ Rails way ʹࡌͬͯ͘Εͳ͍ • iOS ΞϓϦ • Android

    ΞϓϦ • Web F/E Client ͕౤͛ͯ͘Δ params ͕ ActiveRecord ͕ظ଴͢Δ͔ͨͪͰདྷ Δͱ͸ݶΒͳ͍ɻ @morygonzalez at Fukuoka.rb #107
  3. Majestic Monolith ™ ࣌୅ͷ Rails • View ·Ͱ Rails Ͱ࡞Δ͜ͱ͕લఏ

    • View ͔Β͸ ActiveRecord ͕ѻ͍͠΍͍͢ Parameter ͔͠΍ͬͯ ͜ͳ͍ • Model ʹϦΫΤετύϥϝʔλʔΛͦͷ··஫ೖͯ͠΋໰୊ͳ ͍ @morygonzalez at Fukuoka.rb #107
  4. ղܾࡦ 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
  5. ղܾࡦ 2 ଞʹ΋ྺ࢙తͳࣄ৘Ͱ࿦ཧ࡟আ΍ͬͯͨΓ Collection ΦϒδΣΫτ ͷߋ৽Λ͢΂ͯ DELETE INSERT ʹͨ͠Γ͠ͳ͍ͱ͍͚ͳͯ͘ɺ͢ ΂ͯͷҋΛٵऩ͢Δ

    AcceptNestedAttributesCustamizable ͱ ͍͏΍͹໊͍લͷϞδϡʔϧ΋͋Γ·͢… @morygonzalez at Fukuoka.rb #107
  6. Majestic Monolith ™ ࣌୅ͷ Rails • ! ʮ͜͜ͷ͜·͍෦෼ͷ৘ใΛදࣔ͢ΔͨΊʹεϩʔΫΤϦ͕ ग़ͪΌ͏ͳʙʯ •

    " ʮύʔγϟϧΩϟογϡͰΑ͘Ͷʁʯ গʑεϩʔΫΤϦ͕ग़ͯ͠·ͬͨͱͯ͠΋ύʔγϟϧΩϟογϡ ʢ View ͷ෦෼Ωϟογϡʣʹಀ͛Δ͜ͱͰ͓஡Λ୙͢͜ͱ͕Ͱ͖ ͨɻ @morygonzalez at Fukuoka.rb #107
  7. API ࣌୅ͷ Rails • ! ʮ͜͜ͷ͜·͍෦෼ͷ৘ใΛදࣔ͢ΔͨΊʹεϩʔΫΤϦ͕ ग़ͪΌ͏ͳʙʯ • " ʮrender

    :json ͢Δͱ͖Ωϟογϡ͢Δͱ͔ਖ਼ؾͰ͢ ͔ʁʯ ActiveModel Serializer ͳͲΛ࢖͍ Rails ͸ϞσϧͷσʔλΛγϦΞ ϥΠζ͢Δ܅ͱԽͯ͠͠·ͬͨͷͰ View ͷ෦෼Ͱద౰ʹΩϟο γϡ͢Δ͜ͱ͕Ͱ͖ͳ͍ɻ @morygonzalez at Fukuoka.rb #107
  8. Kaminari ͱ͸ʁ • ϖʔδωʔγϣϯͷ gem ʹ༗໊ͳ gem Ͱ͢ • API

    ࣌୅Ͱ΋ݱ໾Ͱɺ JSON ʹ meta ৘ใͱͯ͠ϖʔδωʔγϣ ϯ݁ՌΛ͚ͬͭͨ͘Γ͠·͢ @morygonzalez at Fukuoka.rb #107
  9. 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
  10. ๻͸͜͏ͨ͠οε 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
  11. ๻͸͜͏ͨ͠οε ंྠͷ࠶ൃ໌ײ͋Δ Paginator ΫϥεΛ࡞੒ ͠ɺ :cache_total_count ͷϑϥάΛݟͯ Rails.cache ʹ஗͍ ΫΤ

    Ϧ Λग़͢ total_count ͷ݁ՌΛΩϟογϡͨ͠ΓऔΓग़ͨ͠Γ͢ Δɻ ৄ͘͠͸ϒϩάʹॻ͖·ͨ͠ͷͰ͝ཡԼ͍͞ɻ Kaminari ͕ൃߦ͢Δ஗͍ COUNT ΫΤϦͷ݁ՌΛΩϟογϡ͢Δ - portal shit! @morygonzalez at Fukuoka.rb #107
  12. ·ͱΊ • ࢒೦ͳ͕Β Rails ͸ API ࣌୅ʹͳͬͯҎલ΄ͲศརͰ͸ͳ͘ͳͬ ͨ gem Λ͍͔ͭ͘ೖΕͨΒԿͱͳ͘࡞Εͯͨ࣌୅͸ऴΘͬͨͬΆ

    ͍ • ͱ͸͍͑ Ruby ͷ Productivity ͸࠷ߴͰ͢ʢΩϝΔͱؾ͍࣋ͪ ͍ʣ • ࣃΛ৯͍͠͹ͬͯؤுΓ·͠ΐ͏ @morygonzalez at Fukuoka.rb #107