Rails 7 ใกล้มาถึง ไม่มี Release Date Confirmed แต่คาดว่าจะ Available ก่อน Christmas ไม่เหลือนานแล้ว Latest Version ณ ตอนเผยแพร่คือ 7.0.0.rc1 First Release Candidate Basecamp, HEY, Github, และ Shopify Run Rails 7 Alpha ใน Production แล้ว ดังนั้น RC ควร Stable ค่อนข้าง
ในบทความนี้ เราจะดู New Features และ Changes ที่ Rails 7 จะนำมา
Node และ Webpack Not Required
ใช่ อ่านถูกต้อง! JavaScript ใน Rails 7 ไม่ต้อง NodeJS หรือ Webpack อีกต่อไป และยังใช้ npm packages ได้
Transpile ES6 with Babel และ Bundle with Webpack ต้อง Setup เยอะ Rails Support ได้ดีด้วย Gem Webpacker แต่นำ Baggage มามาก Hard to Understand และ Make Changes โดยเฉพาะ Maintain Upgradability
ตอนนี้ Default สำหรับ New Apps ด้วย rails new คือ Import Maps ผ่าน Gem importmaps-rails แทนที่จะเขียน package.json และ Install Dependencies ด้วย npm หรือ yarn ใช้ ./bin/importmap CLI Pin (หรือ Unpin/Update) Dependencies
เช่น Install date-fns:
จะเพิ่ม Line ใน config/importmap.rb:
ใน JavaScript Code คุณใช้ต่อได้เหมือนเดิม:
One Thing: Setup นี้ No Transpiling ระหว่างที่เขียนและ Browser Gets ส่วนใหญ่ OK เพราะ Browsers ที่ Matter Support ES6 Out of the Box
แตหมายความว่า TypeScript หรือ JSX ใช้ไม่ได้ เพราะต้อง Transpile to JS ก่อนใช้
ถ้าอยาก React with JSX ยังต้อง Fall Back Different Setup (webpack/rollup/esbuild)
Rails 7 ทำได้ One Command With Chosen Strategy:
Turbolinks และ UJS ถูก Replace ด้วย Turbo และ Stimulus
Apps Generated with Rails 7 ได้ Turbo และ Stimulus (จาก Hotwire) Default แทน Turbolinks และ UJS Hotwire เป็น New Approach Deliver Fast Updates to DOM by Sending HTML Over the Wire
Encryption at Database Layer
Rails 7 Allow Mark Certain Database Fields Encrypted ใช้ Method encrypts บน ActiveRecord::Base หมายความว่าหลัง Setup เริ่มต้น เขียน Code แบบนี้ได้:
คุณใช้ Encrypted Attributes ต่อได้เหมือน Attribute ปกติ Rails 7 Encrypt/Decrypt อัตโนมัติระหว่าง Database และ App
แตมี Quirk เล็กน้อย: ไม่สามารถ Query Database by That Field ได้ Unless Pass deterministic: true Option to encrypts Method Deterministic Mode Less Secure Than Default Non-deterministic Mode ใช้แค่ Attributes ที่ Absolutely Need to Query
Asynchronous Querying
มี Method load_async ใช้ตอน Query Data Fetch Results in Background Important โดยเฉพาะตอน Load Several Un-related Queries จาก Controller Action รัน:
จะ Fire Both Queries Background At Same Time ถ้าแต่ละ Query 200ms Total Time ~200ms แทน 400ms ถ้า Serial
Zeitwerk Mode for Rails 7
Breaking Change สำหรับ Older Apps Still Run Classic Loader All Rails 7 Apps Must Use Zeitwerk Mode Switch Pretty Easy Check Full Zeitwerk Upgrade Guide
Other Rails 7 Updates
Retry Jobs Unlimited Times
ActiveJob Allow Pass :unlimited as attempts Parameter on retry_on Rails จะ Attempt Job 無限次
Named Variants
ตอนนี้ Name Variants บน ActiveStorage แทน Specify Size Every Access
Hash to HTML Attributes
มี Method tag.attributes ใหม่สำหรับ Views Translate Hash into HTML Attributes:
จะ Produce
Ruby debug
New Default Debugging Changed from byebug to debug Gem
แทน Call byebug ตอนนี้ Call debugger ใน Code Enter Debugging Session
Assert Single Record with sole
ตอน Query Records เรียก sole หรือ find_sole_by (แทน first หรือ find_by) เมื่อ Assert Query Should Match Single Record
Check Presence/Absence of an Association
ตอนนี้ใช้ where.associated(:association) Check Association Present on Record แทน Join และ Check Existence of ID
Stream Generated Files from Controller Actions
ตอนนี้ใช้ send_stream ภายใน Controller Action Start Streaming File Being Generated On-the-fly
Provide Immediate (Partial) Response User Know Something Happening Added Benefit Deploy on Heroku
เพราะ File Stream Immediately Heroku Not Terminate Connection หมายความว่าไม่ต้อง Resort Background Jobs Generate One-off Files ที่นานกว่า 30 Seconds
Upgrading to Rails 7
เหมือน Previous Versions Upgrade Simple ไม่มี Official Upgrade Guide Yet Steps เหมือนเดิม:
- Change Rails Version in Gemfile (
7.0.0.rc1ณ Publication Date) Runbundle update - Run
bundle exec rails app:updateFollow Interactive CLI Add/Replace/Modify Files As Required - Run Tests Verify Everything Works Expected
Wrap Up
Full List of Bug Fixes, Features, Changes ใน Rails 7 Release Notes ไม่ Comprehensive ณ ตอนนี้ แต่ Expect Updated Soon
ถ้ายัง Run Rails 6 หรือ Lower โปรดทราบ Final Release of Rails 7, Rails 6.1 Enter "Security Issues Only" Mode No More Bug Fixes นี่ Mark EOL for Rails 5.2 เพราะไม่ได้รับ Fixes อีก
Have Fun Coding!
P.S. หากต้องการอ่านบทความ Ruby Magic ทันทีที่เผยแพร่ สมัคร Newsletter Ruby Magic ของเราและจะไม่พลาดบทความเด็ดๆ ไป!
Pulkit Goyal
ผู้เขียน אורח Pulkit เป็น Senior Full-stack Engineer และ Consultant Free Time เขียนประสบการณ์บนบล็อกตัวเอง
บทความทั้งหมดโดย Pulkit Goyal