Skip to content

Instantly share code, notes, and snippets.

I may be slow to respond.

Aleksey Leshchuk alekseyl

I may be slow to respond.
View GitHub Profile
alekseyl / virtual_attr.rb
Created Dec 13, 2016
virtual attributes example
View virtual_attr.rb
# DB way
.joins(purchase: :product)
.select(["shipments.*","sum(purchases.items_count*products.gross_weight) as gross_weight"])
alekseyl / all_keys.rb
Last active Dec 13, 2016
for medium article 'Rails fast collection render'
View all_keys.rb
# nil means current_user is empty i.e. it's a guest
keys = [ [:user, :admin, :moderator, nil], [:ru, :en ] ]
# all combination of keys:
all_flatten_keys = keys.pop.product(*keys)
# [[:ru, :user], [:ru, :admin], [:ru, :moderator], [:ru, nil],
# [:en, :user], [:en, :admin], [:en, :moderator], [:en, nil]]
alekseyl / prerender.rb
Created Dec 13, 2016
for medium article 'Rails fast collection render'
View prerender.rb
class Product <ActiveRecord::Base
after_commit :prerender
def prerender
keys = [ [:user, :admin, :moderator, nil], [:ru, :en ] ]
keys.pop.product(*keys).each do |lr|
# lr - locale + role
assigns: {locals: {product: self, role: lr[1], locale: lr[0]} },
inline: '<% render partial: "products/product", locals: @locals %>')
#of course products/_product.html.slim must start with
View cache_fine_tuning_example.conf
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503
alekseyl / digest_namespace.rb
Last active Jan 31, 2017
Digest on all files from folder in rails
View digest_namespace.rb
def all_files_in(dir)
Dir[ File.join(dir, '**', '*') ].reject { |p| p }
new_namespace = Digest::SHA256.hexdigest( all_files_in( 'app/views' )
.map{|fl| Digest::SHA256.file(fl).hexdigest }
.join )
View rails_example_for_nginx_cache.rb
#in controller:
before_action :resourceful_cache_headers
before_action :resourceless_cache_headers
before_action :skip_session
def skip_session
# if you need to deliver flash, or authencity token, or you explicitly need session
request.session_options[:skip] = true unless you_need_session
alekseyl / nginx_cache_full_example.conf
Last active Mar 28, 2018
nginx/rails cache config example
View nginx_cache_full_example.conf
proxy_cache_pass /path/to/cache levels= keys_zone=rails_cache:10m max_size=10g use_temp_path=off inactive=120m;
location / {
try_files $uri @rails;
location @rails {
# lets skip authorized user.
proxy_no_cache $cookie_remember_user_token;
proxy_cache_bypass $cookie_remember_user_token;
alekseyl / run_async_with_rescue.rb
Created Feb 17, 2017
run_async_with_rescue example
View run_async_with_rescue.rb
def run_async_with_rescue( airbrake_params = {} )
backtrace = Kernel.caller do
rescue => e
e.set_backtrace( backtrace )
notice = Airbrake.build_notice(e, airbrake_params)
Airbrake.notify(notice) if notice
alekseyl / devise_notification_threaded_example.rb
Last active Feb 17, 2017
deliver devise emails with threads
View devise_notification_threaded_example.rb
def send_devise_notification(notification, *args)
# don't forget email not sended without calling deliver
message = devise_mailer.send(notification, self, *args)
# this is the params you want to anylise when something crashes
add_to_airbrake_notice = attributes.slice('email', 'name', 'surname', 'cell', 'id')
# run message delivering
run_async_with_rescue(add_to_airbrake_notice) { message.deliver }
alekseyl / conditional_location.conf
Last active Feb 21, 2017
Conditional location selection with try_files. It may be useful in cases of A\B version of a site running on same domain name. You can do sticky cookie with this example.
View conditional_location.conf
server {
set $versioned_location @version_one;
if ( $cookie_condition ) {
set $versioned_location @version_two;
if ( $cookie_site_version = "version_one" ) {
set $versioned_location @version_one;