#90
Benchmarking and Refactoring the content_for View Helper
7-23-2017
Summary
In a recent blog post, we looked into the content_for view helper to render breadcrumbs. Once we got the feature working, it's time to refactor the feature to lower technical debt.
4
rails
performance
benchmark
refactor
7:11
Summary
In a recent blog post, we looked into the content_for view helper to render breadcrumbs. Once we got the feature working, it's time to refactor the feature to lower technical debt.
4
Resources
Original Blog Entry - https://blog.driftingruby.com/hacking-content_for-to-create-a-simple-display-helper/
Rails Pertest - https://github.com/rails/rails-perftest
Source - https://github.com/driftingruby/090-benchmarking-and-refactoring-the-content-for-view-helper
Summary
Terminalrails generate performance_test homepageGemfilegem 'rails-perftest'
gem 'ruby-prof'application.html.erb <%= benchmark 'Breadcrumbs' do %>
<%= render 'layouts/breadcrumbs' %>
<% end %>test/performance/homepage_test.rbrequire 'test_helper'
require 'rails/performance_test_help'
class HomepageTest < ActionDispatch::PerformanceTest
self.profile_options = { runs: 500, metrics: [:wall_time, :memory], output: 'tmp/performance' }
test "homepage" do
get '/'
end
endTerminalrails test:performance_breadcrumbs.html.erb# Filed removed since it's no longer needed.
# <%= content_tag :nav, class: 'breadcrumb' do %>
# <%= link_to 'Home', root_path, class: 'breadcrumb-item' %>
# <% breadcrumbs = JSON.parse(sanitize yield(:breadcrumbs)) if content_for?(:breadcrumbs) %>
# <% breadcrumbs.each do |breadcrumb| %>
# <% case breadcrumb.class.to_s %>
# <% when 'Array' %>
# <% string, link = breadcrumb %>
# <%= link_to string, link, class: "breadcrumb-item #{breadcrumb.equal?(breadcrumbs.last) ? 'active' : nil}" %>
# <% when 'String' %>
# <%= content_tag :span, breadcrumb, class: "breadcrumb-item #{breadcrumb.equal?(breadcrumbs.last) ? 'active' : nil}" %>
# <% end %>
# <% end if content_for?(:breadcrumbs) %>
# <% end %>
# <%= yield :breadcrumbs %>index.html.erb<%= content_for :breadcrumbs, breadcrumbs_helper(['Users', ['John Doe', 'URL_STRING'],'Static', 'Edit']) %>application_helper.rb def breadcrumbs_helper(breadcrumbs)
html = ''
html << "<nav class='breadcrumb'>"
html << link_to('Home', root_path, class: 'breadcrumb-item')
breadcrumbs.each do |item|
case item.class.to_s
when 'Array'
string, link = item
html << link_to(string, link, class: "breadcrumb-item #{item.equal?(breadcrumbs.last) ? 'active' : nil}")
when 'String'
html << content_tag(:span, item, class: "breadcrumb-item #{item.equal?(breadcrumbs.last) ? 'active' : nil}")
end
end
html << '</nav>'
html.html_safe
endapplication.html.erb<%= yield :breadcrumbs %>
<%= yield %>
Any idea why deleting _breadcrumbs.html.erb and putting the yield code it into application.html.haml was slower?