Railsの認証プラグインClearanceを使う
Merbと違って、Railsには標準的なユーザ認証機構が用意されていないので、
昔から様々な認証用プラグインが乱立していました。
login_generator,
acts_as_authenticated,
restful_authentication,
restful_openid_authentication and so on...
今回は、@a_matsudaさんや
@lchinさんの勧めもあり、
比較的新しいClearanceという認証プラグインを試してみる事にしました。
インストール方法
config/environment.rbに
ruby>>
config.gem "thoughtbot-clearance",
:lib => 'clearance',
:source => 'http://gems.github.com',
:version => '>= 0.5.3'
<<--
config/environments/test.rbに
ruby>>
config.gem 'thoughtbot-shoulda',
:lib => 'shoulda',
:source => "http://gems.github.com",
:version => '>= 2.10.0'
config.gem 'thoughtbot-factory_girl',
:lib => 'factory_girl',
:source => "http://gems.github.com",
:version => '>= 1.2.0'
<<--
と記述し、
pre>>
% sudo rake gems:install
% sudo rake gems:install RAILS_ENV=test
<<--
あとは、
pre>>
% ./script/generate clearance
<<--
これでモデル、コントローラ、View、migrationファイルの作成が行われ、
マイグレーションの実行まで完了します。
続いて、config/environments/*.rb に、HOST="hostname"のようにホスト名を設定します。test, developmentでは"localhost"、
productionでは実際に使われるホスト名を指定すれば良いようです。
さらに、config/environment.rbの中で、
ruby>>
DO_NOT_REPLY = 'do_not_reply@s21g.com'
<<--
のように、アクティベーションメールの送信もとメールアドレスを設定します。
最後に、config/routes.rbの中で、何でも良いのでrootのNamedRouteを定義します。
ruby>>
map.root :controller => 'top'
<<--
以上で完了です。
追記
現時点でのバージョン(thoughtbot-clearance (0.5.3),
thoughtbot-factory_girl (1.2.0),
thoughtbot-shoulda (2.10.1))では、
clearanceに含まれているshoulda_macrosを手動でtest
ディレクトリの下にコピーする必要があるようです。
See Also