Skip to content

Instantly share code, notes, and snippets.

# Guide
# Configure the essential configurations below and do the following:
#
# Repository Creation:
# cap deploy:repository:create
# git add .
# git commit -am "initial commit"
# git push origin master
#
# Initial Deployment:
@jhjguxin
jhjguxin / Capistrano-Deployment-Recipe.rb
Created September 3, 2012 06:39 — forked from mrrooijen/Capistrano-Deployment-Recipe.rb
a "base" Capistrano Rails Deployment Recipe. Use it to deploy your Rails application. It is also easily expandable. So feel free to grab this Recipe and add your own tasks/customization!
# Guide
# Configure the essential configurations below and do the following:
#
# Repository Creation:
# cap deploy:repository:create
# git add .
# git commit -am "initial commit"
# git push origin master
#
# Initial Deployment:
@jhjguxin
jhjguxin / deploy.rb
Created September 28, 2012 06:19 — forked from agaidot/deploy.rb
Start and Stop tasks for resque workers, with capistrano deploy hook (without God)
after "deploy:create_symlink", "deploy:restart_workers"
##
# Rake helper task.
# http://pastie.org/255489
# http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
# http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
@jhjguxin
jhjguxin / active_record_associations.rb
Created November 9, 2012 05:38 — forked from karmi/active_record_associations.rb
An example of elasticsearch & Tire setup for ActiveRecord associations
# An example of elasticsearch & Tire setup for ActiveRecord associations.
#
# A `Book has_many :chapters` scenario, with mapping and JSON serialization
# for indexing associated models.
#
# Demonstrates three important caveats as of now:
#
# 1. You you have to use `touch: true` in the `belongs_to` declaration,
# to automatically notify the parent model about the update.
#
@jhjguxin
jhjguxin / deploy.rb
Created December 19, 2012 06:40 — forked from andruby/deploy.rb
after "deploy:symlink", "deploy:restart_workers"
##
# Rake helper task.
# http://pastie.org/255489
# http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
# http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
@jhjguxin
jhjguxin / mongoid_batches_operate.rb
Last active December 14, 2015 22:48 — forked from justinko/gist:1272234
mongoid batches operate include find and remove
def find_in_batches(criteria, batch_size = 1000, sleep_seconds = 1)
current_batch = 0
count = criteria.count
while count > 0 do
criteria.skip(current_batch * batch_size).limit(batch_size).collect{|doc| yield doc}
count -= batch_size
current_batch += 1
#logger.info "now picked #{current_batch * batch_size}, batch_size is #{batch_size} then sleep #{sleep_seconds}'s ..."
sleep sleep_seconds
@jhjguxin
jhjguxin / how_to_concerns.md
Last active November 28, 2018 17:16 — forked from dhh/gist:1014971
how to concerns with rails 3

Routes

小心地使用 Match(Rails 3 已实现)

Rails 3 提供了 match 方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:

注:(r3 代表 Rails 3,r4 代表 Rails 4)

# routes.rb
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
# - a browser with WebSocket support
#
# Usage:
# ruby redis_pubsub_demo.rb
#
after "deploy:symlink", "deploy:restart_workers"
after "deploy:restart_workers", "deploy:restart_scheduler"
##
# Rake helper task.
# http://pastie.org/255489
# http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
# http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')