rails 5 subjective overview

32
RAILS 5 SRUG 28.01.2015 Jan Berdajs @mrbrdo

Upload: jan-berdajs

Post on 18-Jan-2017

555 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Rails 5 subjective overview

RAILS 5SRUG 28.01.2015

Jan Berdajs@mrbrdo

Page 2: Rails 5 subjective overview

RELEASE DATE

• Rails 5.0.0.beta1 was released 18. December 2015

• We don’t know when the final version will come

• Rails 4.0.0.beta1 - 26. February 2013

• Rails 4.0.0.rc1- 1. May 2013 (2 months after beta1)

• Rails 4.0.0 Final - 25. June 2013 (4 months after beta1)

Page 3: Rails 5 subjective overview

RUBY 2.2.2 OR HIGHER

• Symbol garbage collector (no more Symbol DDoS, no more being extra careful)

• Incremental GC

• Module#prepend

• Keyword arguments

Page 4: Rails 5 subjective overview

UPDATES

• Bugfixes only for Rails 4.2.x and 5.x

• Rails 4.1.x and lower will not be supported anymore

Page 5: Rails 5 subjective overview

DEPRECATED CODE REMOVED

• ActionMailer

• deliver removed - use deliver_now or deliver_later

• *_path url helpers removed - use *_url

Page 6: Rails 5 subjective overview

DEPRECATED CODE REMOVED

• ActiveRecord

• protected_attributes completely unsupported now (the old attr_accessible)

• activerecord-deprecated_finders completely unsupported, but who was still using that anyway (find(:all, conditions: { … }))

Page 7: Rails 5 subjective overview

DEPRECATED CODE REMOVED

• ActionPack

• some test helpers for testing controllers were moved into a gem rails-controller-testing

• assert_template

• assigns

Page 8: Rails 5 subjective overview

AND STUFF

• template.html is no longer parsed by ERB (must have .erb extension)

Page 9: Rails 5 subjective overview

BORING CHANGESThat most of us won’t care about

Page 10: Rails 5 subjective overview

RAILS COMMAND• You can now call rake tasks via the rails command

• e.g. rails db:migrate

• Who cares

• Maybe easier for big noobs

• Don’t like too much

Page 11: Rails 5 subjective overview

TURBOLINKS 2

Good news:

Rails 5 will not include Turbolinks 2!

Page 12: Rails 5 subjective overview

TURBOLINKS 5

Bad news:

Rails 5 will include Turbolinks 5.

Page 13: Rails 5 subjective overview

TURBOLINKS

• If you are already using Rails 5 (master), you might still be using Turbolinks 2

Page 14: Rails 5 subjective overview

TURBOLINKS 5• It has native iOS and Android wrapper implementations. Supposedly so

if you use a WebView in a native app you can use Turbolinks.

• So basically useful for hybrid native apps.

• It also implements partial updates and uses some HTML5 (data-*) attributes to make things easier.

• Who cares? Probably only Basecamp.

• I don’t know why this is part of Rails.Probably because Basecamp.

Page 15: Rails 5 subjective overview

MINITEST RUNNER• Basically add a bunch of features to minitest runner command, that rspec already has

since like forever.

• Run a single test file, or specify line. Had for years in rspec.

• Run multiple test files. Had for years in rspec.

• Color output. Had for years in rspec.

• Fail fast. Had for years in rspec.

• Etc…

• Do I care? No… At least Rails core development will be easier. But they could just switch to rspec.

Page 16: Rails 5 subjective overview

MYSQL

• Added support for JSON datatype.

Page 17: Rails 5 subjective overview

MYSQL

• Added support for JSON datatype.

Page 18: Rails 5 subjective overview

HAS_SECURE_TOKEN

• good idea but not that great implementation (unreliable)

• it uses SecureRandom with Base58 to generate a 24-char token.

class Invite < ActiveRecord::Base has_secure_token :invitation_codeend

invite = Invite.newinvite.saveinvite.invitation_code # => 44539a6a59835a4ee9d7b112invite.regenerate_invitation_code # => true

Page 19: Rails 5 subjective overview

MORE EXCITING CHANGESThat I would say are cool

Page 20: Rails 5 subjective overview

BETTER PERFORMANCE

• Lots of work done here, e.g. freezing strings

• Development mode now uses evented FS monitor, which is much faster than checking all files if they were updated, as it worked in Rails 4

Page 21: Rails 5 subjective overview

RAILS-API

• rails new name --api

• active_model_serializers

• more or less works just like rails-api worked

• it’s nice that the core rails team will be working on this

Page 22: Rails 5 subjective overview

ACTIONCABLE• It’s basically like Heroku pusher - websockets, push server…

• Uses parts of faye-websocket and concurrent-ruby (threading lib)

• Similar to faye, you subscribe to channels and then receive events in JS when new messages arrive.

• It might not sound that useful but it actually has a lot of use cases

Page 23: Rails 5 subjective overview

ACTIONCONTROLLER::RENDERER

• Refactor of ActionController

• It makes it much easier to render templates outside of controllers (useful in delayed jobs and for ActionCable)

• ApplicationController.render ‘sample/index'

• ApplicationController.render assigns: { rails: 'Rails' }, inline: '<%= "Hello

#{@rails}" %>’

• Makes this much easier when you need it. Before we had to do crazy stuff with AbstractController ::Base

Page 24: Rails 5 subjective overview

ACTIVERECORD ATTRIBUTES

• Basically allows you to define coercion rules for attributes.

• It also works for virtual attributes.

• It’s not that big of a deal, we could just define a getter and setter method to achieve the same thing.

• But it is nice and may play with the framework better than just defining attribute accessor methods.

Page 25: Rails 5 subjective overview

ACTIVERECORD ATTRIBUTES

class Book < ActiveRecord::Baseend

book.quantity # => 12.0

class Book < ActiveRecord::Base attribute :quantity, :integerend

book.quantity # => 12

class Product < ApplicationRecord attribute :price_in_cents, MoneyType.newend

class MoneyType < ActiveRecord::Type::Integer def type_cast(value) # convert values like '$10.00' to 1000 endend

product = Product.new(price_in_cents: '$10.00')product.price_in_cents #=> 1000

Page 26: Rails 5 subjective overview

APPLICATIONRECORD

• Now controllers inherit from ApplicationRecord instead of ActiveRecord::Base

• Basically just so ActiveRecord::Base monkey-patching won’t have to be done, instead you do that in ApplicationRecord now, so if some gems use ActiveRecord::Base they won’t be affected by the monkey-patches.

Page 27: Rails 5 subjective overview

ACTIVERECORD OR

• Finally we can use “or” in ActiveRecord

• Unfortunately the API is a bit stupid imo

• I prefer how it’s done in Sequel

Book.where('status = 1').or(Book.where('status = 3'))

Book.where(Sequel.or(status: 1, name: "Book"))Book.where { |t| { t.status => 1 } | { t.name => "Book" } }

Page 28: Rails 5 subjective overview

ACTIVERECORD IN_BATCHES

Person.where('age >= 18').in_batches(of: 1000) do |people| people.update_all(can_vote: true)end

Page 29: Rails 5 subjective overview

BELONGS_TO VALIDATION

• By default, when you have a belongs_to association you will automatically get a presence validation for it

• You can explicitly make it optional:class Book < ActiveRecord::Base belongs_to :author, optional: trueend

Page 30: Rails 5 subjective overview

ACTIVERECORD HOOKS

• Until Rails 4, if you return false from a before_* hook, it would halt the whole operation (e.g. save)

• In Rails 5, this is no longer the case.

• You now have to use

• Oh my god THANK YOU

throw(:abort)

Page 31: Rails 5 subjective overview

CONCLUSION• There are some new things, and not a lot of changes to existing APIs, so

upgrading shouldn’t be too hard. Also use rails rails:update.

• The belongs_to validation is one of the things you’ll have to be careful about.

• We have better tools to create API projects, and if you’re not doing an API project, you have some new Turbolinks features to make everything snappier.

• Similar release to Rails 4, there are not many breaking changes, and it’s a good thing. No need to fix something that’s not broken.

Page 32: Rails 5 subjective overview

THANKS