Skip to content

Instantly share code, notes, and snippets.

View redis_stats.rb
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 Mar 5, 2018
Policy like in cancan, but with class-level declarations
View policy.rb
# 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 Nov 30, 2017
Turbolinks adapter for vue
View vue-turbolinks-adapter.coffee
@printercu
printercu / telegram_bot.rake
Created Sep 5, 2017
Telegram::Bot poller with daemons gem
View telegram_bot.rake
# 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 Jan 24, 2017
Convert svg images to multiple dimensions.
View convert-images.rb
#!/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 Jan 29, 2017
Speed up Rails 5 integration tests
View rails-5.0-integration-tests-speedup.md

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 Aug 15, 2016
Keep MarkerClusterer working after Google shut down svn-repos
View marker-clusterer.md

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 Dec 6, 2016
Set custom symbol for currency in Money gem
View gist:022b0ee5bc3db99cd5bc
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,
View sql_adapter.php
<?php
class SqlAdapter
{
protected $pdo;
protected $query;
protected $sth;
protected $params;
protected $lastMethod;
@printercu
printercu / class_attribute_bm.rb
Last active Aug 29, 2015
Benchmarking thread-safe class_attribute
View class_attribute_bm.rb
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)
You can’t perform that action at this time.