Rails3 devise_invitableã§Deviseã«ã¦ã¼ã¶æå¾ æ©è½ã追å ãã
Gemã®ã¤ã³ã¹ãã¼ã«
RubyGemsã«ãããã¼ã¸ã§ã³ã ã¨æ°ããdeviseã ã¨ä½¿ãã¾ããã®ã§ãããã¥ã¡ã³ãã®éãã«å¤ãdeviseãæå®ãã¾ãã
gem 'devise', '~>1.2.0' gem 'devise_invitable', '~> 0.4.0'
$ bundle install
Gitã®ã½ã¼ã¹ãç´æ¥æå®ããã°1.4ã§ã使ãã¾ãã
gem 'devise', '1.4.2' gem 'devise_invitable', :git => "https://github.com/scambra/devise_invitable.git"
$ bundle install
Deviseã®ã»ããã¢ãã
ååã®è¨äºãRails3アプリにDeviseで簡単に認証システムを組み込むãã®ãRailsããã¸ã§ã¯ãã«ã¤ã³ã¹ãã¼ã«ããåèã«Deviseãã¤ã³ã¹ãã¼ã«ãã¦ãã ããã
devise_invitableã®ã¤ã³ã¹ãã¼ã«
Wikiã«ã¯ãèªåã¤ã³ã¹ãã¼ã«ã¨æåã¤ã³ã¹ãã¼ã«ã®2éããç´¹ä»ããã¦ãã¾ãããããã§ã¯èªåã¤ã³ã¹ãã¼ã«ã試ãã¾ããã
$ rails generate devise_invitable:install insert config/initializers/devise.rb create config/locales/devise_invitable.en.yml
ãrails generate devise_invitable MODELãMODELã®ã¨ãããæå¾
ã«ã¤ããã¢ãã«åã«å¤ãã¦å®è¡ãã¾ãã
Userã®å ´åã¯ä»¥ä¸ã®éã
$ rails generate devise_invitable User insert app/models/user.rb invoke active_record create db/migrate/20110906110900_devise_invitable_add_to_users.rb
èªåã¤ã³ã¹ãã¼ã«ã§ãå®è¡ãããå 容ã¯ãWikiã®ãManual installationãã®ç¯ã®éãã§ãã
db:migrateãã¾ãã
$ reke db:migrate
以ä¸ã§ãã»ããã¢ããã¯çµäºã§ãã
æå¾ ãã
æå¾ ã¯User.inviteã¡ã½ãããå¼ã³åºãã¾ãã
User.invite!(:email => "new_user@example.com", :name => "John Doe")
ãã®æãä»ã®å±æ§ãããã·ã¥ã§æå®ã§ãã¾ãã
ã¾ããã/users/invitation/newãã«ã¢ã¯ã»ã¹ããã¨ããã©ã«ãã®æå¾ ãã¼ã¸ã表示ããã¾ãã
以éã¯ãå種ãªãã·ã§ã³ã®è¨å®ã«ã¤ãã¦è§£èª¬ãã¾ãã
Deviseã«è¿½å ãããè¨å®
- invite_for: æå¾ ã®ãã¼ã¯ã³ã®æå¹æéãè¨å®ãã¾ããããã©ã«ãã¯0ã§ãã
ãªãã·ã§ã³ã¯Userã¢ãã«ã«ä»¥ä¸ã®ç¨ã«è¿½å ãã¾ãã
devise :invitable, :database_authenticatable, :registerable,:confirmable,
:recoverable, :rememberable, :trackable, :validatable, :invite_for => 2.weeks
- invitation_limit: åã¦ã¼ã¶ãæå¾ å¯è½ãªäººæ°ã®ãªããããæå®ãã¾ããããã©ã«ãã¯ãªããããªãã§ãã
- invite_key: æå¾ ãéã£ãã¦ã¼ã¶ãèå¥ãããã¼ãããã©ã«ãã¯ã¡ã¼ã«ã¢ãã¬ã¹ã
- validate_on_invite: ãforce a record to be valid before being actually invited.ãæå¾ ããåã«ã¬ã³ã¼ãã確èªæ¸ã¿ã«ãã¦ãã¾ãï¼ è©¦ãã¦ãªãã®ã§ãªãã¨ãã
- resend_invitation: æå¾ ãåéã§ãããã©ãããããã©ã«ãã¯åéå¯è½ã
Viewã®ã«ã¹ã¿ãã¤ãº
以ä¸ã®ã³ãã³ãã§æ¨æºã®ãã¥ã¼ãããã¸ã§ã¯ãã«ã³ãã¼ãã¦ç·¨éãã¾ãã
$ rails generate devise_invitable:views create app/views/devise/invitations create app/views/devise/invitations/edit.html.erb create app/views/devise/invitations/new.html.erb create app/views/devise/mailer create app/views/devise/mailer/invitation_instructions.html.erb
- invitations/edit.html.erb: ã³ã³ãã¡ã¼ã ã¡ã¼ã«ã®ãªã³ã¯ãã¯ãªãã¯ãã¦ãacceptå¦çã®å¾ã«è¡¨ç¤ºããããã¹ã¯ã¼ãè¨å®ç»é¢
- invitations/new.html.erb: æå¾ ãã¼ã¸
- mailer/invitation_instructions.html.erb: æå¾ ã¡ã¼ã«ã®æé¢
ã¡ã¼ã«ã®ã¿ã¤ãã«ã¯ä»¥ä¸ã®ãã¡ã¤ã«ã«ããã¾ãã(2011å¹´11æ15æ¥è¿½è¨)
- config/locales/devise_invitable.en.yml
Controllerã®ã«ã¹ã¿ãã¤ãº
Devise::InvitationsControllerãç¶æ¿ããã³ã³ããã¼ã©ãä½æããroutes.rbã®devise_forã«æå®ãã¾ãã
controller/usersã«ä»¥ä¸ã®ã¯ã©ã¹ãä½æããå ´å
class Users::InvitationsController < Devise::InvitationsController end
routes.rbã¯ä»¥ä¸ã®ããã«æå®ãã¾ãã
devise_for :users, :controllers => { :invitations => 'users/invitations' }
ç¬èªã®ã³ã³ããã¼ã©ã¼ãæå®ããå ´åã¯ããã¹ãå¤ããã®ã§ä»¥ä¸ã®ã³ãã³ãã§ãã¥ã¼ãã³ã³ããã¼ã©ã«å¯¾å¿ãããã¹ã«ã³ãã¼ãã¾ãã
rails generate devise_invitable:views users