Skip to content

Instantly share code, notes, and snippets.

@skojin
skojin / nginx.conf
Created February 16, 2011 14:33
nginx rails configuration, with assets expiration
server {
listen 80;
server_name APP.com;
rewrite ^/(.*) http://www.APP.com/$1 permanent;
}
server {
listen 80;
server_name www.APP.com;
root /home/USER/sites/APP/current/public;
@skojin
skojin / active_record_instantiate_logging.rb
Created March 30, 2011 12:31
Log time of model instantiate in addition to SQL execution time
if Rails.logger.level = Logger::DEBUG
ActiveRecord::Base.module_eval do
class << self
def find_by_sql_with_instantiate_logging(sql)
message = connection.send(:format_log_entry, "#{name} Load+Instantiate")
ActiveRecord::Base.benchmark(message, Logger::DEBUG, false) do
find_by_sql_without_instantiate_logging(sql)
end
end
@skojin
skojin / set_cookie_domain.rb
Created March 31, 2011 09:15
middleware to allow set rails session subdomain wide, need when application is mulity domain. tested on rails 2.3.5
@skojin
skojin / testing.rake
Created May 10, 2011 16:33
set of rake tasks to run part of unit tests with less keyboard typing
desc "Shortcut for functional tests"
task :f => "test:functionals"
desc "Shortcut for unit tests"
task :u => "test:units"
desc "Shortcut for integration tests"
task :i => "test:integration"
desc "Run unit and functional tests"
@skojin
skojin / back_default_redirect.rb
Created July 13, 2011 14:09
replacement for redirect_to(:back) that works even if no referer (e.g. via direct url)
# same as redirect_to(:back) but works even if no referer (e.g. via direct url)
# include this module to ApplicationController
module BackDefaultRedirect
protected
# use as redirect_to back_or_default(root_url)
def back_or_default(default = '/')
referer = request.env['HTTP_REFERER']
# if has HTTP_REFERER and it not equals current url
if referer && !referer.include?(request.request_uri)
@skojin
skojin / navigation_helper.rb
Created November 15, 2011 14:40
Rails Helper to build 'active' links
module NavigationHelper
# get navigation 'active' css class if rule match
def nav_class(match_rules)
navigation_url_active?(nil, match_rules) ? 'active' : nil
end
# @param match_rules if :resource symbol, then match to url that starts withs specified url
# @param match_rules if :same symbol, then match exactly to url
# @param match_rules if hash {:controller, :action, :path, :method} and same yes with _not suffix (like :controller_not)
@skojin
skojin / v1
Created January 26, 2012 01:53
Get public IP address and copy to clipboard
curl -silent http://checkip.dyndns.org | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | pbcopy
@skojin
skojin / youtube_download.rb
Created February 21, 2012 13:31
download video from youtube subscription
# download video from youtube subscription
# depends: youtube-dl
# OS: windows
require 'open-uri'
require 'hpricot'
USERNAME = ''
YOUTUBE_DL_PATH = "python C:\\programs\\Video\\youtube\\youtube-dl\\youtube-dl.py"
YOUTUBE_DL_PARAMS = "--retries=30 --max-quality=22 -c -i -w"
@skojin
skojin / application_controller.rb
Created October 18, 2012 13:55
make p and puts in controller actions output into console and color it to make more bright
class ApplicationController < ActionController::Base
include ColorizedPrint if Rails.env.development?
end
module ActiveRecordSelectAttributesExtension
# Returns an array of arrays containing the field values.
# Order is the same as that returned by +columns+.
# select_rows("id, type") => [[1, 'one'], [2, 'one'], [3, 'two']]
def select_rows(fields = nil)
scope = fields ? self.scoped.select(fields) : self.scoped
connection.select_rows(scope.to_sql)
end