Skip to content

Instantly share code, notes, and snippets.

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}"
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)
@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);})()
@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 / 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 / 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 / .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 / 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 / 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 / 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