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

Rails 5 - What's in it for me?

Prathamesh Sonpatki
April 06, 2016
110

Rails 5 - What's in it for me?

Prathamesh Sonpatki

April 06, 2016
Tweet

Transcript

  1. RC 1 coming soon Pending issues for RC 1 -

    https://github.com/rails/rails/milestones/5.0.0
  2. These are the features proposed by DHH for Rails 5.

    DHH is creator of Ruby on Rails and he is still passionately involved in Rails development after more than 10 years. Complete list - https://github.com/rails/rails/issues?q=milestone%3A5.0.0+author%3Adhh+is%3Aclosed
  3. Seamless integration with other parts Works well with other parts

    of Rails stack like Active Record, Active Job
  4. module ApplicationCable class Connection < ActionCable::Connection::Base identified_by :current_user def connect

    self.current_user = find_verified_user end protected def find_verified_user id = cookies.signed[:user_id] if current_user = User.find_by(id: id) current_user else reject_unauthorized_connection end end end end An example showing how you can use Active Record, cookies etc with Action Cable
  5. Ruby side Initially it was started with hard dependency on

    event machine. It was later dropped in https://github.com/rails/rails/commit/74497eabd52f2f9f8c383808b11286283046c2b2. Now Action Cable does not have hard dependency on Event machine
  6. config.api_only = true Convert existing app to API only app.

    You will also have to do some more stuff.
  7. Caching in development mode In previous Rails versions, caching does

    not work automatically in development. now it will work in Rails 5.
  8. Reloading in development Rails reloads all code in development when

    something changes. For that it has to do a complete walkthrough of all files.
  9. Test Runner Rails 5 has added Rspec like test runner

    http://blog.bigbinary.com/2016/01/03/test-runner-in-rails-5.html
  10. $ bin/rails t Run options: --seed 51858 # Running: .F

    Failure: PostsControllerTest#test_should_get_new: Expected response to be a <success>, but was a <302> redirect to <http://test.host/posts> bin/rails test test/controllers/posts_controller_test.rb:15 See the command which can be run as test for failed tests
  11. $ bin/rails t -f Run options: -f --seed 59599 #

    Running: ..F Failure: PostsControllerTest#test_should_get_new: Expected response to be a <success>, but was a <302> redirect to <http://test.host/posts> bin/rails test test/controllers/posts_controller_test.rb:15 Interrupted. Exiting... Finished in 0.179125s, 16.7481 runs/s, 22.3308 assertions/s. 3 runs, 4 assertions, 1 failures, 0 errors, 0 skips Fail fast with -f switch
  12. $ bin/rails t -b Error: PostsControllerTest#test_should_create_post: NameError: undefined local variable

    or method `boom' for #<PostsController: 0x007fc53c4eb868> /rails-5-test-runner-app/app/controllers/posts_controller.rb:29:in `create' /sources/rails/actionpack/lib/action_controller/metal/ basic_implicit_render.rb:4:in `send_action' /sources/rails/actionpack/lib/abstract_controller/base.rb:183:in `process_action' /sources/rails/actionpack/lib/action_controller/metal/rendering.rb:30:in `process_action' /sources/rails/actionpack/lib/abstract_controller/callbacks.rb:20:in `block in process_action' /sources/rails/activesupport/lib/active_support/callbacks.rb:126:in `call' ..... /sources/rails/activesupport/lib/active_support/testing/assertions.rb: 71:in `assert_difference' /rails-5-test-runner-app/test/controllers/posts_controller_test.rb:19:in `block in <class:PostsControllerTest>' See complete backtrace with -b switch
  13. def test_index get posts_url assert_response :success end Example of changed

    controller test. https://github.com/rails/rails/issues/22076
  14. ActionDispatch::IntegrationTest Now it is this one. Also a lot of

    effort has gone into making these tests faster. Action Controller tests may be deprecated and turned into a gem in Rails 5.1.
  15. More focus on integration testing From Rails 5 onwards more

    focus is on doing integration testing.
  16. Use it with caution! It will literally set the cache

    control headers for forever. Make sure that you are using it with static content only.
  17. No record is deleted. No record is added. This result

    does not change unless something is added or deleted.
  18. # index.html.erb <%= render partial: 'todo', collection: @todos %> #

    _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  19. # index.html.erb <%= render partial: 'todo', collection: @todos, cached: true

    %> # _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %> Just pass `cached: true` and Rails will fetch all caches at once.
  20. ApplicationRecord ApplicationRecord is a new superclass for all app models,

    analogous to app controllers subclassing ApplicationController instead of ActionController::Base. This gives apps a single spot to configure app-wide model behavior
  21. #or Added the #or method on ActiveRecord::Relation, allowing use of

    the OR operator to combine WHERE or HAVING clauses https://github.com/rails/rails/commit/b0b37942d729b6bdcd2e3178eda7fa1de203b3d0
  22. has_secure_token Added ActiveRecord::SecureToken in order to encapsulate generation of unique

    tokens for attributes in a model using SecureRandom https://github.com/rails/rails/pull/18217
  23. >> user = User.first => <User id: 11, name: 'John',

    email: '[email protected]', token: "jRMcN645BQyDr67yHR3qjsJF", token: "qjCbex522DfVEVd5ysUWppWQ"> >> user.token => "qjCbex522DfVEVd5ysUWppWQ" >> user.regenerate_token => true >> user.token => "tYYVjnCEd1LAXvmLCyyQFzbm"
  24. t.timestamps # Rails 5 But in Rails 5, there is

    no `null: false` still it adds `NULL` constraint when the migration is run in Rails 5.
  25. Time.current => Fri, 12 Feb 2016 08:53:31 UTC +00:00 Time.current.next_day

    => Sat, 13 Feb 2016 08:53:31 UTC +00:00 Time.current.prev_day => Thu, 11 Feb 2016 08:53:31 UTC +00:00
  26. Time.current => Fri, 12 Feb 2016 09:15:10 UTC +00:00 Time.current.next_week

    => Mon, 15 Feb 2016 00:00:00 UTC +00:00 Time.current.next_week(same_time: true) => Mon, 15 Feb 2016 09:15:20 UTC +00:00
  27. Time.current => Fri, 12 Feb 2016 09:47:40 UTC +00:00 Time.current.on_weekend?

    => false Time.current.tomorrow => Sat, 13 Feb 2016 09:48:47 UTC +00:00
  28. Time.current => Fri, 12 Feb 2016 09:47:40 UTC +00:00 Time.current.on_weekday?

    => true Time.current.tomorrow => Sat, 13 Feb 2016 09:48:47 UTC +00:00
  29. Time.current => Fri, 12 Feb 2016 09:47:40 UTC +00:00 Time.current.next_weekday

    => Mon, 15 Feb 2016 09:55:14 UTC +00:00 Time.current.prev_weekday => Thu, 11 Feb 2016 09:55:33 UTC +00:00
  30. users = [{id: 1, name: 'Max'}, {id: 2, name: 'Mark'},

    {id: 3, name: 'Jen'}] users.pluck(:name) => ["Max", "Mark", "Jen"]
  31. users = [{id: 1, name: 'Max'}, {id: 2, name: 'Mark'},

    {id: 3, name: 'Jen'}] users.pluck(:id, :name) => [[1, "Max"], [2, "Mark"], [3, "Jen"]]
  32. # In Rails 4.x users = User.all SELECT `users`.* FROM

    `users` users.pluck(:id, :name) # SELECT "users"."id", "users"."name" FROM “users" => [[1, "Max"], [2, "Mark"], [3, "George"]]
  33. # In Rails 5 users = User.all SELECT `users`.* FROM

    `users` users.pluck(:id, :name) => [[1, "Max"], [2, "Mark"], [3, "George"]]
  34. vehicles = {car: 'Hyundai', bike: 'Honda', bus: 'Mercedes', truck: 'Tata'}

    vehicles.without(:bike, :bus) => {:car=>"Hyundai", :truck=>"Tata"}
  35. Halting callback chain Changed the way in which callback chains

    can be halted. The preferred method to halt a callback chain from now on is to explicitly throw(:abort). https://github.com/rails/rails/pull/17227
  36. return false from one of the `before` callbacks In Rails

    4, if we return false from any of the before_ filters, the entire callback chain used to stop.
  37. While upgrading, it will still halt For older apps getting

    upgraded to Rails 5, the callback will still halt with returning false. as this is a breaking change.
  38. rubyonrails.org/doctrine/ If you are new to Rails, check out the

    Rails Doctrine to understand philosophy behind Rails.