Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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_select_attributes_extension.rb
Created February 15, 2011 16:16
extend rails3 activerecord public interface with powerfull connection.select_value(s) method, put this file to config/initializers/
module ActiveRecordSelectAttributesExtension
def select_id(field = 'id')
select_values(field)
end
def select_values(field = nil)
scope = field ? self.scoped.select(field) : self.scoped
connection.select_values(scope.to_sql)
end
@skojin
skojin / backup_my_gists.rb
Created February 6, 2011 12:12
script to backup my gists
require 'json'
require 'open-uri'
gists = JSON.parse(open("http://gist.github.com/api/v1/json/gists/#{ENV['GIST_USER']}").read)['gists']
repos = gists.map{|g| g['repo']}
existing, new = repos.partition{|r| File.exists?(r)}
new.each do |r|
puts "clone new #{r} gist"
`git clone git://gist.github.com/#{r}.git`
@skojin
skojin / eval_javascript_in_parent_helper.rb
Created February 1, 2011 20:03
respond_to_parent as single helper withot respond_to, works with rails3
# more simple version of https://github.com/itkin/respond_to_parent.git without dirty hacks that works good in rails3
# usage:
# in controller#create append render :layout => false
# in create.html.erb
# <%= eval_javascript_in_parent(<<EOF
# $('#entries-list').prepend('#{escape_javascript(render @entry)}');
# EOF
# ) %>
#
# most source from https://github.com/itkin/respond_to_parent/blob/master/lib/responds_to_parent.rb
@skojin
skojin / .irbrc
Created January 25, 2011 13:39
my .irbrc
require 'irb/completion'
require 'pp'
require 'rubygems'
# Automatic Indentation
IRB.conf[:AUTO_INDENT] = true
# Remove the annoying irb(main):001:0 and replace with >>
IRB.conf[:PROMPT_MODE] = :SIMPLE
# Add all gems in the global gemset to the $LOAD_PATH so they can be used in rails3 console with bundler