Skip to content

Instantly share code, notes, and snippets.

View aderyabin's full-sized avatar
👨‍💻
building something new

Andrey Deryabin aderyabin

👨‍💻
building something new
View GitHub Profile
@aderyabin
aderyabin / gist:335138
Created March 17, 2010 11:27
generate random sequence
# generate random sequence
#SecureRandom also has methods for: base64, hex, bytes, random_number
#see: http://api.rubyonrails.org/classes/ActiveSupport/SecureRandom.html
require 'active_support/secure_random'
ActiveSupport::SecureRandom.hex(16)
# outputs: 6dc19d21e958e04645cddadb96e9c927
@aderyabin
aderyabin / gitignore
Created March 24, 2010 04:48
.gitignore
log/*.log
.DS_Store
public/cache/**/*
public/system/**/*
config/database.yml
config/*.sphinx.conf
config/s3_credentials.yml
*~
*.cache
*.pid
@aderyabin
aderyabin / remove_duplicates.sql
Created May 28, 2010 15:09
Remove duplicates from table
SHOW CREATE TABLE old_table;
INSERT INTO new_table SELECT DISTINCT * FROM old_table;
DROP TABLE old_table;
RENAME TABLE new_table TO old_table;
@aderyabin
aderyabin / gist:731631
Created December 7, 2010 09:52
polymorphic_select_tag
# polymorphic_select_tag @item, :owner, Post.all(:order=>'created_at DESC').collect{|b| #[b.title, [Post, b.id]]}
def polymorphic_select_tag(object, field, option_array=[], options={}, html_options={})
option_tags = "<option #{'selected="selected"' if (object.try(field) == nil)}/>" +
option_array.map do |tuple|
"<option value='#{tuple[1][0]}-#{tuple[1][1]}' #{'selected="selected"' if ((object.try(field).try(:id).to_s == tuple[1][1].to_s && object.try(field).try(:type).to_s == tuple[1][0].to_s) rescue false)}>#{tuple[0]}</option>"
end.to_s
prefix = options[:prefix] || ''
object_id = object.id
@aderyabin
aderyabin / gist:733296
Created December 8, 2010 13:45
define ruby version from rails application
IO.popen("ruby -v").readlines.to_s
@aderyabin
aderyabin / gist:734625
Created December 9, 2010 11:22
BreadCrumb from URL
# raw get_bread_crumb(request.request_uri)
def get_bread_crumb(url)
begin
breadcrumb = ''
so_far = '/'
elements = url.split('/')
for i in 1...elements.size
so_far += elements[i] + '/'
@aderyabin
aderyabin / db_dump.rake
Created January 26, 2011 16:04
rake db:dump
namespace :db do
desc "Backup database"
task :backup => :environment do
db_config = ActiveRecord::Base.configurations[Rails.env]
backup_path = "db/backups"
Dir.mkdir("#{Rails.root}/#{backup_path}") unless Dir.exist?("#{Rails.root}/#{backup_path}")
datestamp = Time.now.strftime("%Y%m%d%H%M%S")
file_name = "#{RAILS_ENV}_#{db_config['database']}_#{datestamp}.sql.gz"
@aderyabin
aderyabin / gist:953511
Created May 3, 2011 15:16
create_attr
def create_method(name, &block)
self.class.send(:define_method, name, &block)
end
def create_attr_methods(name)
create_method("#{name}=".to_sym) { |val| instance_variable_set("@" + name, val) }
create_method(name.to_sym) { instance_variable_get("@" + name) }
end
def create_attr(name, value = nil)
@aderyabin
aderyabin / gist:1437679
Created December 6, 2011 10:21
google chart
Gchart.line_xy(
:data => [[-1], [5.6401944, 5.693536, 5.589820899999999, 5.9419287999999995, 6.140745299999999, 5.6134203, 4.6947865, 4.3917905, 3.969158, 6.6177003, 5.5682765, 5.3081125], [-1], [4.469642, 4.398298800000001, 4.4974612, 4.5136058, 4.7888061, 4.619375999999999, 1.2265295, 2.977351, 3.2739, 4.8158845, 4.4618883, 4.4220661], [-1], [4.3555019999999995, 4.215306200000001, 4.2568991, 5.3902482, 5.4936451, 4.6054233, 2.781287, 1.945629, 2.80498, 5.7298489, 4.2575779, 4.2970867], [-1], [3.0365569999999997, 3.1637157, 2.8597486, 3.3726310999999995, 3.1634554, 3.1847927, 1.9845335, 1.8754915, 1.9770175, 3.7286499, 3.2215343, 2.8594553], [-1], [2.6395803000000004, 2.5640393999999995, 2.3002405, 2.7694116999999996, 2.9414337, 2.7909993, 1.578009, 0.617353, 2.0158055, 3.0155125, 2.4871983, 2.3391488], [-1], [2.4379844, 2.2451725999999996, 2.0674406999999997, 2.6068367, 2.4240726, 2.3331188, 1.242108, 1.1643645, 1.4375965, 2.9311815, 2.6668297, 2.2688855], [-1], [2.2926647000000004, 2.2747717, 2.7112956,
@aderyabin
aderyabin / gist:1465125
Created December 12, 2011 05:22
AppleScript to migrate from Things to Omnifocus
--------------------------------------------------
--------------------------------------------------
-- Import tasks from Things to OmniFocus
--------------------------------------------------
--------------------------------------------------
tell application "Things"
-- Loop through ToDos in Things
repeat with aToDo in to dos of list "Next"