Skip to content

Instantly share code, notes, and snippets.

@kinopyo
kinopyo / coffee-script-jquery-document-ready
Created July 9, 2011 12:20
All source code snippets used in my blog
jQuery ->
alert "hello"
$ ->
alert "hello"
@kinopyo
kinopyo / custom_logger.rb
Created October 11, 2011 15:40
Custom logger file in Rails
# lib/custom_logger.rb
class CustomLogger < Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
end
end
logfile = File.open("#{Rails.root}/log/custom.log", 'a') # create log file
logfile.sync = true # automatically flushes data to file
CUSTOM_LOGGER = CustomLogger.new(logfile) # constant accessible anywhere
@kinopyo
kinopyo / application_helper.rb
Created March 27, 2012 14:28
Rails: Detect if mobile agent
# from https://github.com/ruby-china/ruby-china/blob/master/app/helpers/application_helper.rb
MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
'webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile'
def mobile?
agent_str = request.user_agent.to_s.downcase
return false if agent_str =~ /ipad/
@kinopyo
kinopyo / gist:2343176
Created April 9, 2012 12:37
Override Devise RegistrationsController, override redirect path after updating user

Override Devise RegistrationsController

Overview

Here I'll show you

  • How to override devise registrations_controller(related to create/update user account)
  • How to change redirect path after updating user

Override RegistrationsController

@kinopyo
kinopyo / gist:1205708
Created September 9, 2011 07:53
Snippets for using mysql in ruby
require "mysql"
# more examples:
# http://www.kitebird.com/articles/ruby-mysql.html
hostname = 'localhost'
username = 'yourname'
password = 'password'
database = 'mysql'
port = 3306
@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
; Ported from https://www.autohotkey.com/board/topic/1257-half-qwerty-one-handed-typing/
; Many thanks to Chris for helping me out with this script.
mirror_1 = 0
mirror_2 = 9
mirror_3 = 8
mirror_4 = 7
mirror_5 = 6
mirror_q = p
mirror_w = o
@kinopyo
kinopyo / ruby_http_post.rb
Created June 24, 2011 06:04
Pure ruby code posting form data with parameters, and get the response.
require 'net/http'
require 'uri'
#1: Simple POST
res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
{'q' => 'ruby', 'max' => '50'})
puts res.body
#2: POST with basic authentication
res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
@kinopyo
kinopyo / 1. Add to Gemfile
Created February 2, 2012 06:26
sitemap_generator with Heroku and Amazon S3 problem: Internal server error
# in gemfile i have these lines
gem 'sitemap_generator', '2.0.1.pre1'
gem 'carrierwave'
gem 'fog'
@kinopyo
kinopyo / omniauth_macros.rb
Created November 4, 2011 05:44
Integration test with Omniauth. This example is using twitter, and assume you've installed rspec and capybara. Official document is here: https://github.com/intridea/omniauth/wiki/Integration-Testing
# in spec/support/omniauth_macros.rb
module OmniauthMacros
def mock_auth_hash
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser',