Skip to content

Instantly share code, notes, and snippets.

View tonyfg's full-sized avatar

Tony Gonçalves tonyfg

View GitHub Profile
@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 / 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 / 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