O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26

289 visualizações

Publicada em

Speech of Nihad Abbasov, Backend Engineer at Digital Classifieds, at Ruby Meditation #26 Kyiv 16.02.2019
Next conference - http://www.rubymeditation.com/

What to expect from Rails 6? Review of new features.

Announcements and conference materials https://www.fb.me/RubyMeditation
News https://twitter.com/RubyMeditation
Photos https://www.instagram.com/RubyMeditation
The stream of Ruby conferences (not just ours) https://t.me/RubyMeditation

Publicada em: Tecnologia
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26

  1. 1. New features in Rails 6 KIEV, UKRAINE, FEB 16 2019 Nihad Abbasov Digital Classifieds @NARKOZ
  2. 2. Agenda Rails 6 01 Major Features Notable changes Q&A 02 03 04
  3. 3. We're intending to release Rails 6.0 by @RailsConf 2019. Here's the full "aspirational timeline" 😄. DHH
  4. 4. “ASPIRATIONAL TIMELINE” https://weblog.rubyonrails.org/2018/12/20/timeline-for-the-release-of-Rails-6-0/ ● January 15: Beta 1 ● February 1: Beta 2 ● March 1: Release Candidate 1 ● April 1: Release Candidate 2 ● April 30: Final release
  5. 5. Ruby 2.5+ REQUIREMENTS
  6. 6. GETTING STARTED $ gem install rails --pre $ rails new myapp Uses the latest beta pushed to rubygems
  7. 7. GETTING STARTED $ git clone https://github.com/rails/rails.git $ rails/railties/exe/rails new myapp Uses edge from github repository
  8. 8. UPGRADING $ rails app:update
  9. 9. MAJOR FEATURES + Action Mailbox + Action Text + Parallel Testing + Webpacker as the default + Multiple databases
  10. 10. Action Mailbox FEATURE #1
  11. 11. Receive emails in app
  12. 12. DEPRECATED ActionMailer::Base.receive
  13. 13. SUPPORTED INGRESSES ● Amazon SES ● Exim ● Mailgun ● Mandrill ● Postfix ● Postmark ● Qmail ● SendGrid
  14. 14. SETUP $ rails action_mailbox:install $ rails db:migrate Configure your SMTP server to pipe inbound emails. config.action_mailbox.ingress = :relay
  15. 15. USE CASES
  16. 16. Code
  17. 17. app/mailboxes/application_mailbox.rb class ApplicationMailbox < ActionMailbox::Base routing "post@example.com" => :posts routing /Areply-[0-9a-f]+z/i => :comments end Route can be a string, regex, lambda/proc, object responding to #match?, :all
  18. 18. app/mailboxes/posts_mailbox.rb class PostsMailbox < ApplicationMailbox before_processing :ensure_sender_is_a_user private def ensure_sender_is_a_user unless User.exist?(email: mail.from) bounce_with UserMailer.missing(inbound_email) end end end
  19. 19. app/mailboxes/posts_mailbox.rb def process User.find_by(email: mail.from) .posts.create!(post_attrs) end private def post_attrs { title: mail.subject || Time.current.to_s, body: EmailParser.parse(mail.source) } end
  20. 20. { InboundEmail statuses incoming lifecycle pending processing delivered failed bounced
  21. 21. Action Text FEATURE #2
  22. 22. Rich text editor
  23. 23. NO RAW HTML EDITING MODE https://github.com/basecamp/trix/issues/42#issuecomment-179905641
  24. 24. Code
  25. 25. SETUP $ rails action_text:install $ rails db:migrate Creates action text table, installs trix npm package
  26. 26. app/models/article.rb class Article < ApplicationRecord has_rich_text :content end <%= form_with(model: @article) do |form| %> <%= form.rich_text_area :content %> <%= form.submit %> <% end %> app/views/articles/_form.html.erb
  27. 27. OPT-OUT --skip-action-text
  28. 28. Parallel Testing FEATURE #3
  29. 29. Enabled by default
  30. 30. test/test_helper.rb class ActiveSupport::TestCase parallelize(workers: 4, with: :processes) end Uses processes. Can be changed to use threads.
  31. 31. Run test suite $ PARALLEL_WORKERS=6 bin/rails test Overrides number of workers defined in test_helper.rb
  32. 32. Webpacker as the default FEATURE #4
  33. 33. Only for JavaScript
  34. 34. Multi databases FEATURE #5
  35. 35. Automatic DB switching
  36. 36. Request is sent to the replica ● The request is a read request (GET or HEAD) ● AND It's been 5 seconds since the last write to the database
  37. 37. Request is sent to the primary ● It's not a GET/HEAD request (ie is a POST, PATCH, etc) ● Has been less than 5 seconds since the last write to the database
  38. 38. config/environments/production.rb config.active_record.database_selector = { delay: 10.seconds } Uses ActiveRecord::Middleware::DatabaseSelector::Resolver
  39. 39. Database connection switching API Connects a model to the databases specified: # in your model: connects_to database: { writing: :primary, reading: :primary_replica } database keyword takes a hash consisting of a role and a database_key
  40. 40. Notable changes
  41. 41. Image Processing Active Storage
  42. 42. { RESIZE AND PROCESS IMAGES image_processing ImageMagick GraphicsMagick libvips https://github.com/janko/image_processing
  43. 43. fast image processing with low memory needs LIBVIPS
  44. 44. BENCHMARKS https://github.com/libvips/libvips/wiki/Speed-and-memory-use
  45. 45. config/application.rb config.active_storage.variant_processor = :vips Use libvips for processing ActiveStorage variants
  46. 46. ALLOCATIONS COUNT
  47. 47. Enumerable#index_with post = Post.new( title: "hey there", body: "what's up?" ) %i[title body].index_with do |a| post.public_send(a) end # => { title: "hey there", body: "what's up?" } Converts an enumerable to a hash: keys are enumerable items with the values returned in the block
  48. 48. ActiveRecord::Base.create_or_find_by User.create_or_find_by!( username: "john", email: "john@example.com" ) Similar to #find_or_create_by method: Deals with race conditions by leaning on unique DB constraints
  49. 49. Command for changing databases $ rails db:system:change --to=postgresql $ rails db:system:change --to=sqlite3 Modifies config/database.yml and Gemfile
  50. 50. `before?` & `after?` methods in date & time classes date1.after? date2 date1.before? date2 # same as: date1 > date2 date1 < date2 For comparing dates and times: Makes date and time comparisons more human readable
  51. 51. Endless ranges in where relation.where( column: 1..Float::INFINITY ) # is equivalent to: relation.where(column: (1..)) Support for endless ranges introduced in Ruby 2.6
  52. 52. New uploaded files stored on save @user.avatar = params[:avatar] @user.save The uploaded file is stored when @user is successfully saved
  53. 53. EXPANDED ROUTES
  54. 54. The End. Questions? Thanks!

×