Skip to content

Instantly share code, notes, and snippets.

View tonyfg's full-sized avatar

Tony Gonçalves tonyfg

View GitHub Profile
@tonyfg
tonyfg / log_response_time.rb
Created September 14, 2018 09:14
Log Rails request response time
# config/initializers/log_response_time.rb
event_logger = ::Logger.new("#{Rails.root}/log/response_times.log")
event_logger.formatter = proc { |_severity, _datetime, _progname, msg|
"#{msg}\n"
}
ActiveSupport::Notifications.subscribe('process_action.action_controller') do |_, started, finished, _, stats|
request_time = (finished - started) * 1000
controller = stats[:controller][0..-11].underscore
@tonyfg
tonyfg / application_controller.rb
Last active January 29, 2024 08:37
Log Rails object allocations during a request
class ApplicationController < ActionController::Base
around_action :track_object_allocation
def allocation_logger
@@allocation_logger ||= begin
logger = Logger.new("#{Rails.root}/log/allocation_trace.log")
logger.formatter = proc { |_severity, _datetime, _progname, msg|
"#{msg}\n"
}
logger
@tonyfg
tonyfg / models.rb
Created September 14, 2018 09:40
Org list models
# app/models/organization.rb
class Organization < ApplicationRecord
has_many :hqs
has_many :stores, through: :hqs
end
# app/models/country.rb
class Country < ApplicationRecord
end
@tonyfg
tonyfg / organizations_controller.rb
Created September 14, 2018 09:43
Org list unoptimized #index action
# app/controllers/organizations_controller.rb
class OrganizationsController < ApplicationController
def index
@organizations = Organization.all
end
end
@tonyfg
tonyfg / index.html.erb
Created September 14, 2018 09:48
Org list HTML views
# app/views/organizations/index.html.erb
<table>
<tbody>
<% @organizations.each do |organization| %>
<tr>
<td><%= organization.name % ></td>
<td><%= organization.address %></td>
<td><%= organization.contact_name %></td>
<td><%= organization.contact_phone %></td>
@tonyfg
tonyfg / index.json.jbuilder
Created September 14, 2018 09:50
Org list JSON views
# app/views/organizations/index.json.jbuilder
json.array! @organizations, partial: 'organizations/organization', as: :organization
# app/views/organizations/_organization.json.jbuilder
json.extract! organization, :id, :name, :address, :contact_name, :contact_phone, :created_at, :updated_at
json.url organization_url(organization, format: :json)
json.hqs organization.hqs do |hq|
json.extract! hq, :id, :name
@tonyfg
tonyfg / organizations_controller.rb
Created September 14, 2018 10:54
Org list #index optimized with .includes
# old action
def index
@organizations = Organization.all
end
# new action
def index
@organizations = if request.format.html?
Organization.includes(hqs: :country).all
else
@tonyfg
tonyfg / select_joins.rb
Created September 14, 2018 12:43
Org list #index optimized with .select and .joins
#
# app/controllers/organizations_controller.rb
#
# old action
def index
@organizations = Organization.all
end
# new action
@tonyfg
tonyfg / oj.rb
Created September 14, 2018 12:52
Rails OJ initializer
# config/initializers/oj.rb
Oj.optimize_rails
@tonyfg
tonyfg / Gems.txt
Last active February 9, 2022 18:56
Gems
Code quality:
rubocop - linter
https://github.com/prettier/plugin-ruby - (NOT A GEM!) Prettier for ruby
overcommit - git hooks to run linting before commits, etc
bundler-audit - check your gemfile for outdated gems with security issues
bundler-leak - check your gemfile for gems with known memory leaks
brakeman - rails static code analyzer with a focus on security
sqlint - linter for SQL code (I know, not ruby, but useful for a lot of ruby apps)
Background jobs: