Skip to content

Instantly share code, notes, and snippets.

@kinopyo
kinopyo / gist:3804414
Created September 29, 2012 15:47
Javascript: relative datetime
var relativeDate = function(date){
if (navigator.appName === 'Microsoft Internet Explorer') return '';
var unit = {
now: 'Now',
minute: '1 min',
minutes: ' mins',
hour: '1 hr',
hours: ' hrs',
day: 'Yesterday',
@kinopyo
kinopyo / testmail.sh
Created September 22, 2012 09:50
Bash test send mail.
#!/bin/bash
fromAddr="admin@example.com"
toAddr="foo@bar.com"
subjString="subject ha nihongo ng!?"
bodyString="hello how are you"
echo -e "From: <${fromAddr}>\nTo: <${toAddr}>\nSubject:${subjString}\n\n${bodyString}" | /usr/sbin/sendmail -t ${toAddr}
@kinopyo
kinopyo / gist:3665235
Created September 7, 2012 11:14
Rails irreversible drop table migration
class DropProductsTable < ActiveRecord::Migration
def up
drop_table :products
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
@kinopyo
kinopyo / 1_before.rb
Created September 6, 2012 04:50
Null Object Pattern example, extract from "Ben Orenstein - Refactoring from Good to Great, Boston Ruby August 2012" presentation. http://bostonrb.org/presentations/refactoring-a-live-coding-odyssey
require "ostruct"
class JobSite
attr_reader :contact
def initialize(location, contact)
@location = location
@contact = contact
end
@kinopyo
kinopyo / column_definition.rb
Created August 30, 2012 13:36
These are codes about instance_eval() in ruby: what is does, and how is it used in Rails ActiveRecord and ActiveSupport.
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null)
@kinopyo
kinopyo / gist:3444889
Created August 24, 2012 02:31
rake task example
# [RAILS_ROOT]/lib/tasks/sample.rake
desc "print hello world!" # description.
task "hello_world" do # rake task name.
p "hello world!" # print "hello world!"
end
namespace :myapp do
desc "import data from somewhere"
# load rails environment
@kinopyo
kinopyo / gist:3081634
Created July 10, 2012 06:46
Rails: Equivalent of .try() for a hash
foo = { :a => '11', :b => '22' }
foo[:b] # => "22"
# equivalent of .try() for a hash
foo.try(:[], :b) # => "22"
# using variable as key
key = :b
foo.try(:[], key) # => "22"
@kinopyo
kinopyo / gist:3007933
Created June 28, 2012 00:47
My SublimeText2 Preferences.sublime-settings
{
"color_scheme": "Packages/User/Tomorrow-Night.tmTheme",
"draw_minimap_border": true,
"find_selected_text": true,
"font_face": "Ubuntu Mono",
"font_options":
[
"subpixel_antialias"
],
"font_size": 15.0,
@kinopyo
kinopyo / gist:2987947
Created June 25, 2012 10:56
jQuery: disables button and swaps text to loading text.
// disables button and swaps text to loading text
$.fn.setLoadingState = function (loadingText) {
if (!loadingText) {
loadingText = 'Loading...'
}
$(this).prop('disabled', true).val(loadingText);
// allow jQuery chaining
return this;
};
@kinopyo
kinopyo / gist:2964009
Created June 21, 2012 05:20
My most commonly used heroku command alias.
alias hpush="git push heroku master"
alias hrc="heroku run rails console"
alias hlog="heroku logs --tail"
alias ho="heroku open"
alias hdb="heroku run rake db:migrate"