04
Jul

基于Sinatra的Http认证

收集一些Sinatra的Http Basic认证程序

* Sinatra Basic Authentication – Selectively Applied
You’ll find similar DIY implementations in many sinatra projects (e.g. sinatra-wiki, …)

* sbfaulkner / sinatra_authentication
Like DIY, but uses Rack::Auth in a nice helper. Easy to use for models classes.

* blindgaenger / sinatra-auth « Yep, this project!
Adds Rack::Auth as middleware and checks protected routes before dispatching to sinatra.

* maxjustus / sinatra-authentication
If you need session based authentication using DataMapper, this is for you!

* Sinatra Lighthouse [PATCH] HTTP Authentication
But actually I really hope any HTTP authentication will become an integrated part of sinatra!

* sinatra-auth
use the basic HTTP authentication of rack in your sinatra application

01
Jul

在Rails程序中使用Gmail发送邮件

Gmail发送邮件是需要TLS的支持才可以的,ActionMailer默认不支持TLS,需要添加一个插件来实现此功能
./script/plugin install git://github.com/collectiveidea/action_mailer_optional_tls.git
添加Email设置的选项
touch config/initializers/email_setting.rb
vim config/initializers/email_setting.rb
添加如下内容即可
ActionMailer::Base.smtp_settings = {
:tls => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "YOURDOMAIN",
:authentication => :plain,
:user_name => "GOOGLEUSERNAME",
:password => "GOOGLEPASSWORD"
}

27
Jun

CSS圆角效果

一直以来做圆角效果都是通过图片“加工”出来了,很是不方便,网上也有不少CSS的例子,左一个span,右一个span的,套的人头都晕了,现在用CSS3的属性就可以直接做出圆角,而且很漂亮,不过IE对CSS3并不支持,所以还需要一个Hack,好在已经在外国朋友做了这样的事,写了一个名叫DD_roundies的JS脚本,测试了一下,效果还不错。
以下代码兼容Firefox,Chrome,Safari
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 10px;
border-radius: 5px;

加上前面提到的JS脚本在IE下也可以做到如上的圆角效果
/* IE only */
DD_roundies.addRule('.class_name_at_here', '10px');
/* varying radii, IE only */
DD_roundies.addRule('.class_name_at_here', '10px 4px');
/* varying radii, "all" browsers */
DD_roundies.addRule('.class_name_at_here', '5px', true);

27
Jun

简化Rails国际化方法的调用

Rails 提供的国际化方法在调用时多少书写起来有点麻烦(至少在我看来是这样)I18n.t ‘translate key’,我比较懒,而且极度不喜欢大小写键来回切换,找了个方法,用起来不错,记录一下,添加如下内容为一个lib文件,并在启动加载它即可以符号+l的方式调用,比如 :translate_key.l ,看起来是不是不错呢 :)
# Taken from Globalite
class Symbol # :nodoc:
# Localizes the symbol into the current locale.
# If there is no translation available, the replacement string will be returned
def localize(replacement_string = '__localization_missing__', args={})
I18n.translate(self, {:default => "#{replacement_string}(#{self})"}.merge(args))
end
alias :l :localize
def l_in(locale, args={})
I18n.translate(self, {:locale => locale, :default => "_localization_missing_(#{self})"}.merge(args)) unless locale.nil?
end
# Note that this method takes the replacement string after the args hash unlike other Globalite methods
def localize_with_args(args={}, replacement_string = '__localization_missing__')
I18n.translate(self, {:default => "#{replacement_string}(#{self})"}.merge(args))
end
alias :l_with_args :localize_with_args
end

11
Jun

Apache+Passenger部署Rails

使用Apache + Passenger部署Rails应用程序很是简单,就以下几条命令即可完成!

$ sudo gem install passenger
$ passenger-install-apache2-module

添加如下三行配置到apache2.conf的文件尾部

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.2
PassengerRuby /usr/bin/ruby1.8

生成虚拟主机配置文件,并添加如下配置内容:
sudo touch /etc/apache2/sites-available/domain.com
sudo vim /etc/apache2/sites-available/domain.com

< VirtualHost *:80 >
ServerName www.domain.com
ServerAdmin webmaster@domain.com
DocumentRoot /path/to/rails_app_name/public
RailsEnv development
RailsAllowModRewrite off
< directory “/path/to/rails_app_name/public” >
Order allow,deny
Allow from all
< /directory >
< /VirtualHost >

再做一个软链接并重启Apache
ln -s /etc/apache2/sites-available/domain.com /etc/apache2/sites-enabled/domain.com
sudo /etc/init.d/apache2 restart

这个部署方式没什么好说的,就是简单,更多的优点请自行Google,做为开发环境的配置还是很不错滴~而且Rails应用程序本身不需要做什么配置,如若重启,只需要
touch tmp.restart.txt 就搞定了.

添加基于域名的虚拟主机配置
NameVirtualHost *:80 #重点是这句
< VirtualHost *:80 >
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
< /VirtualHost>
< VirtualHost *:80 >
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
< /VirtualHost >

02
Jun

解决Rails 2.3.2中不识别mongrel prefix参数的方法

采用mongrel部署应用到子目录里会用到prefix这个选项,但此选项目前在Rails 2.3.2会出现如下错误
uninitialized constant ActionController::AbstractRequest (NameError)
这是因为Rails内部改变了类名导致的,手工Hack一下,在environment.rb里添加如下代码
Rails::Initializer.run do |config|
...
config.action_controller.relative_url_root = "/path"
...
end

在config/initializers目录下新建一个action_controller.rb并填入以下代码(当然也可以直接添加在environment.rb中)
module ActionController
class AbstractRequest < ActionController::Request
def self.relative_url_root=(path)
ActionController::Base.relative_url_root=(path)
end
def self.relative_url_root
ActionController::Base.relative_url_root
end
end
end

重启应用就O了

29
May

在VIM中注释多行代码

用VIM也有段时间,但一直没有找到列编辑的方法,很是不爽,今天要注释一段代码,要是一行一行加,非要死人不可,查了下帮助,找到如下方法,蛮实用的.

假如要在10,20行间在第1列插入或删除一个#字符
:10,20s/^\(.\{0\}\)/\#/g
:10,20s/^\(.\{1\}\)//g

13
May

在子目录中部署Rails应用程序

今天做测试需要将多个Rails应用程序部署在一个域名下的多个不同的子目录,例如:

http://domain.com/ 主应用
http://domain.com/app1 子应用1
http://domain.com/app2 子应用2

测试环境主要通过Nginx+Mongrel+Rails 2.2.2组合.Nginx和Mongrel的安装不在本文讨论范围之内,请自行Google.
(more…)