Railsアプリにサクッとdeviseを導入のユーザー作成と似てますが、管理者はアカウント登録できないようにするので、初期の管理ユーザーをseedで作れたり、RSpecでアカウント登録できない事をテストできるようにしたいと思います。
Rails6。色々、変わってるのでメモしておきます。
余談:コロナ影響で所属会社でもリモートワークが推奨され、ここ1ヶ月通勤時間が短縮できて良いのですが、結局ダラダラしちゃって良くないという事で、GWから主に朝の時間にRailsの復習を兼ね元々やりたかったアプリ開発をスタートさせました。
今は習慣化出来てるので良いのですが、リモートワークが緩和されたら、どう継続して行くかが課題ですね。
管理者用のdeviseユーザー作成
この辺はuserをadmin_userにしただけで、他は変わりません。
$ rails g devise admin_user $ rails g devise:views admin_users $ rails g devise:controllers admin_users
config/routes.rbの2行目辺りのを変更
devise_for :admin_users
↓
devise_for :admin_users, controllers: {
sessions: 'admin_users/sessions'
}
$ rails db:migrate
【参考】ここまでのコミット内容
https://dev.azure.com/nightonly/rails-app-origin/_git/rails-app-origin/commit/6542bbf46eebb76b56aba838d3a188cb9c6133ab
管理者のアカウント登録禁止
app/models/admin_user.rbの4行目辺りのを変更
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
↓
devise :database_authenticatable,
:recoverable, :rememberable, :validatable
deviceのroutingテスト追加
spec/routing/admin_users/registrations_routing_spec.rbを作成
但し、ルートがないことの確認(.not_to be_routable)
require 'rails_helper'
RSpec.describe AdminUsers::RegistrationsController, type: :routing do
describe 'routing' do
it 'routes to #new' do
expect(get: '/admin_users/sign_up').not_to be_routable
end
it 'routes to #create' do
expect(post: '/admin_users').not_to be_routable
end
it 'routes to #edit' do
expect(get: '/admin_users/edit').not_to be_routable
end
it 'routes to #update' do
expect(put: '/admin_users').not_to be_routable
end
it 'routes to #destroy' do
expect(delete: '/admin_users').not_to be_routable
end
it 'routes to #cancel' do
expect(get: '/admin_users/cancel').not_to be_routable
end
end
end
spec/routing/users/sessions_routing_spec.rbを作成
require 'rails_helper'
RSpec.describe AdminUsers::SessionsController, type: :routing do
describe 'routing' do
it 'routes to #new' do
expect(get: '/admin_users/sign_in').to route_to('admin_users/sessions#new')
end
it 'routes to #create' do
expect(post: '/admin_users/sign_in').to route_to('admin_users/sessions#create')
end
it 'routes to #destroy' do
expect(delete: '/admin_users/sign_out').to route_to('admin_users/sessions#destroy')
end
end
end
spec/routing/users/passwords_routing_spec.rbを作成
require 'rails_helper'
RSpec.describe AdminUsers::PasswordsController, type: :routing do
describe 'routing' do
it 'routes to #new' do
expect(get: '/admin_users/password/new').to route_to('devise/passwords#new')
end
it 'routes to #new' do
expect(post: '/admin_users/password').to route_to('devise/passwords#create')
end
it 'routes to #edit' do
expect(get: '/admin_users/password/edit').to route_to('devise/passwords#edit')
end
it 'routes to #update' do
expect(put: '/admin_users/password').to route_to('devise/passwords#update')
end
end
end
$ rspec 38 examples, 0 failures
【参考】ここまでのコミット内容
https://dev.azure.com/nightonly/rails-app-origin/_git/rails-app-origin/commit/4c95420da7d18c7134fa580b7fe067869c9c5510
初期の管理ユーザーをseedで作成
初期データは追加される場合もあるので、idも明示して、既に存在する場合はSkipするようにします。
db/seeds.rbの最後に追加
Dir.glob("#{Rails.root}/db/seed/*.yml").each do |filename|
puts 'filename: ' + filename
target_model = File.basename(filename, '.yml').classify.constantize
puts 'model: ' + target_model.to_s
File.open(filename) do |file_contents|
yaml_contents = YAML.safe_load(file_contents)
yaml_contents.each do |yaml_record|
id = yaml_record['id']
if target_model.find_by(id: id)
puts 'id: ' + id.to_s + ' ... Skip create'
next
end
puts 'id: ' + id.to_s + ' ... Create'
target_model.create(yaml_record)
end
end
end
db/seed/admin_users.ymlを作成
- id: 1 email: admin@mydomain password: changepassword
$ rake db:seed filename: db/seed/admin_users.yml model: AdminUser id: 1 ... Create
ログインしてみる。
$ rails s -> http://localhost:3000/admin_users/sign_in
コミットしないけど、
app/views/layouts/application.html.erbの25行目辺りに追加すると動作確認しやすい。
<ul>
<% if admin_user_signed_in? %>
<li><%= current_admin_user.email %></li>
<li><%= link_to t('log_out'), destroy_admin_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to t('log_in'), new_admin_user_session_path %></li>
<% end %>
</ul>
<hr/>
【参考】ここまでのコミット内容
https://dev.azure.com/nightonly/rails-app-origin/_git/rails-app-origin/commit/767bc132460ca3bc71caa9d44214f71a6f4a0389

“管理者用のdeviseユーザーを作成” に対して2件のコメントがあります。