Railsアップグレード百景
by
r7kamura
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
RailsΞοϓάϨʔυඦܠ by r7kamura
Slide 2
Slide 2 text
ࣗݾհ • Ryo Nakamura • https://github.com/r7kamura • https://twitter.com/r7kamura • ϑϦʔϥϯεͰRailsͷࣄΛ͍ͬͯ·͢ • Rails 1͔Β6·ͰΞοϓάϨʔυͨ͜͠ͱ͋Γ
Slide 3
Slide 3 text
ࠓ͢͜ͱ ʮRailsͬͯ͜͏͍͏ײ͡ͰΞοϓάϨʔυ͍ͯ͘͠ͱ ྑ͍ͷͶʯͱ͍͏ͷ͕͔ͬͯΒ͑Δͱخ͍͠ • ࣄલ४උ • ΞοϓάϨʔυͷྲྀΕ (Rails 4, 5, 6͕ର) • ࣄྫू
Slide 4
Slide 4 text
ࠓ͞ͳ͍͜ͱ • Πϯϑϥͷ (1͚ͩΞοϓάϨʔυ͢ΔɺϦΫΤετΛෳ ͯ͠ݕূڥʹྲྀ͢) • ৫ίϛϡχέʔγϣϯͷ (ۀҕୗͰ୲͢Δ߹ͷ͋ Ε͜ΕɺϨϏϡΞʔׂͷ͋Ε͜Ε) • QAख๏ͷ (ςετ߲ͷબఆํ๏ͳͲ) • ݩʑΞοϓάϨʔυͷ·ΓͲ͜Ζ100બʹ͢Δ༧ఆͩͬͨ
Slide 5
Slide 5 text
ࣄલ४උ
Slide 6
Slide 6 text
࡞ۀલʹ͓ͬͯ͘ͱྑ͍͜ͱ • GitHub Issuesͷϥϕϧͷཧ ← ࢁPRΛग़͢ͷͰ • ςετݕূڥͷඋ ← ԿσϓϩΠ͢ΔͷͰ • ։ൃڥͷඋ ← ԿςετΛಈ͔͢ͷͰ • CIͷඋ ← Կಈ͔͢͜ͱʹͳΔͷͰ • RuboCopͷඋ ← ͍Ζ͍Ζॿ͔ΔͷͰ
Slide 7
Slide 7 text
GitHub Issuesͷϥϕϧͷཧ
Slide 8
Slide 8 text
ςετݕূڥͷඋ • RailsΞοϓάϨʔυ࣌ʹͦ͜ςετͷ͋Γ͕ͨ͞Λ ௧ײ͢Δ͜ͱʹͳΔ… • Pull Request͝ͱʹݕূڥ͕༻ҙ͞ΕΔ͕ͭ͋Δ ͱຊʹศར • μϛʔσʔλ͕༻ҙ͞Ε͍ͯΔݕূڥͩͱϕετ ʢ2ͭҎ্Ϩίʔυ͕͋Δͱࣦഊ͢Δͱ͔͋Δʣ
Slide 9
Slide 9 text
CIͷඋ • ߴԽ (ྫ: Performance Pricing PlanରԠ) • ςετͷछྨͷݟ͠ (ྫ: db:migrationςετ) • ϩάͷݟ͠ (ྫ: ςετதͷෆཁͳඪ४ग़ྗ) • ςετ݁Ռͷू (ྫ: CircleCIͷςεταϚϦʔରԠ)
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
RuboCopͷඋ • Bundler/Gemfileʹै͓ͬͯ͘ͱޙͷมߋ࡞ۀָ͕ • RailsΧςΰϦͷCop༗ޮԽ͓ͯ͘͠ͱḿΔ • ηΩϡϦςΟύϑΥʔϚϯεͷݟͯ͘ΕΔ • ݹ͍APIͷݺͼग़͠Λauto-correctͰ͖Δͷศར
Slide 13
Slide 13 text
GemfileͰͷఆٛॱংͷ᠘ • গͳ͘ͱrailsઌ಄ʹॻ͍͓͍ͯͨํ͕ྑ͍ • ͜͏͍͏ίʔυؚ͕·ΕΔGem͕͋ΔͨΊ if defined?(Rails) … end
Slide 14
Slide 14 text
ΞοϓάϨʔυͷྲྀΕ
Slide 15
Slide 15 text
ΞοϓάϨʔυͷྲྀΕ 1. bundle update 2. ςετͱमਖ਼ 3. masterͷऔΓࠐΈ (ਵ࣌σϓϩΠ) 4. όʔδϣϯมߋΛσϓϩΠ 5. ࣄޙॲཧ
Slide 16
Slide 16 text
bundle update • جຊతʹ —conservative ΦϓγϣϯΛ͚Δ • bundle update --conservative actionmailer actionpack actionview activemodel activerecord activesupport rails railties Έ͍ͨʹΔ • ґଘgemΛઌʹΞοϓάϨʔυ͠ͳ͍ͱ͍͚ͳ͍έʔε͋Δ • deviserails-i18nΈ͍ͨʹಉ͡όʔδϣϯͰRails 4 & 5྆ରԠ Ͱ͖ͳ͍gem͋ΔͷͰҙ (͜͏͍͏gem͕ຊʹհ)
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
ςετͱमਖ਼ 1. ςετ͕ਖ਼ৗʹ࣮ߦ͞ΕΔΑ͏ʹ͢Δ 2. ςετͷࣦഊΛ0݅ʹ͢Δ 3. ܯࠂͷΛݮΒ͍ͯ͘͠ 4. masterʹมߋ͕͋ΕऔΓࠐΜͰ1ʹΔ
Slide 19
Slide 19 text
ςετҎ֎Ͱ͓͢͢Ίͷ֬ೝࣄ߲ • rails console ͕ਖ਼ৗʹىಈ͢Δ͔? • RubyCSSͷϑΝΠϧΛฤूͨ͠ͱ͖ө͞ΕΔ͔? • Rails.application.eager_load! ͕ޭ͢Δ͔? • rails db:rollback db:migration ͕ޭ͢Δ͔? • RAILE_ENV=production rails assets:precompile ͕ޭ͢Δ͔?
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
masterͷऔΓࠐΈ • Ұʹࢁมߋ͢ΔͱݕূrevertେมͳͷͰɺ όʔδϣϯมߋ࣌ͷࠩ࠷খԽ͍ͨ͠ • มߋ৽چόʔδϣϯ྆ରԠͳܗͰՃ͑Δ • ςετͷमਖ਼ͱฒߦͯ͠masterʹมߋΛೖΕ͍ͯ͘ (ͷͰ༧Ίcommit͚͓ͯ͜͏)
Slide 23
Slide 23 text
ࠩΛখ͘͢͞Δ • bundle update ͷ —conservative Φϓγϣϯ • rails_kwargs_testing • activerecord-compatible_legacy_migration
Slide 24
Slide 24 text
rails_kwargs_testing • ςετͰͷϝιουҾ͕ get “/”, params: { page: “1” } ʹ • RSpecͷϩά͕ܯࠂͰຒΊਚ͘͞Εͯ͠·͏ • 5.0Ҡߦͱಉ࣌ʹೖΕΔͱɺ࡞ۀதʹmasterଆͰςετ͕ॻ ͖ΘΔͱconflict͢Δ͠৭ʑେม • 4.2Ͱ৽ܗࣜͰॻ͚ΔΑ͏ʹͯ͘͠ΕΔͷ͕͜ͷgem
Slide 25
Slide 25 text
Rails/HttpPositionalArguments • લड़ͷϝιουҾΛݕࠪ͢ΔCop • લճrails5-spec-converterΛ͕ͬͨbuggy… • CopͰauto-correct͢Δͷ͕ྑͦ͞͏
Slide 26
Slide 26 text
activerecord-compatible_legacy_migration • class Foo < ActiveRecord::Migration[4.2]; end ͷΑ͏ʹόʔδϣ ϯΛࢦఆ͠ͳ͍ͱ͍͚ͳ͘ͳͬͨ (5.0Ͱܯࠂɺ5.1Ͱྫ֎) • 4.2ͰActiveRecord::Migration[4.2]ͱॻ͚ͳ͍ͷͰɺࣄલʹର ԠͰ͖ͣɺόʔδϣϯมߋ࣌ʹ͜ͷରԠಉ࣌ʹؚ·Εͯ͠·͏ • 4.2Ͱ5.xͰಈ࡞͢ΔܧঝݩΛఏڙͯ͘͠ΕΔͷ͕͜ͷgem • ※·͋ԿରԠͤͣ5.0ʹͯ͠db:migration࣌ʹͪΐͬͱܯࠂ͕ ग़ΔΑ͏ʹͳΔ͚͔ͩͩΒɺͦΜͳʹॏཁͰͳ͍͔
Slide 27
Slide 27 text
ࣄྫू
Slide 28
Slide 28 text
Rails 4.2.9ʹͨ͠ΒparamsपΓͰྫ֎͕… • get “/search/:query”, to: “items#search” • Rails 4.2.9ͩͱURIύε͕ASCII-8BITʹͳΔ • Rails 4.2.9ͰյΕͯ4.2.10Ͱͬͨ • RailsͷύονόʔδϣϯҰؾʹ࠷৽·Ͱ্͛Α͏
Slide 29
Slide 29 text
Rails 5ʹͨ͠ΒINSERTͰྫ֎͕… • `created_on` DATE DEFAULT ‘0’ • Rails 5͔ΒσϑΥϧτͰMySQLαʔόଆͷSQLϞʔυ ߟྀ͢ΔΑ͏ʹͳͬͨ • ݁ՌతʹNO_ZERO_DATEͱSTRICT_TRANS_TABLES ͕༗ޮԽ͞ΕͯΤϥʔʹ • Rails 4ͷ͔Βݫ͓ͯ͘͘͠͠ͱ٢
Slide 30
Slide 30 text
Rails 5ʹͨ͠Βىಈ࣌ʹྫ֎͕… • ActiveSupport.on_load(:action_controller) { include Foo } • APIϞʔυͬͯͳ͍ͷʹ ActionController::API ͕ྫ֎Λ ग़͢Α͏ʹͳͬͨͧ??? • ActiveSupport.on_load(:action_controller) ActionController::API ͷಡΈࠐΈ࣌ʹൃՐ͢Δ • Rails 4 ࣌ʹఆ͍ͯ͠ͳ͔ͬͨ͜ͱ͕ى͖ͨ
Slide 31
Slide 31 text
Rails 5ʹͨ͠Β404͕204ʹ… • ςϯϓϨʔτ͕ଘࡏ͠ͳ͍ͱ͖ͷڍಈ͕มΘͬͨ • Rails 4ͩͱྫ֎͕ൃੜͯ͠404 • Rails 5ͩͱ204 • ͜͜Ͱʮ͍ͭͰʹϦϑΝΫλϦϯάͯ͠͠·͓͏ʯ ͱ͔Γ࢝ΊΔͱຊʹྑ͘ͳ͍…
Slide 32
Slide 32 text
Rails 5ʹͨ͠ΒControllerͷςετ͕… post “/login”, name: “…”, password: “…” post “/articles”, body: “…” • Rails 5.0 ʹROW_POST_DATA͕Ҿ͖ܧ͕ΕΔෆ۩߹͕ • Rails 5.2 Ͱमਖ਼͞Ε͍ͯΔ͕ 5.0 Ͱͦͷ·· • దʹΞϓϦଆͰύον͙ͯͯ྇ͷ͕ྑͦ͞͏
Slide 33
Slide 33 text
Rails 5ʹͨ͠ΒparamsपΓͰςετ͕… • get “/foo”, bar: nil, baz: Time.current • Rails 4 ·ͰςετͰͨ͠paramsͱͯͨ͠͠ΦϒδΣΫ τ͕ControllerͰࢀর͞Ε͍ͯͨ • Rails 5͔Βී௨ʹΤϯίʔυ/σίʔυ͞ΕΔ • ຊདྷ͋Γಘͳ͍݅ͷςετ͕େྔʹଘࡏ͍ͯͨ͠ • Content-Typeparamsͷ༰Λࡉ͔͘ݟͯ͠ରԠ͢Δ
Slide 34
Slide 34 text
Rails 5ʹͨ͠ΒparamsपΓͰྫ֎͕… • ActionController::Parameters͕HashΛܧঝ͠ͳ͘ͳͬͨ (େม͗͢Δ) • ҰԠ5.0Ͱܯࠂ͖ͰHash૬ͷϝιου͕ݺͼग़ͤΔ Α͏ʹߟྀ͞Ε͍ͯΔ • ͔͠͠ params.is_a?(Hash) ʹഊ • ͜ͷܧঝݩมߋͷӨڹ͕Rails 5ԽͰҰ൪ා͍
Slide 35
Slide 35 text
Rails 5.1ʹͨ͠ΒparamsपΓͰςετ͕… • get “/”, params: nil ʹͯ͠ΔͱRails 5.1͔Βྫ֎͕ग़Δͧ??? • υΩϡϝϯτΛݟΔݶΓparamsnilͰྑͦ͞͏ • Ͳ͏ΒRails 5.1ʹ͢Δͱ͖ʹ࣮ΛϛεͬͨͬΆ͍ • rails/rails#34737 Λग़ͯ͠mergeࡁΈ • Rails 6 ʹ͔͠ೖΒͳ͍ͷͰ 5 Ͱ݁ہ nil Λͤͳ͍
Slide 36
Slide 36 text
Rails 5.1ʹͨ͠ΒARͷthrough͕… • Rails 5.1͔Β has_many :b, through: a ͕ޮ͔ͳ͍ͧ??? • ௐࠪͨ͠ΒҰ࿈ͷincludeͷதͰ͜͏͍͏͜ͱ͕ى͖͍ͯͨ X.has_many :a X.has_many :b, through: :a X.has_many :a # ← ͜͜Ͱthrough: :aͷఆ͕ٛফ͑Δ
Slide 37
Slide 37 text
Rails 6ʹͨ͠Βvalidation͕… • Rails 6͔Βhas-and-belongs-to-manyͳதؒϨίʔυͰ ఆ͍ٛͯ͠Δvalidation͕ಈ͔ͳ͘ͳͬͨ…? • ࠶ݱίʔυॻ͍ͯTwitterͰΰωͯͨΒkamipo͞Μ͕ rails/rails#35116 Ͱͯ͘͠Εͨ • ͜ͷޙผ݅Ͱ࠶ݱίʔυॻ͍ͯ2ʙ3ճΰωͯͨΒ kamipo͞Μ͕શ෦ͯ͘͠Εͨ…
Slide 38
Slide 38 text
Rails 6ʹͨ͠Βbelongs_toͷڍಈ͕… • ಉ͡Ϋϥε͔Βಉ͡Ϋϥεʹbelongs_toΛผ໊Ͱఆ ͍ٛͯ͠ΔϞσϧͰɺinverse_of͕ڞ༻͞Εͯ͠·͏ • ࠶ݱՄೳ͕ͩະղܾ • rails/rails#35204 ͰIssueΛཱͯͯௐࠪத
Slide 39
Slide 39 text
Tips1: Contributing to Rails͕ศར • rails/railsͷIssueͷग़͠ํ • Pull Requestͷग़͠ํ • ςετͷಈ͔͠ํ • ࠷খͷ࠶ݱίʔυΛॻ͘ͱ͖ͷςϯϓϨʔτ
Slide 40
Slide 40 text
Tips2: r7kamuraʹؙ͛͢Δબࢶ • ࣮ࡍRailsΞοϓάϨʔυΛ୭͕୲͢Δͷ͔ʁ • ਓࣄධՁݸʑਓͷϞνϕʔγϣϯͷ͕͋Δ • ୲ऀΛ͚ͯۀҕୗͷਓʹؙ͛͢Δͱ͍͏ͷ ৭ʑߟ͑ΔͱΘΓͱ༗ޮͩͱࢥ͍ͬͯΔ • RailsΞοϓάϨʔυͷࣄΛืू͍ͯ͠·͢
Slide 41
Slide 41 text
͓͞Β͍
Slide 42
Slide 42 text
͓͞Β͍ • ࡞ۀͱฒߦͯ͠։ൃج൫Λ͍͑ͯ͜͏ • ύονόʔδϣϯҰؾʹ্͛Α͏ • όʔδϣϯมߋ࣌ͷࠩΛ࠷খԽ͍ͯ͜͠͏ • ᠘ଟ͍͕ਅ໘ʹςετॻ͍ͯͨΒݕͰ͖Δ • Railsଆ͕͓͔͍͠ͳͱࢥͬͨΒ࠶ݱίʔυΛॻ͜͏