Skip to content

Instantly share code, notes, and snippets.

@HungYuHei
HungYuHei / Gemfile
Created October 19, 2012 01:41
followable with redis Sorted-Set
# Gemfile
gem 'redis', '~> 3.0.1'
@HungYuHei
HungYuHei / _form.html.erb
Created October 11, 2012 04:04 — forked from huacnlee/_form.html.erb
UpYun 表单上传验证信息 helper 方法,用于生成 input tag
<form action="http://v0.api.upyun.com/rbtest/" method="post" enctype="multipart/form-data">
<%= upyun_form_input_tag(:return_url => new_post_path) %>
<input type="file" name="file" />
<button type="submit">上传图片</button>
</form>
@HungYuHei
HungYuHei / regexp-exclude-specified-string
Created September 12, 2012 09:11
正则表达式匹配不包含指定字符串
// exclude foo_bar
^(?!.*?foo_bar).*?$
@HungYuHei
HungYuHei / smser.rb
Created August 24, 2012 11:49 — forked from mimosz/smser.rb
短信宝
# -*- encoding: utf-8 -*-
require 'digest/md5'
require 'nestful'
class Smsbao
attr_accessor :login, :passwd
def initialize(login, passwd)
@login = login
@passwd = Digest::MD5.hexdigest(passwd)
@HungYuHei
HungYuHei / README.markdown
Created August 16, 2012 12:09 — forked from greypants/README.markdown
RAILS 3: nav_link helper for adding 'selected' class to navigation elements

#Behold, the nav_link:

The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.

For full usage details, see: http://viget.com/extend/rails-selected-nav-link-helper

Drop nav_link_helper.rb into app/helpers in your Rails 3.x app and enjoy.

@HungYuHei
HungYuHei / restricted_user.sh
Created July 21, 2012 13:06
Create A Restricted User
#!/bin/bash
username="user_name"
cp -a /bin/bash /bin/rbash
useradd -c "A Restricted User" -d /home/$username -s /bin/rbash $username
rm -rf /home/$username
mkdir -m 555 /home/$username
mkdir -m 555 /home/$username/bin
cp -a /bin/ls /home/$username/bin
@HungYuHei
HungYuHei / dynamic_document.rb
Created July 17, 2012 09:15 — forked from camallen/dynamic_document.rb
Extend the fields of a Mongoid document from a .yml file
module ExtendFields
require "yaml"
private
def extend_fields
dynamic_fields.each { |name, type| self.class.field name, type: type }
end
def dynamic_fields
YAML.load_file( File.join( Rails.root, 'config', 'class_fields', "#{self.class.name.underscore}.yml" ) ) rescue {}
@HungYuHei
HungYuHei / deploy.rb
Created June 27, 2012 03:55 — forked from stympy/deploy.rb
Skip asset pre-compilation when deploying if the assets didn't change
namespace :deploy do
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
@HungYuHei
HungYuHei / gist:2923344
Created June 13, 2012 10:40
sed sample
sed -i '' "s/:disable_with/\'data-disable-with\'/g" `grep disable_with -rl app/views`
@HungYuHei
HungYuHei / solr_cap.rb
Created June 2, 2012 06:59 — forked from doitian/solr_cap.rb
sunspot solr in capistrano
namespace :deploy do
task :setup_solr_data_dir do
run "mkdir -p #{shared_path}/solr/data"
end
end
namespace :solr do
desc "start solr"
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec sunspot-solr start --port=8983 --data-directory=#{shared_path}/solr/data --pid-dir=#{shared_path}/pids"