restful-authentication と aasm のインストールと設定

使用環境
OS Windows XP
Ruby 1.8.6 mswin32
Ruby on Rails 2.2.2
MySQL Community Server 5.0.77
NetBeans 6.5
Mongrel 1.1.5

aasm のインストール

gem sources -a http://gems.github.com
gem install rubyist-aasm

Git(MinGW版)のインストール

インストール - Git入門 - アットウィキ
PATH に インストールディレクトリ\bin を追加

プロジェクト作成

rails test1

restful-authentication インストール

git clone git://github.com/technoweenie/restful-authentication.git vendor/plugins/restful_authentication

generate authenticated

ruby script/generate authenticated user sessions --aasm

application.rb に追加

アプリ全体で使えるように

include AuthenticatedSystem

config/environment.rb に追加

# aasm (restful-authenticationで使用)
config.gem "rubyist-aasm", :lib => "aasm", :source => "http://gems.github.com"

# メール送信
config.active_record.observers = :user_observer

# メール送信
$ADMINEMAIL = '管理者のメールアドレス'
$SERVICE_URL = 'http://localhost:3000'

# メール送信 (Yahoo!メール)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => 'smtp.mail.yahoo.co.jp',
  :port => 587,
  :authentication => :login,
  :user_name => 'username',
  :password => 'password'
}

Yahoo!メールを使用する場合、メールソフトで送受信できるように ログイン - Yahoo!メール で設定しておく

config/routes.rb に追加

# アクティベーション
map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil

config/environments/development.rb を変更

# メール送信エラーを発生させる
config.action_mailer.raise_delivery_errors = true

app/models/user_mailer.rb を変更

class UserMailer < ActionMailer::Base
  def signup_notification(user)
    setup_email(user)
    @subject    += 'Please activate your new account'
    # CHANGE
    @body[:url]  = "#{$SERVICE_URL}/activate/#{user.activation_code}"
  end

  def activation(user)
    setup_email(user)
    @subject    += 'Your account has been activated!'
    @body[:url]  = $SERVICE_URL # CHANGE
  end

  protected
    def setup_email(user)
      @recipients  = "#{user.email}"
      @from        = $ADMINEMAIL # CHANGE
      @subject     = "[YOURSITE] "
      @sent_on     = Time.now
      @body[:user] = user
    end
end

適当にscaffoldする

ruby script/generate scaffold todo name:string

views/layouts/todo.html.erb を application.html.erb に名前変更する

データベースとテーブルを作成

rake db:create
rake db:migrate

サーバ起動

ruby script/server

テスト

ActiveRecord::StatementInvalid: Mysql::Error: Incorrect datetime value

WindowsMySQLを使用していると大量に発生
MySQLインストールディレクトリにある my.ini の

# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

コメントアウトし、MySQLを再起動する
Google グループ

users_controller_test.rb

test_should_activate_user の修正

assert_redirected_to '/session/new'

assert_redirected_to '/login'