Skip to content

Instantly share code, notes, and snippets.

class RedisStats
attr_reader :redis, :pattern
def initialize(redis, pattern = '*')
@redis = redis
@pattern = pattern
end
def all_keys
@all_keys ||= [].tap do |result|
@printercu
printercu / policy.rb
Last active March 5, 2018 11:43
Policy like in cancan, but with class-level declarations
# Simple policy implementation. Like a mixin of Pundit and CanCan.
module Policy
ACTIONS_MAP = {
index: :read,
show: :read,
new: :create,
edit: :update,
}.freeze
class Error < StandardError; end
@printercu
printercu / vue-turbolinks-adapter.coffee
Last active October 13, 2020 21:38
Turbolinks adapter for vue
@printercu
printercu / telegram_bot.rake
Created September 5, 2017 12:05
Telegram::Bot poller with daemons gem
# lib/capistrano/tasks/telegram_bot.rake
namespace :telegram do
namespace :bot do
namespace :poller do
{
start: 'start an instance of the application',
stop: 'stop all instances of the application',
restart: 'stop all instances and restart them afterwards',
reload: 'send a SIGHUP to all instances of the application',
@printercu
printercu / convert-images.rb
Created January 24, 2017 18:36
Convert svg images to multiple dimensions.
#!/usr/bin/env ruby
#
# For some/file.svg run:
#
# script some/file
#
# Requirement for mac os:
#
# brew cask install inkscape
@printercu
printercu / rails-5.0-integration-tests-speedup.md
Last active March 7, 2022 01:20
Speed up Rails 5 integration tests

Speed up integration tests in Rails 5 app

Rails 5 recompiles templates on every request in test env. Fix will be released only in 5.0.2.

Add this lines to test helper to get the performance now (I've got x2 improvement):

class << ActionView::LookupContext::DetailsKey
 def clear
@printercu
printercu / marker-clusterer.md
Last active August 15, 2016 17:56
Keep MarkerClusterer working after Google shut down svn-repos

First we’ll configure Rails, assuming you already have a copy of google-maps-utility-library-v3 in your repo. If you don’t have it yet, there are instructions in the bottom.

Most likely, you already have added utility folder to the assets pipeline:

# config/initializer/assets.rb
Rails.application.config.assets.paths += %W(
 #{Rails.root}/vendor/assets/google-maps-utility-library-v3
@printercu
printercu / gist:022b0ee5bc3db99cd5bc
Last active December 6, 2016 05:48
Set custom symbol for currency in Money gem
2.2.1 (main):0[1] > Money.new(100, :RUB).currency.symbol
=> "₽"
2.2.1 (main):0[2] > show-method Money.new(100, :RUB).currency.symbol
From: /Users/max/.rvm/gems/ruby-2.2.1/gems/money-6.5.1/lib/money/currency.rb @ line 211:
Owner: Money::Currency
Visibility: public
Number of lines: 3
attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol,
@printercu
printercu / sql_adapter.php
Created November 7, 2014 09:21
SqlAdapter
<?php
class SqlAdapter
{
protected $pdo;
protected $query;
protected $sth;
protected $params;
protected $lastMethod;
@printercu
printercu / class_attribute_bm.rb
Last active August 29, 2015 14:05
Benchmarking thread-safe class_attribute
require 'benchmark'
require 'active_support/all'
class Class
def class_attribute2(*attrs)
options = attrs.extract_options!
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
instance_predicate = options.fetch(:instance_predicate, true)