Slide 1

Slide 1 text

Rails 5 What’s in it for me? @_cha1tanya @BigBinary @Srijan

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Overview of Rails 5

Slide 4

Slide 4 text

Rails.5.started_on? Nov, 2014

Slide 5

Slide 5 text

https://github.com/rails/rails/commit/f25ad07f5ade46eb978fa8265

Slide 6

Slide 6 text

9359+ Commits

Slide 7

Slide 7 text

Beta 3 http://weblog.rubyonrails.org/2016/2/27/Rails-5-0-beta3/ http://weblog.rubyonrails.org/2016/2/2/Rails-5-0-beta2/ http://weblog.rubyonrails.org/2015/12/18/Rails-5-0-beta1/

Slide 8

Slide 8 text

RC 1 coming soon Pending issues for RC 1 - https://github.com/rails/rails/milestones/5.0.0

Slide 9

Slide 9 text

Lots of new features

Slide 10

Slide 10 text

Bug fixes

Slide 11

Slide 11 text

Improvements

Slide 12

Slide 12 text

DHH Features

Slide 13

Slide 13 text

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

Slide 17

Slide 17 text

Incremental GC https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/

Slide 18

Slide 18 text

Chance to cleanup Rails internals

Slide 19

Slide 19 text

Keyword arguments Rails code can use keyword arguments internally as now it only supports Ruby 2.2.2 +

Slide 20

Slide 20 text

Module#prepend http://www.justinweiss.com/articles/rails-5-module-number-prepend-and-the-end-of-alias-method-chain/

Slide 21

Slide 21 text

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.

Slide 53

Slide 53 text

In Rails 4.x

Slide 54

Slide 54 text

In Rails 5 :)

Slide 55

Slide 55 text

`rails` CLI https://github.com/rails/rails/issues/18878

Slide 56

Slide 56 text

rake db:migrate Sometimes rake

Slide 57

Slide 57 text

rails server sometimes rails?

Slide 58

Slide 58 text

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.

Slide 109

Slide 109 text

caching changes to caching in Rails 5

Slide 110

Slide 110 text

http_cache_forever https://github.com/rails/rails/issues/18192

Slide 111

Slide 111 text

def index http_cache_forever do render ‘index’ end end https://github.com/rails/rails/pull/18394

Slide 112

Slide 112 text

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.

Slide 113

Slide 113 text

Collection Caching

Slide 114

Slide 114 text

@users = User.where(city: 'Pune')

Slide 115

Slide 115 text

No record is deleted. No record is added. This result does not change unless something is added or deleted.

Slide 116

Slide 116 text

Caching of collections Rails 5 allows caching of collections

Slide 117

Slide 117 text

ActiveRecord::Relation#cache_key https://github.com/rails/rails/pull/20884

Slide 118

Slide 118 text

@users.cache_key => "users/query-67ed32b36805123"

Slide 119

Slide 119 text

cache(@users) do ……… end

Slide 120

Slide 120 text

Limitations Doesn’t work well with limits and groups.

Slide 121

Slide 121 text

Partials caching https://github.com/rails/rails/issues/18900

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

multi_fetch_fragments gem https://github.com/rails/rails/pull/18948

Slide 124

Slide 124 text

Folded in Rails 5

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

Caching Action Mailer views https://github.com/rails/rails/pull/22825

Slide 127

Slide 127 text

Active Record Changes to Active Record in Rails 5

Slide 128

Slide 128 text

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

Slide 135

Slide 135 text

>> user = User.new >> user.save => true >> user.token => 'qjCbex522DfVEVd5ysUWppWQ'

Slide 136

Slide 136 text

URL safe tokes are url safe

Slide 137

Slide 137 text

Fixed length strings and fixed length

Slide 138

Slide 138 text

Base 58 encoding

Slide 139

Slide 139 text

Regenerating also possible

Slide 140

Slide 140 text

>> user = User.first => >> user.token => "qjCbex522DfVEVd5ysUWppWQ" >> user.regenerate_token => true >> user.token => "tYYVjnCEd1LAXvmLCyyQFzbm"

Slide 141

Slide 141 text

Versioned migrations http://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html

Slide 142

Slide 142 text

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

Slide 159

Slide 159 text

Enumerable#pluck

Slide 160

Slide 160 text

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

Slide 161

Slide 161 text

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

Slide 162

Slide 162 text

Improvement to Active Record’s pluck It also makes Active Record’s pluck perform better.

Slide 163

Slide 163 text

# 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"]]

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

Enumerable#without

Slide 166

Slide 166 text

vehicles = ['Car', 'Bike', 'Truck', 'Bus'] vehicles.without("Car", "Bike") => ["Truck", "Bus"]

Slide 167

Slide 167 text

vehicles = {car: 'Hyundai', bike: 'Honda', bus: 'Mercedes', truck: 'Tata'} vehicles.without(:bike, :bus) => {:car=>"Hyundai", :truck=>"Tata"}

Slide 168

Slide 168 text

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.

Slide 176

Slide 176 text

No content

Slide 177

Slide 177 text

No content

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

No content

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

https://github.com/rails/rails/pull/21057 https://github.com/rails/rails/pull/18643 https://github.com/rails/rails/pull/21411 https://github.com/rails/rails/pull/21155 https://github.com/rails/rails/pull/20946

Slide 182

Slide 182 text

And more… There is lot more than this in Rails 5.

Slide 183

Slide 183 text

Attributes API https://speakerdeck.com/sgrif/designing-a-great-ruby-api-how-were-simplifying-rails-5

Slide 184

Slide 184 text

Sprockets 3/4 https://github.com/rails/sprockets/blob/3.x/UPGRADING.md

Slide 185

Slide 185 text

Turbolinks 5 https://github.com/turbolinks/turbolinks

Slide 186

Slide 186 text

What’s next? Where to go from here?

Slide 187

Slide 187 text

rubyonrails.org/doctrine/ If you are new to Rails, check out the Rails Doctrine to understand philosophy behind Rails.

Slide 188

Slide 188 text

5.0.0.RC1 If you have an existing app, try upgrading Rails 5.0.0RC1 when it is out.

Slide 189

Slide 189 text

weblog.rubyonrails.org Checkout Rails blog for new release announcements

Slide 190

Slide 190 text

@rails Rails twitter handle

Slide 191

Slide 191 text

github.com/rails/rails Daily news of what happens in Rails world. Subscribe to issues to get more details.

Slide 192

Slide 192 text

This week in Rails Rails newsletter.

Slide 193

Slide 193 text

rails-weekly.goodbits.io Weekly scoop of things happened in Rails world.

Slide 194

Slide 194 text

blog.bigbinary.com/categories/ Rails%205 Our own blog series about new features in Rails 5.

Slide 195

Slide 195 text

Try RC

Slide 196

Slide 196 text

Report bugs

Slide 197

Slide 197 text

Fix bugs!

Slide 198

Slide 198 text

Let’s make Rails better

Slide 199

Slide 199 text

Thank you! @_cha1tanya @BigBinary @Srijan