Skip to content

Instantly share code, notes, and snippets.

@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
@skojin
skojin / irb_require_without_bundler_hack.rb
Created January 25, 2011 13:33
workaround to load irb specific gem (loaded in .irbrc) in bundler environment, like rails3 console
# Add all gems in the global gemset to the $LOAD_PATH so they can be used in rails3 console with bundler
if defined?(::Bundler)
$LOAD_PATH.concat Dir.glob("#{ENV['rvm_path']}/gems/#{ENV['rvm_ruby_string']}@global/gems/*/lib")
end
@skojin
skojin / consider_my_requests_local.rb
Created January 24, 2011 15:17
same as config.consider_all_requests_local but for production and only for specified ip address, rails 3.2
silence_warnings do
ActionDispatch::Request::LOCALHOST = (ActionDispatch::Request::LOCALHOST + ['192.168.0.1']).freeze
end
ActionController::Base.module_eval do
def show_detailed_exceptions?
request.local?
end
end
@skojin
skojin / deploy.rb
Created January 21, 2011 12:39
base capistrano configuration with bunlder and passenger
require 'bundler/capistrano'
set :application, "APPNAME"
set :domain, "mydomain.com"
set :user, "deployer"
set :password, ""
set :runner, user
default_run_options[:pty] = true # Must be set for the password prompt from git to work
@skojin
skojin / google-reader-i-ipad-bookmarklet.js
Created November 8, 2010 10:07
bookmarket that increase mobile google reader http://www.google.com/reader/i content font size, tuned for ipad
javascript:(function(){ var css = '.entry-row.expanded {font-size: 130% !important;} .entry {padding-left: 30px;}';var sn = document.createElement("style"); sn.setAttribute("type", "text/css");sn.setAttribute("media", "screen"); sn.appendChild(document.createTextNode(css));document.getElementsByTagName("head")[0].appendChild(sn);})()
module MultiInsert
# This method almost like sequels dataset.import()
#
# Inserts multiple records into the associated table.
#
# This method is called with a columns array and an array of value arrays:
#
# Model.mass_insert(['x', 'y'], [[1, 2], [3, 4]])
#
def multi_insert(columns, values)
module InsertWithUpdate
# INSERT INTO .. ON DUPLICATE KEY UPDATE ..
def insert_with_update(attributes, update_sql)
columns = []
values = []
attributes.each_pair do |attr, value|
columns << connection.quote_column_name(attr)
values << connection.quote(value)
end
connection.insert "INSERT INTO #{quoted_table_name} (#{columns.join(',')}) VALUES (#{values.join(',')}) ON DUPLICATE KEY UPDATE #{update_sql}"
@skojin
skojin / strip_attributes.rb
Created October 22, 2010 11:16
simple active record/model extension to strip attributes
module StripAttributes
def strip_attributes!(*attrs)
attrs.map(&:to_s)
before_validation do |r|
attrs.each do |v|
r[v].strip! if r[v]
end
end
end
end
class BitLy
include HTTParty
base_uri "http://api.bit.ly"
default_params 'login' => 'bitlyapidemo', 'apiKey' => 'R_0da49e0a9118ff35f52f629d2d71bf07'
format :json
def self.shorten(url)
r = get('/v3/shorten', :query => {'longUrl' => url} )
r['data']['url'] rescue nil
end