Features of Rails 5 you have not heard about

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
June 03, 2016
81

Features of Rails 5 you have not heard about

Talk given at Ruby Nation 2016

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

June 03, 2016
Tweet

Transcript

  1. 1.
  2. 4.
  3. 7.
  4. 9.
  5. 11.
  6. 13.

    5

  7. 14.

    5

  8. 15.

    5

  9. 16.

    5

  10. 17.
  11. 19.
  12. 38.

    class Order < ActiveRecord::Base before_save :set_eligibility_for_rebate before_save :ensure_credit_card_is_on_file def set_eligibility_for_rebate

    self.eligibility_for_rebate = decide_eligibility end def ensure_credit_card_is_on_file puts "check if credit card is on file" end end
  13. 39.

    class Order < ActiveRecord::Base before_save :set_eligibility_for_rebate before_save :ensure_credit_card_is_on_file def set_eligibility_for_rebate

    self.eligibility_for_rebate = decide_eligibility end def ensure_credit_card_is_on_file puts "check if credit card is on file" end end Order.create! => ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
  14. 40.

    class Order < ActiveRecord::Base before_save :set_eligibility_for_rebate before_save :ensure_credit_card_is_on_file def set_eligibility_for_rebate

    self.eligibility_for_rebate = decide_eligibility end def ensure_credit_card_is_on_file puts "check if credit card is on file" end end Order.create! => check if credit card is on file => <Order id: 4, eligibility_for_rebate: false>
  15. 42.

    class Order < ActiveRecord::Base before_save :set_eligibility_for_rebate before_save :ensure_credit_card_is_on_file def set_eligibility_for_rebate

    self.eligibility_for_rebate = decide_eligibility throw(:abort) end def ensure_credit_card_is_on_file puts "check if credit card is on file" end end Order.create! => ActiveRecord::RecordNotSaved: Failed to save the record
  16. 43.
  17. 46.

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

    _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  18. 47.

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

    _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  19. 48.
  20. 50.

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

    %> # _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  21. 51.
  22. 57.

    class EmailJob < ActiveJob::Base self.queue_adapter = :sidekiq end class NewletterJob

    < ActiveJob::Base self.queue_adapter = :delayed_job end
  23. 58.

    class EmailJob < ActiveJob::Base self.queue_adapter = :sidekiq end class NewletterJob

    < ActiveJob::Base self.queue_adapter = :delayed_job end EmailJob.queue_adapter => #<ActiveJob::QueueAdapters::SidekiqAdapter: 0x007fb3d0b2e4a0> NewletterJob.queue_adapter => #<ActiveJob::QueueAdapters::DealyedJobAdapter: 0x007fb3d0c61b88>
  24. 69.
  25. 70.
  26. 72.

    def create @post = Post.new(post_params) if @post.save redirect_to @post, notice:

    'Post was successfully created.’ else render ‘new’ end end
  27. 73.

    Started POST "/posts" for ::1 at 2016-06-02 09:24:53 -0400 Processing

    by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"FaRp8NUAZWIW0WnMzLpKKQht9WWeTxeSvOvO2GeFpkGbOXzYEnvxTKEZf7gwY 4oy6cW+GRQ3UE6Zu0iXGB4I5A==", "post"=>{"content"=>"hello"}, "commit"=>"Create Post"} (0.2ms) begin transaction SQL (0.3ms) INSERT INTO "posts" ("content", "created_at", "updated_at") VALUES (?, ?, ?) [["content", "hello"], ["created_at", 2016-06-02 13:24:53 UTC], ["updated_at", 2016-06-02 13:24:53 UTC]] (0.7ms) commit transaction Redirected to http://localhost:3000/posts/1 Completed 302 Found in 6ms (ActiveRecord: 1.3ms)
  28. 75.
  29. 76.

    Started POST "/posts" for ::1 at 2016-06-02 09:25:11 -0400 Processing

    by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"RC46YER7l5yQz1J +DkZIWINBixkaMz9qWWlFzvZT8K3Ksy9IgwADsicHRAryn4hDYunAZZBLeLZ8OcOBicheCA==", "post"=>{"content"=>"hello 2"}, "commit"=>"Create Post"} (0.0ms) begin transaction SQL (0.2ms) INSERT INTO "posts" ("content", "created_at", "updated_at") VALUES (?, ?, ?) [["content", "hello 2"], ["created_at", 2016-06-02 13:25:11 UTC], ["updated_at", 2016-06-02 13:25:11 UTC]] (1.5ms) commit transaction No template found for PostsController#create, rendering head :no_content Completed 204 No Content in 64ms (ActiveRecord: 2.2ms)
  30. 77.

    Started POST "/posts" for ::1 at 2016-06-02 09:25:11 -0400 Processing

    by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"RC46YER7l5yQz1J +DkZIWINBixkaMz9qWWlFzvZT8K3Ksy9IgwADsicHRAryn4hDYunAZZBLeLZ8OcOBicheCA==", "post"=>{"content"=>"hello 2"}, "commit"=>"Create Post"} (0.0ms) begin transaction SQL (0.2ms) INSERT INTO "posts" ("content", "created_at", "updated_at") VALUES (?, ?, ?) [["content", "hello 2"], ["created_at", 2016-06-02 13:25:11 UTC], ["updated_at", 2016-06-02 13:25:11 UTC]] (1.5ms) commit transaction No template found for PostsController#create, rendering head :no_content Completed 204 No Content in 64ms (ActiveRecord: 2.2ms)
  31. 81.

    post articles_path(format: :json), params: { article: { title: 'Ahoy!' }

    }.to_json, headers: { 'Content-Type' => 'application/json' }
  32. 84.

    post articles_path, { article: { title: 'Ahoy!' } }, as:

    :json assert_equal({ id: Article.last.id, title: 'Ahoy!' }, response.parsed_body)
  33. 88.

    <body> <% cache 'signup-text' do %> <h1>Welcome!</h1> <p>You have successfully

    signed up. Your username is: <br /> <% end %> <%= @user.login %>. <br /> </p> <%= render :partial => 'footer' %> </body>
  34. 89.

    <body> <% cache 'signup-text' do %> <h1>Welcome!</h1> <p>You have successfully

    signed up. Your username is: <br /> <% end %> <%= @user.login %>. <br /> </p> <%= render :partial => 'footer' %> </body>
  35. 90.

    Cache digest for app/views/user_mailer/_footer.erb: 7313427d26cc1f701b1e0212498cee38 Cache digest for app/views/user_mailer/welcome_email.html.erb: 30efff0173fd5f29a88ffe79a9eab617

    Rendered user_mailer/_footer.erb (0.3ms) Rendered user_mailer/welcome_email.html.erb (26.1ms) Cache digest for app/views/user_mailer/welcome_email.text.erb: 77f41fe6159c5736ab2026a44bc8de55 Rendered user_mailer/welcome_email.text.erb (0.2ms) UserMailer#welcome_email: processed outbound mail in 190.3ms