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

What's new in Rails 4 DRUG presentation

What's new in Rails 4 DRUG presentation

A presentation given at the Downtown Ruby Users Group in SLC on 4/10/2013.

Avatar for Zach Pearce

Zach Pearce

April 11, 2013

Other Decks in Programming

Transcript

  1. STRONG PARAMETERS Moves decisions about model attribute security from the

    model to the controller, where they belong. Gets rid of attr_accessible and attr_protected Wednesday, April 10, 13
  2. class Person < ActiveRecord::Base attr_accessible :name, :user_id, :tags, :properties end

    class PeopleController < ApplicationController def create @person = Person.new(params[:person]) if @person.save redirect_to @person, notice: 'Person was successfully created.' else render action: 'new' end end end Rails 3 Wednesday, April 10, 13
  3. class Person < ActiveRecord::Base end class PeopleController < ApplicationController def

    create @person = Person.new(person_params) if @person.save format.html { redirect_to @person, notice: 'Person successfully created.' } else format.html { render action: 'new' } end end private def person_params params.require(:person).permit(:name, :user_id, :tags, :properties) end end Rails 4 Wednesday, April 10, 13
  4. A new feature, enabled by default, that speeds up the

    loading of pages Uses javascript to reload just the body of a page instead of doing a full reload Watch out for js that executes on page load. Run that code by listening for the turbolinks “page:load” event. TURBOLINKS Wednesday, April 10, 13
  5. AKA Fragment Cache Digests Meant to help automatically expire caches

    for nested fragment cache calls Automatically adds a digest value to the cache key for the fragment based on the contents of the template and its dependencies (almost automatically) RUSSIAN DOLL CACHING Wednesday, April 10, 13
  6. # app/views/companies/_company.html.erb <% cache company do %> Company name: <%=

    company.name %> <%= render company.employees %> <% end %> # app/views/employees/_employee.html.erb <% cache employee do %> Employee name: <% employee.name %> <% end %> Rails 3 - bad Wednesday, April 10, 13
  7. # app/views/companies/_company.html.erb <% cache [‘v1’, company] do %> Company name:

    <%= company.name %> <%= render company.employees %> <% end %> # app/views/employees/_employee.html.erb <% cache [‘v1’, employee] do %> Employee name: <% employee.name %> <% end %> Rails 3 - better Wednesday, April 10, 13
  8. Rails 4 # app/views/companies/_company.html.erb <% cache company do %> Company

    name: <%= company.name %> <%= render company.employees %> <% end %> # app/views/employees/_employee.html.erb <% cache employee do %> Employee name: <% employee.name %> <% end %> $ rake cache_digests:nested_dependencies TEMPLATE=companies/index [ { "companies/company": [ "employees/employee" ] } ] Wednesday, April 10, 13
  9. Include ActionController::Live in your controller to enable streaming. Use a

    server that does not buffer output and can handle multiple connections (i.e. in development don’t use WEBrick - use thin or puma) Write to response.stream to send data immediately to the browser Close the stream when done ACTIONCONTROLLER::LIVE Wednesday, April 10, 13
  10. class MyController < ActionController::Base include ActionController::Live def index 100.times {

    response.stream.write "hello world\n" } response.stream.close end end ActionController::Live Wednesday, April 10, 13
  11. Dynamic finders (kind of) replaced by find_by .order takes a

    hash as well as a string now .not negates a where clause .none returns an empty Relation that doesn’t hit the database .load causes the query to be executed, but returns a Relation .all returns a Relation instead of an Array (you can still use to_a) .all takes the place of the now deprecated .scoped ACTIVERECORD CHANGES Wednesday, April 10, 13
  12. POSTGRESQL ARRAYS AND HSTORE Hstore and array data types are

    now supported natively Use hstores to store hashes. Keys and values are always stored as strings and must be type cast on the way out of the db The value of using these data types vs. just using a serialized column is in Postgres’ querying capabilities Use config.active_record.schema_format = :sql Wednesday, April 10, 13
  13. MODEL CHANGES You can now include one module, ActiveModel::Model, in

    a class to create a non- database-backed model (validations, callbacks, attributes, etc) Scopes always need to have a callable (lambda, proc, etc) as their second argument Wednesday, April 10, 13
  14. VIEW CHANGES New form helpers: collection_check_boxes and collection_radio_buttons help with

    m2m associations and arrays New date/time form helpers: e.g. date_field instead of date_select. Produces <input type=”date”> Ruby template handler Wednesday, April 10, 13
  15. CONTROLLER CHANGES *_filter is now *_action (e.g. before_action instead of

    before_filter) PATCH is now the primary verb that maps to update actions (PUT still routes to update) Wednesday, April 10, 13
  16. ROUTING CHANGES You can now use concern blocks to reduce

    duplication in routes The match method is deprecated. Use get/post/put/ patch/delete instead Constraints turn into default attributes for url helpers Wednesday, April 10, 13