Skip to content

Instantly share code, notes, and snippets.

require 'benchmark/ips'
Benchmark.ips do |x|
x.report('#exists?') { User.where(id: 1).exists? }
x.report('#any?') { User.where(id: 1).any? }
x.report('#present?') { User.where(id: 1).present? }
x.compare!
end
@zismailov
zismailov / unicorn.rake
Created January 9, 2021 20:03 — forked from mdesantis/unicorn.rake
Rails tasks for managing unicorn server instances
# Tasks for managing Unicorn instances of a Rails application.
# Compatible with Ruby >= 1.9.2 and Rails >= 2 .
# Unicorn signals: http://unicorn.bogomips.org/SIGNALS.html
namespace :unicorn do
class UnicornPIDError < StandardError; end
def rails_env
Rails.env

Setup

Replace IRB with Pry (in your Gemfile) and Byebug with pry-byebug.

gem 'pry-rails', group: [:development, :test]
gem 'pry-byebug', group: [:development, :test]

Using PRY

@zismailov
zismailov / install.md
Created September 4, 2018 12:00 — forked from srt32/install.md
installing PostGIS on OSX

Installing the Postgres PostGIS extension on OSX

For reference: http://postgis.net/install

If you don’t already have PG installed

The most reliable way to get PostGIS on OSX is to download and install Postgres.app. Great for development and testing. Do not mix with other installations. Select the extension when prompted.

If you already have PG installed

@zismailov
zismailov / 1.rb
Created September 26, 2017 12:39
Fetch with includes
[2] pry(main)> Product.last(10).map{|p| p.variants}.flatten
Product Load (1.1ms) SELECT "products".* FROM "products" ORDER BY "products"."id" DESC LIMIT 10
Variant Load (96.5ms) SELECT "variants".* FROM "variants" WHERE "variants"."nomenclature_id" = 247664 AND "variants"."archived_at" IS NULL ORDER BY order_weight [["nomenclature_id", 247664]]
Variant Load (0.6ms) SELECT "variants".* FROM "variants" WHERE "variants"."nomenclature_id" = 247665 AND "variants"."archived_at" IS NULL ORDER BY order_weight [["nomenclature_id", 247665]]
Variant Load (0.3ms) SELECT "variants".* FROM "variants" WHERE "variants"."nomenclature_id" = 247666 AND "variants"."archived_at" IS NULL ORDER BY order_weight [["nomenclature_id", 247666]]
Variant Load (0.6ms) SELECT "variants".* FROM "variants" WHERE "variants"."nomenclature_id" = 247667 AND "variants"."archived_at" IS NULL ORDER BY order_weight [["nomenclature_id", 247667]]
Variant Load (0.5ms) SELECT "variants".* FROM "variants" WHERE "variants"."nome
@zismailov
zismailov / gist:e606d6527563092cd5e2738a57397517
Created September 20, 2017 17:18
Получить все внешние ссылки на веб странице
var curr_domain = window.location.href.split('//')[1].split('/')[0];
var outside_links = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
var res = links[i].href.split('//')[1];
if(typeof res == 'undefined') continue;
if(res.split('/')[0] == curr_domain) continue;
outside_links.push('http://' + res);
}
@zismailov
zismailov / awesome_pry.md
Last active November 1, 2017 07:36
Pry for Rails setup

How to make awesome pry for Rails

Tested on Rails 5.1, for development, test and production (!).

At the top of your Gemfile (don't add to :development or :test):

  • Add gem 'pry' to your Gemfile
  • Add gem 'awesome_print' to your Gemfile

Make sure you updated your application.rb (not development.rb):

@zismailov
zismailov / attachment.rb
Created July 29, 2017 14:46 — forked from madwork/attachment.rb
Polymorphic attachments with CarrierWave and nested_attributes
class Attachment < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
# Associations
belongs_to :attached_item, polymorphic: true
# Validations
validates_presence_of :attachment
/**
* Jesse Weisbeck's Crossword Puzzle (for all 3 people left who want to play them)
*
*/
(function($){
$.fn.crossword = function(entryData) {
/*
Qurossword Puzzle: a javascript + jQuery crossword puzzle
"light" refers to a white box - or an input
@zismailov
zismailov / regex_interpolation.rb
Created July 7, 2017 07:02
Benchmark: regex interpolation
require 'benchmark/ips'
GITHUB_COM = %r{https?://(?:www\.)?github\.com}i
Benchmark.ips do |x|
x.report('with o') { %r{\A#{GITHUB_COM}/([^/]+)/?\z}o }
x.report('without o') { %r{\A#{GITHUB_COM}/([^/]+)/?\z} }
x.compare!
end