Rails 5 - What's in it for me?

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
April 06, 2016
86

Rails 5 - What's in it for me?

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

April 06, 2016
Tweet

Transcript

  1. 2.
  2. 8.

    RC 1 coming soon Pending issues for RC 1 -

    https://github.com/rails/rails/milestones/5.0.0
  3. 13.

    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
  4. 24.

    Seamless integration with other parts Works well with other parts

    of Rails stack like Active Record, Active Job
  5. 25.

    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
  6. 26.

    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
  7. 28.
  8. 34.
  9. 48.

    config.api_only = true Convert existing app to API only app.

    You will also have to do some more stuff.
  10. 69.

    Caching in development mode In previous Rails versions, caching does

    not work automatically in development. now it will work in Rails 5.
  11. 81.

    Reloading in development Rails reloads all code in development when

    something changes. For that it has to do a complete walkthrough of all files.
  12. 86.

    Test Runner Rails 5 has added Rspec like test runner

    http://blog.bigbinary.com/2016/01/03/test-runner-in-rails-5.html
  13. 91.

    $ 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
  14. 93.

    $ 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
  15. 94.
  16. 95.

    $ 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
  17. 105.

    def test_index get posts_url assert_response :success end Example of changed

    controller test. https://github.com/rails/rails/issues/22076
  18. 107.

    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.
  19. 108.

    More focus on integration testing From Rails 5 onwards more

    focus is on doing integration testing.
  20. 112.

    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.
  21. 115.

    No record is deleted. No record is added. This result

    does not change unless something is added or deleted.
  22. 122.

    # index.html.erb <%= render partial: 'todo', collection: @todos %> #

    _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  23. 125.

    # 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.
  24. 128.

    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
  25. 131.

    #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
  26. 133.

    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
  27. 135.
  28. 140.

    >> user = User.first => <User id: 11, name: 'John',

    email: 'john@example.com', token: "jRMcN645BQyDr67yHR3qjsJF", token: "qjCbex522DfVEVd5ysUWppWQ"> >> user.token => "qjCbex522DfVEVd5ysUWppWQ" >> user.regenerate_token => true >> user.token => "tYYVjnCEd1LAXvmLCyyQFzbm"
  29. 145.

    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.
  30. 147.
  31. 151.

    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
  32. 153.

    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
  33. 155.

    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
  34. 157.

    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
  35. 158.

    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
  36. 160.

    users = [{id: 1, name: 'Max'}, {id: 2, name: 'Mark'},

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

    users = [{id: 1, name: 'Max'}, {id: 2, name: 'Mark'},

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

    # 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"]]
  39. 164.

    # In Rails 5 users = User.all SELECT `users`.* FROM

    `users` users.pluck(:id, :name) => [[1, "Max"], [2, "Mark"], [3, "George"]]
  40. 167.

    vehicles = {car: 'Hyundai', bike: 'Honda', bus: 'Mercedes', truck: 'Tata'}

    vehicles.without(:bike, :bus) => {:car=>"Hyundai", :truck=>"Tata"}
  41. 168.

    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
  42. 169.

    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.
  43. 173.

    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.
  44. 176.
  45. 177.
  46. 178.
  47. 179.
  48. 180.
  49. 187.

    rubyonrails.org/doctrine/ If you are new to Rails, check out the

    Rails Doctrine to understand philosophy behind Rails.
  50. 195.
  51. 197.