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

Features of Rails 5 you have not heard about

Prathamesh Sonpatki
June 03, 2016
100

Features of Rails 5 you have not heard about

Talk given at Ruby Nation 2016

Prathamesh Sonpatki

June 03, 2016
Tweet

Transcript

  1. 5

  2. 5

  3. 5

  4. 5

  5. 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
  6. 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
  7. 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>
  8. 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
  9. # index.html.erb <%= render partial: 'todo', collection: @todos %> #

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

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

    %> # _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>
  12. class EmailJob < ActiveJob::Base self.queue_adapter = :sidekiq end class NewletterJob

    < ActiveJob::Base self.queue_adapter = :delayed_job end
  13. 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>
  14. def create @post = Post.new(post_params) if @post.save redirect_to @post, notice:

    'Post was successfully created.’ else render ‘new’ end end
  15. 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)
  16. 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)
  17. 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)
  18. post articles_path(format: :json), params: { article: { title: 'Ahoy!' }

    }.to_json, headers: { 'Content-Type' => 'application/json' }
  19. post articles_path, { article: { title: 'Ahoy!' } }, as:

    :json assert_equal({ id: Article.last.id, title: 'Ahoy!' }, response.parsed_body)
  20. <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>
  21. <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>
  22. 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