Skip to content

Instantly share code, notes, and snippets.

#
# Copyright (c) Yuri Omelchuk <jurgen@upscript.com>
#
# January 8, 2009
#
# Maze class written for RubyLearning contest
# http://rubylearning.com/blog/2009/12/27/rpcfn-mazes-5/
#
# solution is based on find shortest path algorithm
#
@jurgens
jurgens / object_match.rb
Created April 29, 2011 15:24
Complex object matcher
module Kernel
def object_match?(a, b, cmp_key = 'guid')
cmp = lambda {|x,y| (x[cmp_key] <=> y[cmp_key]).to_i}
if a.kind_of? Hash
a.each do |key, value|
return false unless object_match?(value, b[key], cmp_key)
end
elsif a.kind_of? Array
a.sort!(&cmp)
@jurgens
jurgens / product.rb
Created May 26, 2012 11:14
Speed up bulk insert operation
require 'activerecord-import'
class Product < ActiveRecord::Base
has_many :product_keywords, dependent: :delete_all
def update_product_keywords
self.product_keywords.delete_all
records = []
title_keywords.each do |keyword|
@jurgens
jurgens / requests-search_spec.rb
Created September 10, 2012 15:13
thinking sphinx helpers
describe do
describe "Find pages", :sphinx do
before { Factory :page, site_ids: [Site.first.id], title: "first", hidden: false }
before { Factory :page, site_ids: [Site.last.id], title: "second", hidden: true }
before { ThinkingSphinx::Test.index; sleep 1 }
specify "should be able to search pages", :js do
search_for "first"
within ".narrow-col" do
@jurgens
jurgens / gist:3721147
Created September 14, 2012 10:07
convert to 1.9 hash syntax
Dir['**/*.rb'].each { |f|
s = open(f).read
awesome_rx = /(?<!return)(?<!:)(?<!\w)(\s+):(\w+)\s*=>/
count = s.scan(awesome_rx).length
next if count.zero?
s.gsub!(awesome_rx, '\1\2:')
puts "#{count} replacements @ #{f}"
open(f, 'w') { |b| b << s } }
class WWW
STARTS_WITH_WWW = /^www\./i
def initialize(app)
@app = app
end
def call(env)
if env['HTTP_HOST'] =~ STARTS_WITH_WWW
@jurgens
jurgens / spec.txt
Last active December 11, 2015 20:19
Test task
## Требуется реализовать следующую функциональность:
* как админ я могу пригласить нового пользователя в систему, зная его почту
* как пользователь, получивший приглашение, и только с приглашением, я могу зарегистрироваться в системе,
введя свой почтовый адрес (не обязательно тот, на который мне выслали приглашение), пароль
и подтверждение пароля
* как зарегистрированный пользователь я вижу приглашение заполнить номер телефона до тех пор,
пока не заполню это поле в своем профиле
## Дополнительные условия
@jurgens
jurgens / database_cleaner.rb
Created November 22, 2013 10:07
rspec/support/database_cleaner.rb
require 'database_cleaner'
RSpec.configure do |config|
config.before(:each) do |example_group|
DatabaseCleaner.strategy = if (Capybara.current_driver != :rack_test)
[:truncation, { except: %w[statuses] }]
else
:transaction
end
DatabaseCleaner.start
@jurgens
jurgens / gist:8472992
Created January 17, 2014 13:00
setup nginx as a service on ubuntu
wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
sudo mv init-deb.sh /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults
after that you can control nginx with
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart
@jurgens
jurgens / init_map.js.coffee
Created March 5, 2014 15:21
show single point on a google map
class InitMap
constructor: (element, data) ->
latlng = new google.maps.LatLng(data.lat, data.long)
options =
zoom: 12
center: latlng
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(element, options)