Rails 5 features you have not heard about

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
August 13, 2016
92

Rails 5 features you have not heard about

Talk given at Rubyconf Malaysia Rubyconf.my

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

August 13, 2016
Tweet

Transcript

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

    5

  6. 14.

    5

  7. 15.

    5

  8. 16.

    5

  9. 17.

    5

  10. 18.
  11. 20.
  12. 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
  13. 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! => ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
  14. 41.

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

    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. 44.
  17. 47.

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

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

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

    _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  19. 49.
  20. 51.

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

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

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

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

    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. 71.
  25. 72.
  26. 74.

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

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

    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. 77.
  29. 78.

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

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

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

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

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

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

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

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

    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
  36. 103.
  37. 107.

    An optional hash of request parameters An optional hash of

    session variables An optional hash of flash values Rails 4
  38. 108.

    An optional hash of request parameters An optional hash of

    session variables An optional hash of flash values Rails 4
  39. 109.

    class ProductsControllerTest < ActionController::TestCase def test_create post :create, { product:

    { name: "FIFA" } }, { user_id: User.first.id } assert_response :success end end Rails 4
  40. 110.

    class ProductsControllerTest < ActionController::TestCase def test_create post :create, nil, {

    user_id: User.first.id } assert_response :success end end Rails 4
  41. 111.
  42. 112.
  43. 114.

    class ProductsControllerTest < ActionController::TestCase def test_create post :create, params: {

    product: { name: "FIFA" } }, session: { user_id: User.first.id } assert_response :success end end Rails 5
  44. 115.

    DEPRECATION WARNING: ActionController::TestCase HTTP request methods will accept only keyword

    arguments in future Rails versions. Examples: get :show, params: { id: 1 }, session: { user_id: 1 } process :update, method: :post, params: { id: 1 } Rails 5
  45. 129.

    >> user_params.inject DEPRECATION WARNING: Method inject is deprecated and will

    be removed in Rails 5.1, as `ActionController::Parameters` no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.0/classes/ActionController/ Parameters.html
  46. 148.