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
Slide 14
Slide 14 text
Major features
Slide 15
Slide 15 text
Ruby 2.2.2+
Rails 5 will require that your apps runs on Ruby 2.2.2 or latest.
Slide 16
Slide 16 text
Symbol GC
http://www.sitepoint.com/symbol-gc-ruby-2-2/
https://bugs.ruby-lang.org/issues/9634
Action Cable
DHH talking about Action Cable https://www.youtube.com/watch?v=n0WUjGkDFS0
Slide 22
Slide 22 text
Full Stack
It has both Ruby side as well as JavaScript side.
Slide 23
Slide 23 text
Pub/Sub
Based on PUB/SUB pattern.
http://redis.io/topics/pubsub
Slide 24
Slide 24 text
Seamless integration
with other parts
Works well with other parts of Rails stack like Active Record, Active Job
Slide 25
Slide 25 text
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
Slide 26
Slide 26 text
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
Slide 27
Slide 27 text
websocket-driver
nio4r
concurrent-ruby
These are the 3 dependencies of Action Cable on Ruby side.
Slide 28
Slide 28 text
Adapters
Slide 29
Slide 29 text
Inline
Async
Good for development and test mode.
Slide 30
Slide 30 text
Evented Redis
Non-Evented Redis
Postgresql
For production mode.
Slide 31
Slide 31 text
Rendering a
template
outside of controller
https://github.com/rails/rails/issues/18409
Slide 32
Slide 32 text
ActionController::Renderer.render
Slide 33
Slide 33 text
Basecamp 3
Slide 34
Slide 34 text
No content
Slide 35
Slide 35 text
API only apps
Slide 36
Slide 36 text
What is an API only
app
Slide 37
Slide 37 text
Twitter API
Github API
Supplementary APIs where apps are still traditional web apps
Slide 38
Slide 38 text
Proliferation of client
side frameworks
Slide 39
Slide 39 text
Growing tendency of using client side frameworks
Slide 40
Slide 40 text
Rails as a Back End
Which results into using Rails only as back end app.
Slide 41
Slide 41 text
No HTML, only JSON!
Slide 42
Slide 42 text
Slim version of Rails
Slide 43
Slide 43 text
No view layer
Slide 44
Slide 44 text
Limited set of
middlewares
http://edgeguides.rubyonrails.org/api_app.html#choosing-middleware
Slide 45
Slide 45 text
JBuilder
ActiveModel::Serializer
These can be used with API only apps to generate JSON
Slide 46
Slide 46 text
Still Rails!
(But for API)
http://edgeguides.rubyonrails.org/api_app.html#why-use-rails-for-json-apis-questionmark
Slide 47
Slide 47 text
rails new api_app --api
Create new Rails API only app
Slide 48
Slide 48 text
config.api_only = true
Convert existing app to API only app. You will also have to do some more stuff.
Slide 49
Slide 49 text
development
Things improved in development mode in Rails 5
Slide 50
Slide 50 text
Puma
Puma is default web server now
Slide 51
Slide 51 text
config/puma.rb
Newly created Rails 5 apps will come with a config/puma.rb by default.
Slide 52
Slide 52 text
New Welcome
page
Rails 5 got a new shiny welcome page.
Why some rake and
some rails?
Confusing for newcomers
Slide 59
Slide 59 text
rails db:migrate
In Rails 5, just use `rails`
Slide 60
Slide 60 text
rails server
Slide 61
Slide 61 text
rails assets:precompile
Slide 62
Slide 62 text
One `rails` command
to bind them all!
Slide 63
Slide 63 text
Rake Proxy
`rake` still works using a proxy.
Slide 64
Slide 64 text
rake db:migrate #works
this will still work
Slide 65
Slide 65 text
rails restart
Ability to restart Rails app in development.
https://github.com/rails/rails/issues/18876
Slide 66
Slide 66 text
tmp/restart.txt
Similar to how passenger restarts app by touching tmp/restart.txt
Slide 67
Slide 67 text
Works with Pow,
Puma
Currently works with Puma and Pow only.
Slide 68
Slide 68 text
rails dev:cache
Ability to use caching in development
https://github.com/rails/rails/issues/18875
Slide 69
Slide 69 text
Caching in
development mode
In previous Rails versions, caching does not work automatically in development. now it will work in Rails 5.
Slide 70
Slide 70 text
Toggles caching in
development
Slide 71
Slide 71 text
Also restarts app
Slide 72
Slide 72 text
Filtering routes
https://github.com/rails/rails/issues/18902
Ability to filter routes.
Slide 73
Slide 73 text
Have thousands of
routes?
Slide 74
Slide 74 text
Filtering is easier
now
Slide 75
Slide 75 text
rails routes -c users
Slide 76
Slide 76 text
rails routes -c admin/users
Slide 77
Slide 77 text
rails routes -c PostsController
Slide 78
Slide 78 text
rails routes -g PATCH
Slide 79
Slide 79 text
rails routes -g admin
Slide 80
Slide 80 text
Evented File
system monitor
http://weblog.rubyonrails.org/2015/11/11/snappier-development-mode-in-rails-5/
Slide 81
Slide 81 text
Reloading in
development
Rails reloads all code in development when something changes. For that it has to do a complete walkthrough of all files.
Slide 82
Slide 82 text
Check something has
changed or not
Slide 83
Slide 83 text
config.file_watcher
Now it doesn’t have to do it because of a evented file system monitor.
Slide 84
Slide 84 text
gem ‘listen’
It is powered by listen gem internally.
Slide 85
Slide 85 text
test
Changes in test mode
Slide 86
Slide 86 text
Test Runner
Rails 5 has added Rspec like test runner
http://blog.bigbinary.com/2016/01/03/test-runner-in-rails-5.html
Slide 87
Slide 87 text
bin/rails test
Slide 88
Slide 88 text
$ bin/rails test test/models/user_test.rb:27
Running a single test by line number
Slide 89
Slide 89 text
$ bin/rails test test/models test/jobs
Slide 90
Slide 90 text
Improved failure
message
Slide 91
Slide 91 text
$ bin/rails t
Run options: --seed 51858
# Running:
.F
Failure:
PostsControllerTest#test_should_get_new:
Expected response to be a , but was a <302>
redirect to
bin/rails test test/controllers/posts_controller_test.rb:15
See the command which can be run as test for failed tests
Slide 92
Slide 92 text
Failing fast
Slide 93
Slide 93 text
$ bin/rails t -f
Run options: -f --seed 59599
# Running:
..F
Failure:
PostsControllerTest#test_should_get_new:
Expected response to be a , but was a <302> redirect to
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
Slide 94
Slide 94 text
Backtrace
Slide 95
Slide 95 text
$ bin/rails t -b
Error:
PostsControllerTest#test_should_create_post:
NameError: undefined local variable or method `boom' for #
/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 '
See complete backtrace with -b switch
Slide 96
Slide 96 text
Colored output
Slide 97
Slide 97 text
Can see coloured output now for our tests by default.
Slide 98
Slide 98 text
Controller tests
Changes to controller tests in Rails 5.
Slide 99
Slide 99 text
assigns
assert_template
We used to use them in our tests.
Slide 100
Slide 100 text
assigns
assert_template
Deprecated in Rails 5.
https://github.com/rails/rails/issues/18950
Slide 101
Slide 101 text
Why?
https://github.com/rails/rails/issues/18950
Slide 102
Slide 102 text
gem ‘action-controller_testing’
Use this while upgrading old apps.
Slide 103
Slide 103 text
Controller tests are
integration tests
https://github.com/rails/rails/issues/22496
Slide 104
Slide 104 text
def test_index
get :index
assert_response :success
end
Slide 105
Slide 105 text
def test_index
get posts_url
assert_response :success
end
Example of changed controller test.
https://github.com/rails/rails/issues/22076
Slide 106
Slide 106 text
ActionController::TestCase
In Rails 4.x this was the superclass for all controller tests.
Slide 107
Slide 107 text
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.
Slide 108
Slide 108 text
More focus on
integration testing
From Rails 5 onwards more focus is on doing integration testing.
# 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.
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
Slide 129
Slide 129 text
class Post < ActiveRecord::Base
end
Slide 130
Slide 130 text
class Post < ApplicationRecord
end
in Rails 5
https://github.com/rails/rails/pull/22567
Slide 131
Slide 131 text
#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
Slide 132
Slide 132 text
Post.where('id = 1').or(Post.where('id = 2'))
# => SELECT * FROM posts WHERE (id = 1) OR
(id = 2)
Slide 133
Slide 133 text
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
Slide 134
Slide 134 text
class User < ApplicationRecord
has_secure_token
end
Backward compatibility
Migrations must be backward compatible across major Rails versions
Slide 143
Slide 143 text
But API must change
But migrations API must change in major version too.
Slide 144
Slide 144 text
t.timestamps null: false
For eg. in Rails 4.x
Slide 145
Slide 145 text
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.
Slide 146
Slide 146 text
Compatibility Layer
So a compatibility layer is introduced for older migrations
Slide 147
Slide 147 text
ActiveRecord::Migration
[5.0]
Superclass of migrations in Rails 5. Tells Rails for which version this migration was generated.
Slide 148
Slide 148 text
Active Support
Changes to Active Support
Slide 149
Slide 149 text
Improvements to
Date/Time
http://blog.bigbinary.com/2016/02/17/active-support-improvements-in-Rails-5.html
Slide 150
Slide 150 text
next_day/prev_day
Slide 151
Slide 151 text
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
Slide 152
Slide 152 text
next week with same
time
Slide 153
Slide 153 text
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
Slide 154
Slide 154 text
on_weekend?
Slide 155
Slide 155 text
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
Slide 156
Slide 156 text
on_weekday?
Slide 157
Slide 157 text
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
Slide 158
Slide 158 text
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
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
Slide 169
Slide 169 text
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.
Slide 170
Slide 170 text
Stop entire callback
chain
Slide 171
Slide 171 text
Rails 5 no longer has this
side effect!
No longer stops in Rails 5.
Slide 172
Slide 172 text
throw(:abort)
Explicitly throw(:abort) to stop it.
Slide 173
Slide 173 text
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.
Slide 174
Slide 174 text
ActiveSupport.halt_callback_chains_on_return_false
Once you are ready, toggle this flag and it will use new behaviour of throw.
Slide 175
Slide 175 text
Performance
Some PRs which specifically improved performance in Rails 5.