Skip to content

Instantly share code, notes, and snippets.

View alekseyl's full-sized avatar
👨‍💻

Aleksey Leshchuk alekseyl

👨‍💻
View GitHub Profile
@alekseyl
alekseyl / cache_fine_tuning_example.conf
Created January 31, 2017 15:44
nginx fine cache tuning
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
alekseyl / prerender.rb
Created December 13, 2016 09:06
for medium article 'Rails fast collection render'
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
ApplicationController.render(
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
@alekseyl
alekseyl / all_keys.rb
Last active December 13, 2016 09:05
for medium article 'Rails fast collection render'
# 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
alekseyl / virtual_attr.rb
Created December 13, 2016 08:49
virtual attributes example
# DB way
Shipment.scope
.joins(purchase: :product)
.select(["shipments.*","sum(purchases.items_count*products.gross_weight) as gross_weight"])
.group("shipments.id")