Skip to content

Instantly share code, notes, and snippets.

View zhangyuan's full-sized avatar

Yuan Zhang zhangyuan

  • Xi'an, China
  • 12:42 (UTC +08:00)
View GitHub Profile
@zhangyuan
zhangyuan / gist:5003758
Created February 21, 2013 10:28
CarrierWave Uploader Modules

Uploader::UniqFilename to generate unique file name.

# lib/uploader/uniq_filename.rb
module Uploader
  module UniqFilename
    extend ActiveSupport::Concern

    def filename
      return if original_filename.blank?
@zhangyuan
zhangyuan / gist:5003822
Created February 21, 2013 10:38
Capistrano recipe to precompile assets locally and sync public/assets directory to web server.
namespace :deploy do
desc "Sync the public/assets directory."
task :assets_sync do
system('bundle exec rake assets:precompile')
find_servers(:roles => :web).each do |s|
system "rsync -vr --exclude='.DS_Store' public/assets #{user}@#{s}:#{shared_path}/"
end
system('rm -rf public/assets')
end
@zhangyuan
zhangyuan / gist:5003871
Created February 21, 2013 10:47
Model extension with publishing status.

Concerns::Publishable is a module to extend model with publishing ability. Model should have column named of publishing_status .

module Concerns
  module Publishable
    PUBLISHING_STATUSES = %w(unapproved approved published deleted).freeze
    extend ActiveSupport::Concern

    included do
@zhangyuan
zhangyuan / gist:5464970
Created April 26, 2013 04:01
Verify Wechat message signature in Rails controller.
def verify_signature?
wechat_token = 'mysecret' # change it to the secret token
require 'openssl' unless defined?(OpenSSL)
text = %w(timestamp nonce).map {|name| params[name].to_s}.push(wechat_token).sort.join
Digest::SHA1.hexdigest(text) == params[:signature]
end
@zhangyuan
zhangyuan / gist:5924833
Last active December 19, 2015 08:19
Get Image format and characteristics with identify

format

identify -format %m image.jpg

height

identify -quiet -format %h image.jpg
@zhangyuan
zhangyuan / gist:5932220
Last active April 6, 2021 01:53
使用 CarrierWave 和 MiniMagick 合成多张图片、写文字

合成多张图片

以下只适合 v3.5.0 即以前的版本。v3.6.0 以后,对选项增加了 shellescape 减少了命令行注入的风险。

ImageMagick 的 composite 命令可以合成图片,但是一次只能合成两张。如果需要合成多张图片,得用 convert 命令和 -composite 选项。MiniMagick 的push方法,可添加选项和参数(就像在命令行操作一样)。下是一个合成多张图片的 CarrierWave 的 process 示例。

  def composite_images
    manipulate! do |img|
      img.combine_options(:convert) do |c|
@zhangyuan
zhangyuan / gist:6269419
Last active May 29, 2016 16:29
给 Rails3 项目添加简单的主从数据库分离访问

给 Rails3 项目添加简单的主从数据库分离访问

特别感谢:我实现的主从分离,基于 tumayun ( https://github.com/tumayun )的 master_slave ( https://github.com/tumayun/master_slave )项目。该项目是 rails 4 的。由于 Rails4 和 Rails3 在数据库访等方面的接口变化,导致该项目不适合 rails3,因此我将其改造成 rails3,并写了这篇笔记分析其实现。

( 我的Blog http://blog.yuaz.net 暂时无法无法创建文章,因此将这篇笔记贴在这里)

默认情况下,Rails 项目仅支持一个数据库访存。当出现数据库访问瓶颈时,就要考虑主从数据库分离:在主库上写,在从库上读。只要从库的延迟在可接受的范围内,这样的分离可以大大提高性能。本文分析Rails的源码,介绍添加从库的方式。

在程序上,如何访问从库?

@zhangyuan
zhangyuan / gist:6546730
Created September 13, 2013 04:16
判断进程是否在运行
module Process
  # Returns +true+ the process identied by +pid+ is running.
  def running?(pid)
    Process.getpgid(pid) != -1
  rescue Errno::EPERM
    true
  rescue Errno::ESRCH
    false
 end
# config/initializers/char_converter.rb
require 'uri'
module Support
class CharConverter
def initialize(app)
@app = app
end
@zhangyuan
zhangyuan / gist:7037980
Last active November 9, 2020 06:44
Scrap pages generated by javascript with casperjs
// http://casperjs.org/
var casper = require('casper').create({
pageSettings: {
loadImages: false,
}
});
var fs = require('fs');