2007年10月16日火曜日

Aptanaでacts_as_authenticated

acts_as_authenticatedを分かる範囲で調べたところ
script/generate authenticated user account してからmigrateする。
と書いてあるけど、今回は「user」というモデルと「account」という
コントローラーが既にあるので、プラグインすることで
今まで作ったファイルが上書きされちゃわないかの確認。

「実際には変更しない = -p, --pretend」オプションで走らせてみる。
/*Aptanaの「Generator」ではうまく動かなかったので、プロンプトで
script/generate authenticated user account を実行*/

overwrite app/models/user.rb? [Ynaqd]
overwrite app/controllers/account_controller.rb? [Ynaqd]
overwrite test/functional/account_controller_test.rb? [Ynaqd]
overwrite app/helpers/account_helper.rb? [Ynaqd]
overwrite test/unit/user_test.rb? [Ynaqd]
overwrite test/fixtures/users.yml? [Ynaqd]

計6つのファイルが上書きされてしまう模様なので、
別フォルダを作って上記6つのファイルを移動させておく。

-pオプションなしで実行。




exists app/models/
exists app/controllers/
exists app/helpers/
exists app/views/account
exists test/functional/
exists test/unit/
create app/models/user.rb
create app/controllers/account_controller.rb
identical lib/authenticated_system.rb
identical lib/authenticated_test_helper.rb
create test/functional/account_controller_test.rb
create app/helpers/account_helper.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
identical app/views/account/index.rhtml
identical app/views/account/login.rhtml
identical app/views/account/signup.rhtml
exists db/migrate
Another migration is already named create_users: db/migrate/004_create_users.rb




で、できた。が。んん?見落としていたけど、最後の行は−pの時も出ていた。
User modelを作った時に出来たmigrateスクリプトも移動しておかなきゃ
いけなかったんですね。たぶん。
移動させて、もう一度authenticatedしたら、新たにdb/migrate/004_create_users.rbが
出来ました。中身を見てみると・・・




class CreateUsers < force =""> true do |t|
t.column :login, :string
t.column :email, :string
t.column :crypted_password, :string, :limit => 40
t.column :salt, :string, :limit => 40
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :remember_token, :string
t.column :remember_token_expires_at, :datetime
end
end

def self.down
drop_table "users"
end
end


うわー高機能ですね。

さて、ここで問題が。以前に作ったUsersテーブルはどうすればいいのか?

もともと作っていたuserテーブルの中身は、:group_idと:nameだけなので、
group.rb は has_many :users のまま、新しい user.rb に belongs_to :group してもう。


[ /model/user.rb ]

require 'digest/sha1'
class User <>
 [ /controller/account_controller.rb ]

(略)

def logout
self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
flash[:notice] = "You have been logged out."
redirect_back_or_default(:controller => '/schedules', :action => 'month')
end

の後ろに、昨日までに作ったaccount_controller.rbの内容を
とりあえずドバっとコピペしてみたら、ユーザー管理からログインした後に
グループ一覧へ行けるようになり、グループについては新規追加/削除/編集が出来た。
ログアウトも出来る!
・・・・と、一通り喜んでみたものの、まだDBは何も触ってません。
ので、明日はDBを:includeや:has_manyなどを使って作ろうと思ってます。

0 件のコメント: