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

Rails 5.x

Rails 5.x

Rails 5.0 の変更点とRails 5.1 で入りそうな変更について紹介しています

Avatar for Shinichi Maeshima

Shinichi Maeshima

August 18, 2016
Tweet

More Decks by Shinichi Maeshima

Other Decks in Technology

Transcript

  1. ίʔϧόοΫνΣʔϯͷఀࢭ ৚݅ͷมߋ • 有名なやつ • コールバックの停止条件が「false を返す」から 「throw(:abort)」になった • 4.2からアップグレードした人は後方互換性を保った設定

    になっているはず • config/initializers/new_framework_default.rb に 設定ある w IUUQSBJMTHVJEFTKQVQHSBEJOH@SVCZ@PO@SBJMTIUNMUISPXBCPSU &"&#&#$&"#&&&"' &&"&#$&#&&$&"% "&&#
  2. belongs_toのrequired オプションがデフォルトに • Rails 4.2より belongs_to :user, required: true として関連先の存在を調べるバリデーションを定義で

    きた • 5.0 からはデフォルトで required: true • オフにしたい場合は optional: true • 4.2からアップグレードした人は後方互換性を保った 設定になっているはず • config/initializers/new_framework_default.rb に設定ある
  3. ։ൃதͷΩϟογϡͷ ΦϯΦϑ੾Γସָ͕͑ʹ # Enable/disable caching. By default caching is disabled.

    if Rails.root.join('tmp/caching-dev.txt').exist? config.action_controller.perform_caching = true config.cache_store = :memory_store config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=172800' } else config.action_controller.perform_caching = false config.cache_store = :null_store end DPOpHFOWJSPONFOUTEFWFMPQNFOUSC
  4. production 環境での起動後 は自動読み込みが無効になる • http://railsguides.jp/ upgrading_ruby_on_rails.html#production- %E7%92%B0%E5%A2%83%E3%81%A7%E3%81%AE%E8%B5%B7%E5%8B %95%E5%BE%8C%E3%81%AF%E8%87%AA%E5%8B%95%E8%AA%AD %E3%81%BF%E8%BE%BC%E3%81%BF%E3%81%8C%E7%84%A1%E5%8A %B9%E3%81%AB%E3%81%AA%E3%82%8B

    • レアケースだけど、config.autoload_paths << ‘lib' のみ だとはまる可能性あり • ↑↑では Rails.application.config.enable_dependency_loadingをtrue に設定してくださいって書いてあるけど、 config.eagerload_paths << ‘lib’としておくだけで解決で きるケースが多そう
  5. ಛఆͷςʔϒϧͷϨίʔυͷTBWFϝιο υΛҰ࣌తʹແޮʹͰ͖ΔΑ͏ʹͳͬͨ class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true

    after_create -> { Notification.create! comment: self, recipients: commentable.recipients } end module Copyable def copy_to(destination) Notification.suppress do # Copy logic that creates new comments that we do not want # triggering notifications. end end end
  6. "3ͷϞσϧͷ͏ͪɺͲͷଐੑʹΞΫη ε͍ͯ͠Δ͔ΛऔಘͰ͖ΔΑ͏ʹͳͬͨ class UsersController < ApplicationController def index @users =

    User.select(:name, :email) end end Processing by UsersController#index as HTML User Load (0.1ms) SELECT "users".* FROM "users" Rendered users/index.html.erb within layouts/application (1.0ms) ["name", "email"] গ͚ͩ͠ߴ଎Խ
  7. IFSPLVରԠ w IFSPLVͰ͸ɺQSPEVDUJPOͷ%#ফͪ͠ΌͬͨΜ͚ͩͲͳΜͱ͔෮چͰ͖ͳ͍ʁΈ ͍ͨͳ໰͍߹Θ͕ͤ݁ߏ͋ΔΒ͘͠ɺͦΕΛݮΒͨ͢Ίͷमਖ਼͕ೖͬͨ w SBJMTECESPQͳͲͷ%#ഁյܥλεΫ͕QSPEVDUJPO؀ڥͰؒҧ࣮͑ͯߦͯ͠͠· ͏ͷΛ๷ࢭ w %*4"#-&@%"5"#"4&@&/7*30/.&/5@$)&$,ɹͱ͢Δͱ࣮ߦͰ͖Δ w

    Ұ൪࠷ޙʹ%#Λϩʔυͨ͠FOWJSPONFOUͱɺ࡟আ͢Δ%#ͷFOWJSPONFOU͕ҟͳ Δͱ࡟আͰ͖ͳ͍ w EFWFMPQNFOU༻ͷϚγϯͰQSPEVDUJPOͷ%#Λؒҧͬͯফ͢Έ͍ͨͳͷΛ཈੍ w %#࡟আ͢ΔFOWJSPONFOUͰCJOSBJMTECFOWJSPONFOUTFUͱ͢Δͱ࣮ߦͰ͖Δɹ
  8. IFSPLVରԠ w ؅ཧ༻ͷςʔϒϧΛ࡞Δඞཁ͕͋Δ w ͳ͍ͱCJOSBJMTECFOWJSPONFOUTFU͠Ζͱౖ ΒΕΔ w ৄ͘͠͸ͪ͜Β w IUUQZZBHJUVNCMSDPNQPTUSBJMT&"#

    &"&"&'EC&"#&"" &###UBTL&"&&#&"% "&"&&"# &"&&"
  9. SourceMaps • JavaScriptファイルの多いアプリケーションの開 発が便利になる • Sprockets 3までは、開発中はデフォルトでファ イルの結合をしない • ファイルが増えると遅い

    • しかし結合するとエラー箇所が特定しにくい • Sprockets 4からはファイルを結合してもエラー 箇所を特定できるようになった
  10. Provide encrypted secrets $ rails secrets:edit $ Please enter the

    master key: XXXXXX $ [Rails opens config/secrets.yml.enc in unencrypted form in EDITOR] $ [Saving the opened file will encrypt the file back into config/ secrets.yml.enc] • RAILS_MASTER_KEYがあればそれを使う • デプロイ時はこれ
  11. ActiveJob.retry_on class RemoteServiceJob < ActiveJob::Base retry_on CustomAppException # defaults to

    3s wait, 5 attempts retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 } retry_on(YetAnotherCustomAppException) do |exception| ExceptionNotifier.caught(exception) end retry_on ActiveRecord::StatementInvalid, wait: 5.seconds, attempts: 3 retry_on Net::OpenTimeout, wait: :exponentially_longer, attempts: 10 def perform(*args) # Might raise CustomAppException, AnotherCustomAppException, or YetAnotherCustomAppException for something domain specific # Might raise ActiveRecord::StatementInvalid when a local db deadlock is detected # Might raise Net::OpenTimeout when the remote service is down end end