Skip to content

Instantly share code, notes, and snippets.

View dhruvasagar's full-sized avatar

Dhruva Sagar dhruvasagar

View GitHub Profile
@dhruvasagar
dhruvasagar / add_missing_master_variants.rb
Last active June 20, 2018 19:20
Script to add master variants for spree_products that are missing them
puts "Rails environment is #{Rails.env}"
products_without_master = Spree::Product.joins('left outer join spree_variants sv on sv.product_id = spree_products.id and sv.is_master = 1').where('sv.id is null').all
ids_without_master = products_without_master.map(&:id)
puts "Spree products without master variants are #{ids_without_master.count} in number and have ids #{ids_without_master}"
updated_at_values = products_without_master.map(&:updated_at)
ActiveRecord::Base.transaction do
puts "Creating missing master variants inside a transaction..."
function! s:RandomNumber(limit)
let components = split(reltimestr(reltime()), '\.')
let microseconds = components[-1] + 0
return microseconds % a:limit
endfunction
function! s:RandomScheme()
let choices = []
for fname in globpath(&runtimepath, 'colors/*.vim', 1)
let choices += [fnamemodify(fname, ':t:r')]
#!/usr/bin/env bash
set -u
set -e
export GIT_WORK_TREE="/var/www/example.com"
export NODE_VERSION="0.10"
echo "--> Checking out..."
git checkout -f
function! FoldAllBut( foldminlines )
folddoclosed
\ if ( (foldclosed(".") >= 0 ) && ( foldclosedend(".") - foldclosed(".") + 1 < a:foldminlines ))
\ exe "normal! zO"
\ endif
endfunction
@dhruvasagar
dhruvasagar / abilities.rb
Created September 17, 2012 07:58 — forked from ryanb/abilities.rb
How you can break up large Ability class in CanCan
module Abilities
def self.ability_for(user)
if user.admin?
AdminAbility.new(user)
else user
MemberAbility.new(user)
else
GuestAbility.new
end
end
@dhruvasagar
dhruvasagar / damerau_levenshtein_distance.rb
Last active March 3, 2019 21:34 — forked from dimus/damerau_levenshtein distance for ruby
Damerau-Levenshtein distance for ruby in C
#!/usr/bin/env ruby1.9
# encoding: UTF-8
require 'rubygems'
require 'inline'
require 'time'
class DamerauLevenshtein
def distance(str1, str2, block_size=2, max_distance=10)
res = distance_utf(str1.unpack("U*"), str2.unpack("U*"), block_size, max_distance)
(res > max_distance) ? nil : res